Spring MVC Apache tiles:输出中缺少页脚

时间:2016-01-24 07:14:34

标签: spring spring-mvc apache-tiles

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

**** 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中的T​​iles依赖

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

1 个答案:

答案 0 :(得分:1)

你的HTML错了!它在“main-content(<body>)”中有一个结束<html>index.jsp标记!

您有自己的模板classic.jsp,并在index.jsp占位符中加入了<tiles:insertAttribute name="body" />。因此生成的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>

您是否注意到您已经在页脚之前使用</body></html>“关闭”了html内容! - 解决方案很简单:从index.jsp中删除所有标题,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.UrlBasedViewResolver TilesViewviewClass属性配置(或使用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>