zoukankan      html  css  js  c++  java
  • Spring中整合Titles

     在《Spriing实战(第三版)》这本书中,有一个使用titles的例子,但是这是一个不完整的例子。那么要参照起来就比较难了,于是找到了下面这篇博客。

    在Spring中使用tiles2 (因为是英文的,同时又是比较简单的英文,那么就翻译一下,当作学习)

    在这个例子中,你将学会怎样整合Spring和Tiles2.这个例子的目录结构如下:

    添加下面的库文件到库目录,(当然如果是在Eclipse中就是对应的lib文件夹了)。

    01.antlr-runtime-3.0
    02.commons-logging-1.0.4
    03.org.springframework.asm-3.0.0.M3
    04.org.springframework.beans-3.0.0.M3
    05.org.springframework.context-3.0.0.M3
    06.org.springframework.context.support-3.0.0.M3
    07.org.springframework.core-3.0.0.M3
    08.org.springframework.expression-3.0.0.M3
    09.org.springframework.web-3.0.0.M3
    10.org.springframework.web.servlet-3.0.0.M3
    11.
    12.commons-beanutils-1.7.0
    13.commons-digester-1.8
    14.commons-logging-api-1.1
    15.jstl
    16.standard
    17.tiles-api-2.0.4
    18.tiles-core-2.0.4
    19.tiles-jsp-2.0.4
    你将会看到如何创建一个有头部,目录和主体部分的简单典型的Tiles布局。
    在Spring中使用Tiles,在Spring的配置文件中配置下面Tile的定义。
    <?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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
        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.xsd">
         
        <bean id="viewResolver" class="org.springframework.web.servlet.view. ResourceBundleViewResolver" p:basename="views" />
         
        <context:component-scan base-package="com.vaannila.web" />
       
        <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2. TilesConfigurer" p:definitions="/WEB-INF/tiles-defs.xml" />    
             
    </beans>
     使用definitions属性指定Tiles定义文件的位子,这里这个位置是“/WEB-INF/tiles-defs.xml"。Tiles定义文件展示如下:
    <?xml version="1.0" encoding="UTF-8" ?>
     
    <!DOCTYPE tiles-definitions PUBLIC
          "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
           "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
     
    <tiles-definitions>
     
      <definition name="baseLayout" template="/WEB-INF/tiles/baseLayout.jsp">
          <put-attribute name="title"  value="Template"/>
          <put-attribute name="header" value="/WEB-INF/tiles/header.jsp"/>
          <put-attribute name="menu"   value="/WEB-INF/tiles/menu.jsp"/>
          <put-attribute name="body"   value="/WEB-INF/tiles/body.jsp"/>
          <put-attribute name="footer"   value="/WEB-INF/tiles/footer.jsp"/>
      </definition>
       
      <definition name="welcome" extends="baseLayout">
          <put-attribute name="title"  value="Welcome"/>
          <put-attribute name="body"   value="/WEB-INF/jsp/welcome.jsp"/>      
      </definition>
    
      <definition name="friends" extends="baseLayout">
          <put-attribute name="title"  value="Friends"/>
          <put-attribute name="body"   value="/WEB-INF/jsp/friends.jsp"/>      
      </definition>
       
      <definition name="office" extends="baseLayout">
          <put-attribute name="title"  value="Office"/>
          <put-attribute name="body"   value="/WEB-INF/jsp/office.jsp"/>      
      </definition>
       
    </tiles-definitions>
     这里我们首先定义了基本的布局,以后我们将扩展这个基本的布局并且将通过仅仅改变标题和主体部分创建更多tiles。
     
    为了显示视图我们使用ResourceBundleViewResolver。通过定义存储了一对关键值的views.properties文件,我们使用了的基本名属性指定这些。
    welcome.(class)=org.springframework.web.servlet.view.tiles2.TilesView
    welcome.url=welcome
    
    friends.(class)=org.springframework.web.servlet.view.tiles2.TilesView
    friends.url=friends
    
    office.(class)=org.springframework.web.servlet.view.tiles2.TilesView
    office.url=office
     
    about.(class)=org.springframework.web.servlet.view.JstlView
    about.url=/WEB-INF/jsp/about.jsp

     welcome, friends 和 office 引用tile 定义的名字 (the one to right side of the = sign)。我们使用 "org.springframework.web.servlet.view.tiles2. TilesView" 类展示tile.你也可以一起使用其他的TilesView。相关的url通过org.springframework.web.servlet.view.JstlView被映射到相关的jsp页面。

    baseLayout.jsp文件包含了拥有不同区域的的表结构。

    <%@ 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" ignore="true" /></title>
    </head>
    <body>
    <table border="1" cellpadding="2" cellspacing="2" align="center">
    <tr>
    <td height="30" colspan="2">
    <tiles:insertAttribute name="header" />
    </td>
    </tr>
    <tr>
    <td height="250">
    <tiles:insertAttribute name="menu" />
    </td>
    <td width="350">
    <tiles:insertAttribute name="body" />
    </td>
    </tr>
    <tr>
    <td height="30" colspan="2">
    <tiles:insertAttribute name="footer" />
    </td>
    </tr>
    </table>
    </body>
    </html>

     这里我们使用注解controller处理映射去处理请求。在在redirect.jsp页面我们只是请求welcome.htm。

    <% response.sendRedirect("welcome.htm"); %>

    我们向前到 welcome.htm的url将通过WelcomeController类处理。

    package com.vaannila.web;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class WelcomeController {
    
    @RequestMapping("/welcome.htm")
    public String redirect()
    {
    return "welcome";
    }
    }
    

    运行时显示的界面如下:


  • 相关阅读:
    快学Scala 第6章 对象
    Ch05 类
    Ch04 映射和元组
    Ch03 数组相关操作
    Ch02 控制结构和函数
    Ch01 基础
    28.创建对象两种方式、对象的方法、属性、获取属性值的两种方法、属性名含特殊符号时获取属性值的方法、添加属性及值的方法、删除属性的方法、使用方式、遍历对象属性for(var prop in obj)
    27.函数循环扩展作业、非常重要涉及编程思想(一天看一遍) 编程思想其实就是算法,、感悟、演算
    26 .闭包 、函数表达式在for中无法接受实时改变的变量、 在for里要将函数表达式封装成闭包才能接受实时变化的参数,并要将函数表达式return出去
    25.函数例题-预解析 作用域 、函数变量优先级、全局变量污染(直接在全局声明 、 函数里的变量没声明)但是函数里的变量没声明造成的全局变量污染有个前提,函数要被调用)
  • 原文地址:https://www.cnblogs.com/sand-tiny/p/3884919.html
Copyright © 2011-2022 走看看