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

6

5

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>
Benzyl answered 3/5, 2014 at 16:36 Comment(5)
Have you looked at this tutorial richardbarabe.wordpress.com/2014/02/02/…?Trishatriskelion
no, i have followed for another tutorial, but do not understand where the error sufferedBenzyl
Do you have or code on GitHub or something so I can give it a try?Trishatriskelion
github.com/yuramitsyuk/my-first-appBenzyl
thanks a lot, it must be some stupid mistake, but i don't know whereit was madeBenzyl
T
4

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

Trishatriskelion answered 3/5, 2014 at 18:10 Comment(8)
Try doing a clean compile in Maven. It worked for me for both Tomcat and embedded JettyTrishatriskelion
ok with jstl its work on VMware vFabric tc Server Developer Edition v2.9-config server, but still didnt work on jetty...and i also didnt change my servlet mapping, so the problem must be in other placeBenzyl
If you follow my updated answer, the code should work on Jetty also. I verified it many timesTrishatriskelion
That is very weird... Have you updated your GitHub repository with the latest changes so I can look at it again?Trishatriskelion
GirHub repository updated. As I said my (and your) code work fine on tomcat, but still i have error when try it on jettyBenzyl
Ok, I'll check it when I get the chance and let you know!Trishatriskelion
I updated my code and used yours, and after trying http://localhost:8080/index and everything worked correctly using Jetty.Trishatriskelion
Anyway thank you for your time, when i find the solution of this problem, ill write it hereBenzyl
L
7

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.

Lentamente answered 24/4, 2017 at 18:44 Comment(0)
T
4

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

Trishatriskelion answered 3/5, 2014 at 18:10 Comment(8)
Try doing a clean compile in Maven. It worked for me for both Tomcat and embedded JettyTrishatriskelion
ok with jstl its work on VMware vFabric tc Server Developer Edition v2.9-config server, but still didnt work on jetty...and i also didnt change my servlet mapping, so the problem must be in other placeBenzyl
If you follow my updated answer, the code should work on Jetty also. I verified it many timesTrishatriskelion
That is very weird... Have you updated your GitHub repository with the latest changes so I can look at it again?Trishatriskelion
GirHub repository updated. As I said my (and your) code work fine on tomcat, but still i have error when try it on jettyBenzyl
Ok, I'll check it when I get the chance and let you know!Trishatriskelion
I updated my code and used yours, and after trying http://localhost:8080/index and everything worked correctly using Jetty.Trishatriskelion
Anyway thank you for your time, when i find the solution of this problem, ill write it hereBenzyl
T
3

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.

Trill answered 4/9, 2014 at 21:39 Comment(0)
A
2

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";
}
Aureliaaurelian answered 6/2, 2016 at 15:31 Comment(0)
H
1

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" />
Hamford answered 7/12, 2016 at 6:1 Comment(0)
A
1

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

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

Alevin answered 16/11, 2018 at 22:2 Comment(1)
This solution actually resolved the issue we were having.Och

© 2022 - 2024 — McMap. All rights reserved.