Java 我找不到无法解析名称为“DispatcherServlet”的 servlet 中名称为“index”的视图的答案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23052019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:19:29  来源:igfitidea点击:

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

javaspringtomcatspring-mvcservlets

提问by user2571047

I started study Spring MVC and tomcat just.

我刚开始学习 Spring MVC 和 tomcat。

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

我想通过 Spring 和 ThymeLeaf VewTemplate Engine 显示 html 页面。

But It doesn't work.

但它不起作用。

below my configure File and Controller.

在我的配置文件和控制器下面。

web.xml File

web.xml 文件

<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

/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'

我在浏览器上输入 localhost:9000/hello ,然后浏览器显示此错误消息。HTTP 状态 500 - 无法解析名称为“DispatcherServlet”的 servlet 中名称为“index”的视图

type Exception report

输入异常报告

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

消息无法解析名称为“DispatcherServlet”的 servlet 中名称为“index”的视图

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.

javax.servlet.ServletException:无法解析名称为“DispatcherServlet” org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1190) org.springframework.web.servlet.DispatcherServlet 的 servlet 中名称为“index”的视图。 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 Apache Tomcat/7.0.50 日志中提供了根本原因的完整堆栈跟踪。

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

index.html 位于 /webapp/WEB-INF/view/

What I to do Configure anymore?

我该怎么做 配置了?

采纳答案by Sotirios Delimanolis

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

看一下ThymeleafViewResolver#setViewNames(String[]). 它指出

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 @Controllerhandler method returns is

您的@Controller处理程序方法返回的视图名称是

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.

让 Spring 使用您的ThymeleafViewResolver,尽管在稍后的步骤中会失败,因为不存在此类已解析的 JSP。

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

相反,只需相应地摆脱viewNames<property>或修复它和您的@Controller方法。

回答by Deepak R Shankar

maybe the ant pattern you have given in your dispatcher servlet is not allowing your view resolver to resolve the view... change the url pattern to /instead of /*in your dispatcher servlet.

也许您在调度程序 servlet 中给出的蚂蚁模式不允许您的视图解析器解析视图...将 url 模式更改为/而不是/*在您的调度程序 servlet 中。

回答by Manoj Ramanan

replace the last bean alone in thymeleaf resolver with this code it will work fine

用此代码替换百里香解析器中的最后一个豆,它会正常工作

<beans:bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
    <beans:property name="templateEngine" ref="templateEngine" />

</beans:bean>