Java、Spring、Apache Tiles 错误:无法在名称为“dispatcher”的 servlet 中解析名称为“index”的视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23447095/
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 23:01:49  来源:igfitidea点击:

Java, Spring, Apache Tiles error : Could not resolve view with name 'index' in servlet with name 'dispatcher'

javaspring-mvcapache-tiles

提问by flopec

I'm new to Tiles and Spring MVC (I looked through several similar issues but found no solution for 'my problem')

我是 Tiles 和 Spring MVC 的新手(我查看了几个类似的问题,但没有找到“我的问题”的解决方案)

controller:

控制器:

@Controller
public class IndexController {

    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}

My 'general.xml' containing the Tiles definitions:

我的 'general.xml' 包含 Tiles 定义:

<tiles-definitions>
<definition name="common" template="/WEB-INF/layout/classic.jsp">
    <put-attribute name="footer" value="/WEB-INF/layout/footer.jsp" />
</definition>

<definition name="index" extends="common">
    <put-attribute name="title" value="My First Application" />
    <put-attribute name="body" value="/WEB-INF/jsp/index.jsp" />
</definition>

exception:

例外:

javax.servlet.ServletException: Could not resolve view with name 'index' in servlet with name 'dispatcher'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:738)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:551)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:586)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1111)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:478)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1045)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:261)
at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:101)
at org.eclipse.jetty.servlet.DefaultServlet.doGet(DefaultServlet.java:552)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:738)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:551)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:568)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1111)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:478)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1045)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:199)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:109)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:462)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:279)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:232)
at org.eclipse.jetty.io.AbstractConnection.run(AbstractConnection.java:534)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
at org.eclipse.jetty.util.thread.QueuedThreadPool.run(QueuedThreadPool.java:536)
at java.lang.Thread.run(Thread.java:744)

dispatcher-servlet.xml:

调度程序servlet.xml:

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/defs/general.xml</value>
        </list>
    </property>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>  
</bean>

web.xml

网页.xml

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

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.html</url-pattern>
    <url-pattern>*.htm</url-pattern>
    <url-pattern>*.json</url-pattern>
    <url-pattern>*.xml</url-pattern>
</servlet-mapping>

采纳答案by geoand

The problem is extremely simple. Replace your web.xml with

问题非常简单。将您的 web.xml 替换为

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>my-first-app</display-name>


  <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

I also added a jstl dependency to the pom

我还向 pom 添加了 jstl 依赖项

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

After these changes, a request to localhost:8080/indexcorrectly renders the appropriate response

在这些更改之后,localhost:8080/index正确呈现适当响应的请求

回答by Alex San

I've had the same issue today. I am also new at Spring so I didn't know where to start. After a long day I managed to find the answer:

我今天遇到了同样的问题。我也是 Spring 的新手,所以我不知道从哪里开始。经过漫长的一天,我设法找到了答案:

My default.xml file was wrong. I had done some manual refactoring and that caused all the trouble. This is an example of what was wrong:

我的 default.xml 文件是错误的。我做了一些手动重构,这导致了所有的麻烦。这是错误的示例:

<definition name="tos.base" template="/WEB-INF/templates/default.jsp">
<put-attribute name="includes" value="" ></put-attribute>
<put-attribute name="title" value="Title" ></put-attribute>
<put-attribute name="header" value="/WEB-INF/tiles/header.jsp"></put-attribute>
<put-attribute name="content" value="/WEB-INF/tiles/content.jsp"></put-attribute>
<put-attribute name="footer" value="/WEB-INF/tiles/footer.jsp"></put-attribute>
</definition>

<definition name="home" extends="tos.base">
<put-attribute name="title" value="Homepage" ></put-attribute>
<put-attribute name="content" value="/WEB-INF/tiles/home.jsp"></put-attribute>
</definition>

<definition name="current" extends="to.base">
<put-attribute name="title" value="Current" ></put-attribute>
<put-attribute name="content" value="/WEB-INF/tiles/current.jsp"></put-attribute>
</definition>

As you can see I had the wrong value in the last definition "extends" value. I discovered this downgrading from tiles3.TilesViewResolver to tiles2.TilesViewResolver and that provided a more helpful error message.

如您所见,我在最后一个定义“扩展”值中使用了错误的值。我发现从tiles3.TilesViewResolver 降级到tiles2.TilesViewResolver 并提供了更有用的错误消息。

回答by speksy

Try to change the return value of IndexController.index()from "/WEB-INF/jsp/index.jsp"to "index". This works for me:

尝试将IndexController.index()from的返回值更改"/WEB-INF/jsp/index.jsp""index"。这对我有用:

@RequestMapping("/index")
public String index() {
    return "index";
}

回答by hemant

I've had the same issue . Try using correct version of DTD in tiles configuration file.

我有同样的问题。尝试在磁贴配置文件中使用正确版本的 DTD。

https://stackoverflow.com/a/28854479

https://stackoverflow.com/a/28854479

Also in your tiles definition file general.xmluse tile:insertAttributeinstead of tile:addAttribute

同样在您的瓷砖定义文件general.xml 中使用tile:insertAttribute而不是tile:addAttribute

<tile:insertAttribute name="body"/>
     <br />
<tile:insertAttribute name="footer" />

回答by abagri

The problem lies in the project location.

问题在于项目位置。

A combination of jetty, apache tiles and spring's view resolver does not work when there is a space in the location.

当位置有空间时,jetty、apache tile 和spring 的视图解析器的组合不起作用。

For ex : a project location - D:\folder whitespace\your_project will throw the above error -Could not resolve view with name 'index' in servlet with name 'dispatcher'

例如:项目位置 - D:\folder whitespace\your_project 将抛出上述错误 - 无法解析名称为 'dispatcher' 的 servlet 中名称为 'index' 的视图

Solution - Please change your project location to a path with no space in it.

解决方案 - 请将您的项目位置更改为没有空格的路径。

回答by Gene

There's probably a whitespace in your project's path:

您的项目路径中可能有空格:

https://www.youtube.com/watch?v=JAYjZnykalg

https://www.youtube.com/watch?v=JAYjZnykalg