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

StackOverflow https://stackoverflow.com/questions/23447095

  •  14-07-2023
  •  | 
  •  

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

controller:

@Controller
public class IndexController {

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

My 'general.xml' containing the Tiles definitions:

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

dispatcher-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>
有帮助吗?

解决方案 2

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

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

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

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

其他提示

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.

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'

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

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:

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:

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

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

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

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

https://stackoverflow.com/a/28854479

Also in your tiles definition file general.xml use tile:insertAttribute instead of tile:addAttribute

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

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top