Spring Loads "TemplateInputException: error resolution pattern" when starting from jar

I have an application that works great when launched in IntelliJ or through the Gradle bootloader.

however, if I boot bootRepackage and then try to run the resulting jar, I get the following:

2014-12-02 21:46:14.086 ERROR 9839 --- [nio-2014-exec-2] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-2014-exec-2] Exception processing template "/login": Error resolving template "/login", template might not exist or might not be accessible by any of the configured Template Resolvers 2014-12-02 21:46:14.087 ERROR 9839 --- [nio-2014-exec-2] oaccC[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/login", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/login", template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:245) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011) at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335) 

I see that the bank has / templates / **. the content looks fine to me.

One of the possible (?) Factors may be that I am using an HTML page related to the layout in this way:

  layout:decorator="layouts/main" 

I can confirm that the file is in the bank.

/ login is defined as follows:

 @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/login").setViewName("/login"); registry.addViewController("/").setViewName("/login"); } 

and Spring Spring is configured as:

 @Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity security) { security.ignoring().antMatchers("/assets/**"); } @Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable(); http .authorizeRequests() .antMatchers("/").permitAll() .anyRequest().authenticated(); http .formLogin() .loginPage("/login") .defaultSuccessUrl("/home") .failureUrl("/login?error") .permitAll() .and() .logout() .invalidateHttpSession(true) .logoutSuccessUrl("/login?logout") .permitAll(); } } 

I think that everything that may be relevant to this problem ...

I saw https://github.com/spring-projects/spring-boot/issues/112 and the correct location of Thymeleaf views for Spring (among others). Despite these resources, I could not get the permission of the template.

Any suggestions gratefully received.

Going so far with Spring Boot, but still tripping over the last hurdle (near-final deployment) is annoying.

+10
source share
7 answers

I had the same problem using spring boot with default thimeleaf. Everything worked until I tried .jar

the message says: ... org.thymeleaf.exceptions.TemplateInputException: "/ fragments / footer" error resolution template

I decided to play with /, the problem was that they did not need a slash.

I changed this:

 <footer class="footers" th:replace="/fragments/footer :: footer"> 

for this:

 <footer class="footers" th:replace="fragments/footer :: footer"> 
+6
source

The answer seems to be here: https://github.com/spring-projects/spring-boot/issues/1744

In a nutshell: if the resource path contains "//", everything goes wrong.

The fix is ​​to modify application.yml like this:

 spring: thymeleaf: prefix: classpath:/templates 

(the fix is ​​similar for the .properties file, of course)

The effect of the hook is that all links to templates or anything else must be followed by a slash, as in (Thymeleaf.html file):

  layout:decorator="/layouts/main" 

or (Groovy controller):

 @RequestMapping(value = "/home", method = RequestMethod.GET) def home(Model model, Principal principal) { "/home/home" } 

I think spring loading should fix this. Urgently. This is REALLY bad ergonomics, which explodes very much, just at the moment when a person feels that he is approaching the final deployment.

+5
source

Ok, I found where you should look carefully. In your Controller classes, when you use @RequestMapping for a class, there is no slash (for example, @RequestMapping("property") ). In the method, you need a leading slash (for example, @GetMapping("/list") ), and the return value does not have a slash (for example, return "property/list"; )

Fragment Example

 @Controller @RequestMapping("property") public class PropertyController { @NonNull PropertyService service; @GetMapping("/list") public String list() { return "property/list"; } } 
+5
source

spring.thymeleaf.prefix = classpath: / templates /

change

 @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/login").setViewName("/login"); registry.addViewController("/").setViewName("/login"); } 

to

 @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/login").setViewName("login"); registry.addViewController("/").setViewName("login"); } 

view name must be relative path

0
source

After many trials, I found a way to solve my template error problem. I don't know if this is due to the new version of spring-boot, but my change worked.

Each request through the controller instead of returning String "/ login", for example, I modify to create a ModelAndView object:

 @RequestMapping(value="/login", method=RequestMethod.GET) public ModelAndView login() { ModelAndView mv = new ModelAndView("login"); return mv; } 

hugs everyone.

0
source

I ran into a problem when using spring-boot-starter-thymeleaf

So I used the dependency below,

  <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.9.RELEASE</version> </dependency> 
0
source

If the above solutions do not work for you, like me,

I changed my controller to RestController. This fixed my problem.

This is if you use a Front End application, such as Angular, to communicate with the Back-end application.

0
source

Source: https://habr.com/ru/post/1208139/


All Articles