为什么Tiles 3.0.5在Spring Web 4.1.5上不起作用

时间:2015-02-26 03:12:23

标签: spring-mvc tiles tiles-3

我将基于Spring的应用程序(4.1.5)配置为使用Tiles 3.0.5。 在tiles.xml文件中,我放置了一个不存在的文件“ewfsdfsdf.jsp”以查看配置是否有效,然后我导航到hello.htm但它显示了hello.jsp而不是显示有关未找到ewfsdfsdf.jsp文件的任何错误

的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 个答案:

答案 0 :(得分:1)

我可以通过以下tutorial并将以下行添加到springapp-servlet.xml和两个新jar来解决问题

<强>罐

jsf-api.jar
jstl.jar

添加了新的viewResolver

<bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver"/>