Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't find answer that Could not resolve view with name 'index' in servlet with name 'DispatcherServlet'

I started study Spring MVC and tomcat just.

I want to display html page by Spring and ThymeLeaf VewTemplate Engine.

But It doesn't work.

below my configure File and Controller.

web.xml File

<servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

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

<filter>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

/webapp/WEB-INF/DispatcherServlet-serlvet.xml

 <context:component-scan base-package="com.everblog.controller" />

<!-- ViewResolver be configured by thymeleaf -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".html" />
    <property name="templateMode" value="HTML5" />
    <property name="characterEncoding" value="UTF-8" />
</bean>

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

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

And Controller

@Controller
public class PostItemController {

   @RequestMapping(value="/hello", method = RequestMethod.GET)
   public String helloWorld() {
      System.out.println("IS IN");
      return "index";
   }
}

I typed localhost:9000/hello on browser, then browser display this error messages. HTTP Status 500 - Could not resolve view with name 'index' in servlet with name 'DispatcherServlet'

type Exception report

message Could not resolve view with name 'index' in servlet with name 'DispatcherServlet'

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Could not resolve view with name 'index' in servlet with name 'DispatcherServlet' org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1190) org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.50 logs.

index.html is located on /webapp/WEB-INF/view/

What I to do Configure anymore?

like image 930
user2571047 Avatar asked Apr 14 '14 04:04

user2571047


1 Answers

Take a look at the javadoc of ThymeleafViewResolver#setViewNames(String[]). It states

Specify a set of name patterns that will applied to determine whether a view name returned by a controller will be resolved by this resolver or not.

The view name your @Controller handler method returns is

return "index";

That view name definitely does not match the pattern

*.html

You would have to use

return "index.html";

for Spring to use your ThymeleafViewResolver, though that would fail at a later step since no such resolved JSP exists.

Instead, just get rid of the viewNames <property> or fix it and your @Controller method accordingly.

like image 105
Sotirios Delimanolis Avatar answered Oct 07 '22 23:10

Sotirios Delimanolis