Announcement Announcement Module
Collapse
No announcement yet.
[SOLVED] Tiles integration with Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [SOLVED] Tiles integration with Spring

    Hello,

    i've read "spring in action" and "struts in action" but i not able to integrate Tiles correclty.

    Sorry to ask this stupid question but, could someone provide me an example (from a real case ; or url etc... ) ?

    with web.xl ; applicationContext ; [app]-servlet.xml ( with the viewResolver bean we have to use and if i can always use a views.properties files like i did with jslt ? ) and the java bean + jsp ?


    thanks in advance

    regards.

  • #2
    The Spring distro features a Tiles example that shows most of the tiles integration features. Changing the InternalResourceViewResolver to a ResourceBundleViewResolver and including a views.properties file mentioning TilesJstlViews instead of normal JstlViews should do the trick.

    Hope this helps,

    rgds,
    Alef Arendsen

    Comment


    • #3
      Originally posted by Alef Arendsen
      The Spring distro features a Tiles example that shows most of the tiles integration features. Changing the InternalResourceViewResolver to a ResourceBundleViewResolver and including a views.properties file mentioning TilesJstlViews instead of normal JstlViews should do the trick.

      Hope this helps,

      rgds,
      Alef Arendsen
      I've seen this example, i tried it twice ; i will try once again

      Comment


      • #4
        If you have any specific issues with the app or changing the view resolver to a resource bundle view resolver, please let me know.

        rgds,
        Alef Arendsen

        Comment


        • #5
          before trying to integrate Tiles my controller looked like tihs

          Code:
          package infra.web;
          
          
          
          import javax.servlet.ServletException;
          
          import javax.servlet.http.HttpServletRequest;
          
          import javax.servlet.http.HttpServletResponse;
          
           
          
          import org.springframework.beans.factory.InitializingBean;
          
          import org.springframework.context.ApplicationContextException;
          
          
          
          import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
          
          import org.springframework.web.servlet.ModelAndView;
          
          
          
          import infra.Infra;
          
          
          
          
          
          import java.io.IOException;
          
          
          
          import org.apache.commons.logging.Log;
          
          import org.apache.commons.logging.LogFactory;
          
          
          
          public class InfraController extends MultiActionController implements InitializingBean {
          
          	private Infra infra;
          
          
          
          	public void setInfra(Infra infra) {
          
          		this.infra = infra;
          
          	}
          
          
          
          	public void afterPropertiesSet() throws Exception {
          
          		if (infra == null)
          
          			throw new ApplicationContextException("Must set infra bean property on " + getClass());
          
          	}
          
          
          
              /** Logger for this class and subclasses */
          
              protected final Log logger = LogFactory.getLog(getClass());
          
          
          
              /**
          
          	 * Custom handler for index
          
          	 * @param request current HTTP request
          
          	 * @param response current HTTP response
          
          	 * @return a ModelAndView to render the response
          
          	 */
          
              public ModelAndView indexHandler(HttpServletRequest request, HttpServletResponse response)
          
                      throws ServletException, IOException {
          
          
          
                  String now = (new java.util.Date()).toString();
          
                  logger.info("returning index view with " + now);
          
          
          
                  return new ModelAndView("indexView", "now", now);
          
              }
          
              
          
              /**
          
          	 * Custom handler for appli
          
          	 * @param request current HTTP request
          
          	 * @param response current HTTP response
          
          	 * @return a ModelAndView to render the response
          
          	 */
          
              public ModelAndView appliHandler(HttpServletRequest request, HttpServletResponse response)
          
              	throws ServletException, IOException {
          
          
          
              	String now = (new java.util.Date()).toString();
          
              	logger.info("returning appli view with " + now);
          
          
          
              	return new ModelAndView("appliView", "applis", infra.getApplis());
          
              }
          
              
          
              /**
          
          	 * Custom handler for appserver
          
          	 * @param request current HTTP request
          
          	 * @param response current HTTP response
          
          	 * @return a ModelAndView to render the response
          
          	 */
          
              public ModelAndView appserverHandler(HttpServletRequest request, HttpServletResponse response)
          
              	throws ServletException, IOException {
          
          
          
              	String now = (new java.util.Date()).toString();
          
              	logger.info("returning appserver view with " + now);
          
          
          
              	return new ModelAndView("appserverView", "appservers", infra.getAppServers());
          
              }
          
              
          
              /**
          
          	 * Custom handler for oracle
          
          	 * @param request current HTTP request
          
          	 * @param response current HTTP response
          
          	 * @return a ModelAndView to render the response
          
          	 */
          
              public ModelAndView oracleHandler(HttpServletRequest request, HttpServletResponse response)
          
              	throws ServletException, IOException {
          
          
          
              	String now = (new java.util.Date()).toString();
          
              	logger.info("returning oracle view with " + now);
          
          
          
              	return new ModelAndView("oracleView", "oracles", infra.getOracles());
          
              }
          
              
          
              /**
          
          	 * Custom handler for env
          
          	 * @param request current HTTP request
          
          	 * @param response current HTTP response
          
          	 * @return a ModelAndView to render the response
          
          	 */
          
              public ModelAndView envHandler(HttpServletRequest request, HttpServletResponse response)
          
              	throws ServletException, IOException {
          
          
          
              	String now = (new java.util.Date()).toString();
          
              	logger.info("returning env view with " + now);
          
          
          
              	return new ModelAndView("envView", "envs", infra.getEnvs());
          
              }
          
              
          
              /**
          
          	 * Custom handler for appserver
          
          	 * @param request current HTTP request
          
          	 * @param response current HTTP response
          
          	 * @return a ModelAndView to render the response
          
          	 */
          
              public ModelAndView serverHandler(HttpServletRequest request, HttpServletResponse response)
          
              	throws ServletException, IOException {
          
          
          
              	String now = (new java.util.Date()).toString();
          
              	logger.info("returning server view with " + now);
          
          
          
              	return new ModelAndView("serverView", "servers", infra.getServers());
          
              }

          Now when i try to use Tiles ; the controller has to implement or extends Controller class.

          how ?

          when i change my infraController class to
          Code:
          public class InfraController extends ComponentControllerSupport implements Controller {
          i have to declare a mandatory method like eclipse warns me :

          The type InfraController must implement the inherited abstract
          method Controller.handleRequest(HttpServletRequest,
          HttpServletResponse)
          regards.


          ps : in fact my application is a mix of the two sample provide by springframework : petclinic and tiles-example.
          but i dont know how to clearly integrate them together.

          Comment


          • #6
            now i'm here :
            my app-servlet.xml file

            Code:
            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
            
            <!--
              - Application context definition for "InfraEnvironment" DispatcherServlet.
              -->
            
            <beans>
            
            	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            		<property name="basename" value="messages"/>                                                                           
            	</bean>	
            
            	<!-- ========================= MAPPING DEFINITIONS ========================= -->
            
            	<!--
            	  - This bean provides explicit View mappings in a resource bundle instead of the
            	  - default InternalResourceViewResolver. It fetches the view mappings from
            	  - localized "views_xx" classpath files, i.e. "/WEB-INF/classes/views.properties"
            	  - or "/WEB-INF/classes/views_de.properties".
            	  -
            	  - Symbolic view names returned by Controllers will be resolved by this bean
            	  - using the respective properties file, which defines arbitrary mappings between
            	  - view names and resources.
            	  -->
            	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            		<property name="requestContextAttribute" value="rc"/>
            		<property name="viewClass" value="org.springframework.web.servlet.view.tiles.TilesJstlView"/>
            	</bean>
            	
            	<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles.TilesConfigurer">
            		<property name="factoryClass" value="org.apache.struts.tiles.xmlDefinition.I18nFactorySet"/>	
            		<property name="definitions">
            			<list>
            				<value>/WEB-INF/defs/definitions.xml</value>
            			</list>
            		</property>
            	</bean>	
            	<!--
            	  - This bean resolves specific types of exception to corresponding error views.
            		- The default behaviour of DispatcherServlet is to propagate all exceptions to the
            		- servlet container&#58; This will happen here with all other types of exception.
            		-->
            	<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
            		<property name="exceptionMappings">
            			<props>
            				<prop key="org.springframework.dao.DataAccessException">dataAccessFailure</prop>
            				<prop key="org.springframework.transaction.TransactionException">dataAccessFailure</prop>
            			</props>
            		</property>
            	</bean> 
             	<!--
            	 - This bean is an explicit URL mapper that is used by the "infra" DispatcherServlet
            	 - It is used instead of the default BeanNameUrlHandlerMapping.
            	 -->
            	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            		<property name="mappings">
            			<props>
            				<prop key="/*.html">viewController</prop>
            			</props>
            		</property>
            	</bean>
            
            
            	<!-- ========================= CONTROLLER DEFINITIONS ========================= -->
            	
            	<bean id="viewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
            	
            	<!--
            	  - This bean is a MultiActionController that manages general View rendering.
            	  - It uses the "infraControllerResolver" bean below for method name resolution.
            	  -->
            <!-- 
            	<bean id="infraController" class="com.fimasys.infra.web.InfraController">
            		<property name="methodNameResolver" ref="infraControllerResolver"/>
            		<property name="infra" ref="infra"/>
            	</bean>
             -->	
             	<bean id="indexController" class="com.fimasys.infra.web.IndexController">
             			<property name="infra" ref="infra"/>
             	</bean>
             	<bean id="appliController" class="com.fimasys.infra.web.AppliController">
             			<property name="infra" ref="infra"/>
             	</bean>
             	<bean id="appserverController" class="com.fimasys.infra.web.AppServerController">
             			<property name="infra" ref="infra"/>
             	</bean>
             	<bean id="serverController" class="com.fimasys.infra.web.ServerController">
             			<property name="infra" ref="infra"/>
             	</bean> 	
             	<bean id="oracleController" class="com.fimasys.infra.web.OracleController">
             			<property name="infra" ref="infra"/>
             	</bean> 	
             	<bean id="envController" class="com.fimasys.infra.web.EnvController">
             			<property name="infra" ref="infra"/>
             	</bean> 	
             		
            </beans>
            my defs/defintions.xml tiles file :
            Code:
            <?xml version="1.0" encoding="ISO-8859-1" ?>
            
             <!DOCTYPE tiles-definitions PUBLIC
                   "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
                   "http&#58;//jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
            
            <tiles-definitions>
            
            	<!-- DEFAULT MAIN TEMPLATE -->
            	<definition name="maintpl" path="/WEB-INF/jsp/main.jsp">
            		<put name="menu_left"   value="/menu_left.jsp"/>
            		<put name="menu_right"  value="/menu_right.jsp"/>
            	</definition>
            
            	<definition name="indexView" extends="maintpl">
            		<put name="content" value="/WEB-INF/jsp/index.jsp" controller="com.fimasys.infra.web.IndexController"/>
            		<put name="index" value="Index"/>
            	</definition>
            
            	<definition name="appliView" extends="maintpl">
            		<put name="content" value="/WEB-INF/jsp/appli.jsp"/>
            		<put name="index" value="Appli"/>
            	</definition>
            
            	<definition name="appserverView" extends="maintpl">
            		<put name="content" value="/WEB-INF/jsp/appserver.jsp"/>
            		<put name="index" value="Serveurs d'Appli"/>		
            	</definition>
            
            	<definition name="envView" extends="maintpl">
            		<put name="content" value="/WEB-INF/jsp/env.jsp"/>
            		<put name="index" value="Environnements"/>		
            	</definition>
            
            	<definition name="oracleView" extends="maintpl">
            		<put name="content" value="/WEB-INF/jsp/oracle.jsp"/>
            		<put name="index" value="Oracle"/>		
            	</definition>
            
            	<definition name="serverView" extends="maintpl">
            		<put name="content" value="/WEB-INF/jsp/server.jsp"/>
            		<put name="index" value="Serveur"/>		
            	</definition>
            </tiles-definitions>
            my IndexController
            Code:
            package com.fimasys.infra.web;
            
            import javax.servlet.http.HttpServletRequest;
            import javax.servlet.http.HttpServletResponse;
             
            import org.springframework.web.servlet.mvc.Controller;
            import org.springframework.web.servlet.ModelAndView;
            
            import com.fimasys.infra.Infra;
            
            import org.apache.commons.logging.Log;
            import org.apache.commons.logging.LogFactory;
            
            public class IndexController implements Controller &#123;
            
            	private Infra infra;
            	
            	public void setInfra&#40;Infra infra&#41; &#123;
            		this.infra = infra;
            	&#125;
            
            	
                /** Logger for this class and subclasses */
                protected final Log logger = LogFactory.getLog&#40;getClass&#40;&#41;&#41;;
            
                /**
            	 * Custom handler for handleRequest
            	 * @param request current HTTP request
            	 * @param response current HTTP response
            	 * @return a ModelAndView to render the response
            	 */
                public ModelAndView handleRequest&#40;HttpServletRequest request, HttpServletResponse response&#41;
                        throws Exception &#123;	
            
                    String now = &#40;new java.util.Date&#40;&#41;&#41;.toString&#40;&#41;;
                    logger.info&#40;"returning index view with " + now&#41;;
            
                    return new ModelAndView&#40;"indexView", "mydatenow", now&#41;;
                &#125;
            &#125;
            my main.jsp page
            Code:
            <%@ include file="/WEB-INF/includes/head.jsp" %>
            
            <tiles&#58;insert attribute="menu_left"/>
            <tiles&#58;insert attribute="menu_right"/>
            <tiles&#58;insert attribute="content"/>
            				
            <%@ include file="/WEB-INF/includes/foot.jsp" %>
            head.jsp
            Code:
            <%@ page session="false"%>
            <%@ taglib prefix="fmt"     uri="http&#58;//java.sun.com/jstl/fmt" %>
            <%@ taglib prefix="c" 		uri="http&#58;//java.sun.com/jstl/core_rt" %>
            <%@ taglib prefix="tiles"   uri="http&#58;//jakarta.apache.org/struts/tags-tiles" %>
            <%@ taglib prefix="spring"  uri="http&#58;//www.springframework.org/tags" %>
            <?xml version="1.0"?>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http&#58;//www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
            <html xmlns="http&#58;//www.w3.org/1999/xhtml" xml&#58;lang="fr-FR">
            <head>
            	<title><fmt&#58;message key="title"/></title>
            	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            	<meta http-equiv="content-language" content="fr-FR" />
            	<link rel="stylesheet" type="text/css" href="css/infra.css" media="screen" title="Graphic" />
            </head>
            
            <body>
            so when i did http://localhost:8080/infratiles/indexView.html
            i hoped to see my complete page but i just had the header and footer.

            the problem seems to come from the definitions.xml file.

            indexView seems to not call the controller com.fimasys.infra.web.IndexController ; and the log i make in the controller is not executing.

            can someone tell me what's wrong ?

            regards.

            Comment


            • #7
              currently i'm unable to say if the problem come from my tiles definitions files which will be not correctly formed, :?: or if this come from tiles itself which is unable to display all the part of my page ; but just the header and footer but not the menu nor the main body :cry:

              if someone could help me to see the light once for ever.:roll:

              regards.

              Comment


              • #8
                Once again i solve my problem alone

                so the problem was that main.jsp was unable to include any content / menu with <tiles:insert ...

                why ?

                because i just put
                Code:
                <%@ page session="false"%>
                <%@ taglib prefix="fmt"     uri="http&#58;//java.sun.com/jstl/fmt" %>
                <%@ taglib prefix="c" 		uri="http&#58;//java.sun.com/jstl/core_rt" %>
                <%@ taglib prefix="tiles"   uri="http&#58;//jakarta.apache.org/struts/tags-tiles" %>
                <%@ taglib prefix="spring"  uri="http&#58;//www.springframework.org/tags" %>
                in the head.jsp and not in each jsp page i need to insert throw the <tiles:insert

                Thanks to Valentin for being Patient with me

                Comment


                • #9
                  Spring 3 and tiles 2.2

                  Hi,

                  I am a new programmer in Spring MVC and tiles. In my project earlier spring 2.5 and Struts-tiles were used. Now I am assigned a task of upgrading this code to Spring 3 and Tiles 2.2.0. I am extending SimpleFormController in my controllers and overriding protected Object formBackingObject (HttpServletRequest request) throws Exception.

                  In this method I was using ComponentDefinition cd = TilesUtil.getDefinition("editWorker", request,request.getSession().getServletContext());

                  to create definitions.

                  Now I am not able to figure out how to use this method in Spring 3 and how to create definitions.

                  Can anybody please tell me any book where from I can have knowledge regarding Spring 3 and tiles 2.2.x.

                  Thanks in advance.

                  Comment

                  Working...
                  X