Java,Spring,Apache Tiles错误:无法解析名为'dispatcher'的servlet中名为'index'的视图

时间:2014-05-03 16:36:07

标签: java spring-mvc apache-tiles

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

控制器:

@Controller
public class IndexController {

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

我的' 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>

异常:

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$2.run(AbstractConnection.java:534)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
at java.lang.Thread.run(Thread.java:744)

调度-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

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

6 个答案:

答案 0 :(得分:3)

问题非常简单。 用

替换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>

我还在pom中添加了一个jstl依赖

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

完成这些更改后,对localhost:8080/index的请求会正确呈现相应的响应

答案 1 :(得分:3)

问题在于项目位置。

当位置有空间时,jetty,apache瓷砖和弹簧的视图解析器组合不起作用。

例如:项目位置 - D:\文件夹空白\ your_project 将抛出上述错误 - 不能解析名称为&#39; index&#39;的视图在名为&#39; dispatcher&#39;

的servlet中

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

答案 2 :(得分:2)

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

我的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>

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

答案 3 :(得分:1)

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

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

答案 4 :(得分:0)

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

https://stackoverflow.com/a/28854479

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

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

答案 5 :(得分:0)

项目路径中可能存在空格:

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