Announcement Announcement Module
Collapse
No announcement yet.
Tiles Integration in Spring 3 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Tiles Integration in Spring 3

    Hello to all,

    I have create a web site using Tiles for templating purposes but when i redirect the index.jsp to home.jsp. It throw exception unable to resolve the view name. There is home.jsp available in WebContent folder.

    Why is it cannot resolve the view name ?
    Is there any method to resolve the view name to default if nothing found?

    All my jsp files stores in WebContent folder. All my xml files stores in WEB-INF folder.

    Tiles-def.xml
    Code:
    <tiles-definitions>
    	<definition name="masterTemplate" template="/WebContent/layout.jsp">
    		<put-attribute name="header" value="/WebContent/header.jsp"></put-attribute>
    		<put-attribute name="menu" value="/WebContent/menu.jsp"></put-attribute>
    		<put-attribute name="body" value=""></put-attribute>
    		<put-attribute name="footer" value="/WebContent/footer.jsp"></put-attribute>
    	</definition> 
    	
    	<definition name="home" extends="masterTemplate">
    		<put-attribute name="body" value="/WebContent/home.jsp"></put-attribute>
    	</definition>
    		  
    </tiles-definitions>
    web.xml
    Code:
    <display-name>Spring_Tiles</display-name>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
      <listener>
      	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <servlet>
      	<servlet-name>dispatcher</servlet-name>
      	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      	<init-param>
      		<param-name>contextConfigLocation</param-name>
      		<param-value>/WEB-INF/applicationContext.xml</param-value>
      	</init-param>
      	<load-on-startup>1</load-on-startup>
      </servlet>
      
      <servlet-mapping>
      	<servlet-name>dispatcher</servlet-name>
      	<url-pattern>/</url-pattern>
      </servlet-mapping>
    dispatcher-servlet.xml
    Code:
    <context:component-scan base-package="com.peter.controller"></context:component-scan>
    	
    	<!-- HandlerMapping -> DefaultAnnotationHandlerMapping -->
    	<!--<bean id="handlerMapping" class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    	</bean>-->
    	
    	<!-- Handler Adapter -> AnnotationMethodHandlerAdapter -->
    	<!--  <bean id="handlerAdapter" class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    	</bean>-->
    	
    	<!-- View Resolver - Tiles View -->
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    		<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"></property>
    	</bean>
    	
    	<!-- Tile Configurer -->
    	<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    		<property name="definitions">
    			<list>
    				<value>/WEB-INF/tiles-def.xml</value>
    			</list>
    		</property>
    	</bean>
    	
    	<mvc:annotation-driven />
    applicationContext.xml

    Code:
    <!-- Import Servlet Context Configuration -->
    <import resource="dispatcher-servlet.xml"></import>
    index.jsp
    Code:
    <body>
    	Spring Tiles
    	<jsp:forward page="/Spring_Tiles/showHome"></jsp:forward>
    </body>
    HomeController.java
    Code:
    @Controller
    public class HomeController {
    	/**
    	 * 
    	 */
    	public HomeController() {
    	}
    	
    	@RequestMapping(value = "/showHome", method = RequestMethod.GET)
    	public String showHome() {
    		return "/home";
    	}
    }
    Please help.

    Thanks.

  • #2
    StackTrace:
    Could not resolve view with name /name in servlet with name 'dispatcher'

    org.springframework.web.servlet.Dispatcher.render

    Comment


    • #3
      I suggest you take a look at your configuration and code... The name of your view is home not /home...

      Comment


      • #4
        Your application is not configured correctly. You should have one application context that contains beans that are "universal" to your application, while you can have many dispatcher servlets configured. Think about it this way -- your context should know about the application, but not about the implementation details (the servlets), while your servlets should have knowledge of the application, but not each other. It should look something like this:

        web.xml

        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
            <display-name>your-app</display-name>
            <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/applicationContext.xml</param-value>
            </context-param>
            <listener>
                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
            </listener>
            <servlet>
                <servlet-name>spring</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                <load-on-startup>1</load-on-startup>
            </servlet>
            <servlet-mapping>
                <servlet-name>spring</servlet-name>
                <url-pattern>/</url-pattern>
            </servlet-mapping>
        </web-app>
        Now create two configuration files, one called applicationContext.xml, and the other called spring-servlet.xml, and place them both under the /WEB-INF/ directory. Actually, if you don't want to rename applicationContext.xml to something different, then you can do away with the contextConfigLocation context parameter altogether. Also, don't bother with the index.jsp redirection, and remove the welcome file list. You can create a controller that catches the "root" or "home" with the above configuration and the following handler:

        HomeController.java handler:

        Code:
        @RequestMapping(value={"/",""})
        public String index()
        		throws Exception {
        		
        	return "home";
        }
        Finally, for your tiles configuration, as was already mentioned, your tiles definition name needs to match exactly the string you return from your handler method (so "home").

        Comment


        • #5
          By using InternalResourceViewResolver, i will specify "/home" but when using URIBasedView Resolver, need to specify "home"(View name) and not the URI itself.

          For all the view in jsp, need to specify in Tiles configuration file, it is quite bad.
          Why it is view name rather that it is URL?

          Is this correct?

          I found one thing very strange where view cannot be render (display header.jsp, menu.jsp blah blah) when jsp file locate at WebContent folder but when i move to WEB-INF folder, the view can be render. What is the reason?


          Thanks.
          Last edited by peterwkc; Jun 4th, 2012, 02:05 AM.

          Comment

          Working...
          X