Why Tiles 3.0.5 does not work on Spring web 4.1.5

2019-07-23 12:18发布

I configured my Spring based application (4.1.5) to use Tiles 3.0.5. In tiles.xml file I put a file that does not exist "ewfsdfsdf.jsp" to see if the configuration works, then I navigated to hello.htm but it shows hello.jsp rather than showing any error about not finding ewfsdfsdf.jsp file.

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


   <listener>
       <listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-class>
   </listener>
   <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>springapp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springapp</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/springapp-servlet.xml</param-value>
    </context-param>



</web-app>

tiles.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
  "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
  "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">

<tiles-definitions>
    <definition name="baseLayout" template="/baseLayout.jsp">
        <put-attribute name="title" value="Title is here (Tile)"/>

    </definition>

    <definition name="hello" extends="baseLayout">
        <put-attribute name="title" value="HELERE"/>
          <put-attribute name="body" value="ewfsdfsdf.jsp"/>
    </definition>
</tiles-definitions>

baseLayout.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insertAttribute name="title"/></title>
</head>
<body>
      <div id="container">
         <tiles:insertAttribute name="header"/>
         <tiles:insertAttribute name="menu"/>
         <tiles:insertAttribute name="body"/>
         <tiles:insertAttribute name="footer"/>
      </div>
</body>
</html>

springapp-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean name="/hello.htm" class="springapp.web.HelloController" />

<bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>


</beans>

HelloController

package springapp.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloController implements Controller {

    protected final Log logger = LogFactory.getLog(getClass());

    public HelloController(){
        System.err.println("Constructor of HelloController");
    }
    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        logger.info("Returning hello view");

        return new ModelAndView("hello");
    }

}

enter image description here

1条回答
来,给爷笑一个
2楼-- · 2019-07-23 12:40

I could solve the problem by following this tutorial and adding following line to springapp-servlet.xml and two new jars

jars

jsf-api.jar
jstl.jar

new viewResolver added

<bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver"/> 
查看更多
登录 后发表回答