Spring MVC Apache 磁贴:输出中缺少页脚

Spring MVC Apache tiles: footer is missing from output

我现在在 Spring mvc 中尝试学习 apche tiles...但它不起作用...不知道为什么?

****servlet-context.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"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/mvc       
 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 http://www.springframework.org/schema/beans   
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-4.0.xsd">




<mvc:annotation-driven />



<bean   
  class="org.springframework.web.servlet.view.
     InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<context:component-scan base-package="com.mymvc.myapp" />

<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>


</beans>

pom.xml

中的磁贴依赖项
<dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-core</artifactId>
        <version>${apache.tiles}</version>
</dependency>

<dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>${apache.tiles}</version>
</dependency>

<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.6</version>
</dependency>

general.xml

  <?xml version="1.0" encoding="ISO-8859-1" ?>
  <!DOCTYPE tiles-definitions PUBLIC
   "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
   "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
  <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="Java Blog Aggregator" />
    <put-attribute name="body" value="/WEB-INF/views/index.jsp" />
 </definition>
 </tiles-definitions>

classic.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <title><tiles:getAsString name="title"/></title>
 </head>
  <body>
     <tiles:insertAttribute name="body" />
     <br>
     <br>
     <center>
            <tiles:insertAttribute name="footer" />
     </center>
  </body>
 </html>

footer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

   &copy;IShmam Shahriar

IndexController.java

package com.mymvc.myapp;

 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;

 @Controller
 public class IndexController {
      @RequestMapping("/index")
       public String index(){
           return "index";
       }
  }

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
 <!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=ISO- 
      8859-1">
      <title>Insert title here</title>
  </head>
  <body>
       <p>index page</p>
   </body>
 </html>

你的HTML错了!它在你 "main-content (index.jsp)"!

中有一个结束 <body><html> 标签

您有自己的模板:classic.jsp 并在 <tiles:insertAttribute name="body" /> 占位符中包含 index.jsp。所以结果 HTML 看起来像,(完整的 index.jsp 将插入占位符!):

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>...</title>
   </head>
   <body>
<html>                            <!-- problem starts her -->
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO8859-1">
      <title>Insert title here</title>
  </head>
  <body>
       <p>index page</p>
   </body>                        <!-- that is the reason for not -->
 </html>                          <!-- having a footer -->
         <br>
        <br>
         <center>
            &copy;IShmam Shahriar
         </center>
     </body>
 </html>

您是否注意到 "close" html 内容 </body></html> 已经在页脚之前了! -- 解决方案很简单:从 index.jsp:

中删除所有 header、html 和 body 标签
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"   "http://www.w3.org/TR/html4/loose.dtd">

   <p>index page</p>

另外一点:

从配置中删除您的 InternalResourceViewResolver。问题是您有两个视图解析器(内部和 Url/Tiles),您的视图名称(return "index")很可能由内部视图解析器解析,而不是由 Url/Tiles 解析器解析.

根据Spring Reference, Chapter 18.3.2 Integration Tiles,您只需要:

  • org.springframework.web.servlet.view.tiles3.TilesConfigurer
  • org.springframework.web.servlet.view.UrlBasedViewResolverviewClass 配置 TilesView 属性(或使用 org.springframework.web.servlet.view.tiles3.TilesViewResolver - 这是 UrlBasedViewResolver 的已配置子类)
    • 你可以有一个org.springframework.web.servlet.view.ResourceBundleViewResolver

示例:

<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 class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
</bean>