简体   繁体   中英

Unable to resolve view using Thymeleaf in Spring mvc

I am using Thymeleaf in view part of my Spring mvc.But I am unable to get the view pages while making request from browser.

Please find below Thymeleaf configuration:

<bean id="templateResolver"
class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
    <property name="prefix" value="/WEB-INF/templates/" />
    <property name="suffix" value=".html" />
    <property name="templateMode" value="HTML5" />
</bean>

<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver" />
</bean>

<bean id="thymeleafViewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine" />
    <property name="order" value="1" />
    <property name="viewNames" value="*.html" />
</bean>

My Controller class is defined as below:

@Controller
public class PersonController {

  @RequestMapping(value = "/home.html", method = RequestMethod.GET)
  public String navigateToPerson() {
    return "home";
  }
}

I have my view page home.html is located at: \\WEB-INF\\templates\\home.html I have already checked the mapping of url in weblogic console:

INFO: Mapped "{[/home.html],methods=[GET]}" onto public java.lang.String com.userapp.web.PersonController.navigateToPerson()

My web.xml as below:

<servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>
     org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/applicationConfig/applicationConfig.xml</param-value>
  </init-param>
 </servlet>

 <servlet-mapping>
   <servlet-name>DispatcherServlet</servlet-name>
   <url-pattern>*.html</url-pattern>
 </servlet-mapping>

Now when I am making a request to browser as below: http://localhost:7040/UserApplication/home.html

I am getting following error in weblogic console:

Root cause of ServletException.
javax.servlet.ServletException: Could not resolve view with name 'home' in 
servlet with name 'DispatcherServlet'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1266)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
Truncated. see log file for complete stacktrace

I recently faced same issue which i resolved by removing viewNames

According to your xml, you have

<bean id="thymeleafViewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine" />
    <property name="order" value="1" />
    <property name="viewNames" value="*.html" />
</bean>

kindly remove *viewNames** property

<bean id="thymeleafViewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine" />
    <property name="order" value="1" />
</bean>

That should resolve it.

DEPENDENCIES & VERSIONS

spring-webmvc ----> 5.2.1

thymeleaf-spring5 ----> 3.0.11

javax.servlet-api ----> 4.0.1

Note : Same thing applies to Java Based Configuration, just remove setViewNames config ( my codebase that implemented this fix, uses Java Based Configuration )... I'm just starting out with Thymeleaf and why this was able to fix it, i really don't know (Anyone who knows why, should kindly leave a comment)

Hope this helps out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM