How refer the /WEB-INF/ content how a dependency for a module for Testing


(Manuel Jordan) #1

I am working with:

  • Spring Framework - 5.0.4.RELEASE
  • Gradle - 4.7
  • Thymeleaf - 3.0.9.RELEASE

All the project is based with multi modules.

Among many modules I have these two:

  • thymeleaf-02-controller: all about the @Controllers
  • thymeleaf-02-web: all about the .html files (css, js too)

Yes, is important have separated all about the .java files from all about the web files (.html, .js etc …)

About the Gradle their configurations:

In the thymeleaf-02-web module exists:

apply plugin: 'war'

project(':thymeleaf-02-web') {

    description 'Web (HTML, JS, CSS)'

    dependencies {

        exportedProjects.each{
            if("$it"!=":thymeleaf-02-web")
                compile project("$it")
        }

    }

    webAppDirName = 'src/main/webapp'

    war {
        version=''
        baseName = warBaseName
    }

    ...

}

In the thymeleaf-02-controller module exists:

project(':thymeleaf-02-controller') {

    description 'Web - Controller'

    dependencies {

       compile project(':thymeleaf-02-infrastructure')
       compile project(':thymeleaf-02-service-api')

       ...      

       //Omicron: 
       //What is the correct approach?
       //The code shown below does not work
       testCompile project(':thymeleaf-02-web')
       testRuntime project(':thymeleaf-02-web')       
       testRuntime fileTree(dir: "/WEB-INF/view/html", include: '*.html')

    }

}

Note: If the multi module project is exported to a .war file all work fine, it of course in Tomcat. Thus the runtime environment is stable

The problem is about the testing environment, it just for the thymeleaf-02-controller module. Thus always appears:

[main] ERROR org.thymeleaf.TemplateEngine - 
[THYMELEAF][main] Exception processing template "persona/deleteOne": 
An error happened during template parsing 
(template: "ServletContext resource 
[/WEB-INF/view/html/persona/deleteOne.html]") 
org.thymeleaf.exceptions.TemplateInputException: 
An error happened during template parsing 
(template: "ServletContext resource 
[/WEB-INF/view/html/persona/deleteOne.html]")

...

Caused by: java.io.FileNotFoundException: 
Could not open ServletContext resource 
[/WEB-INF/view/html/persona/deleteOne.html]

...

The error is clear, thanks to FileNotFoundException

What is the correct approach to resolve the Omicron section shown above?.

Therefore only for testing the thymeleaf-02-controller module is not able to refer/reach/see/use all the .html files located in the /WEB-INF/view/html/.. directory from the thymeleaf-02-web module.

One thing that is curious is that must exists at least one .java file in the src/main/java Source Folder in the thymeleaf-02-web module to see in the thymeleaf-02-controller module the thymeleaf-02-web how a dependency through the Project and External Dependencies. Otherwise thymeleaf-02-web does not appear how a dependency.


(James Justinic) #2

Even though the current approach works when deployed to a WAR in Tomcat, I would update the approach over hacking something together in the Gradle script. The problem is that the code tries to load the views from a file location that is not consistent between your different execution environments.

Instead, you can configure Spring to load the Thymeleaf views from the classpath. The classpath location can be consistent between environments without much effort. Your controller project would depend on the project with the views like any other dependency and your views would be available to both the application and tests.


(Manuel Jordan) #3

Thanks for the reply.

I remember something similar about Tiles in other project about the .xml files.
They were located within the WEB-INF directory and was mandatory move them to the classpath Thus the following is valid:

@Bean
public TilesConfigurer tilesConfigurer() {
    	TilesConfigurer tilesConfigurer = new TilesConfigurer();
    	tilesConfigurer.setDefinitions("classpath:/com/manuel/jordan/tiles/tiles-definitions.xml");
    	tilesConfigurer.setCheckRefresh(false);
    	return tilesConfigurer;
}

But here the problem with Thymeleaf is that we are talking about .html files. Thus is mandatory they be located within the WEB-INF path. Perhaps I am missing something and you have some snippet code to get a better perspective of your idea?

Thank you


(James Justinic) #4

Thymeleaf has no such requirement. Earlier Thymeleaf releases contained multiple implementations of the ITemplateResolver, which allowed for resolution from the ServletContext, URLs, files and the class loader. In 3.x, additional implementations were added.

However, Thymeleaf recommends using the SpringResourceTemplateResolver for Spring Applications. This template resolver delegates resource loading to the Spring ResourceLoader, which has support for prefixes starting with classpath:. I don’t see anything unique in your post that’s not covered by the Thymeleaf Tutorials except the simple change from /WEB-INF/view/html to classpath:/view/html. I don’t think they mention that in the context of Spring.


(Manuel Jordan) #5

I just remember about that from Spring Boot, of course my case is about Spring Framework

I just found these two posts and is just you have said:

Thanks