SSH笔记五 整合Tiles2

SSH笔记五 整合Tiles2
           SSH笔记目录

          SSH笔记一  用maven构建项目(源码下载ssim1.rar)
        SSH笔记二  整合hibernate和spring(源码下载ssim2-maven.rar
        SSH笔记三  反向生成DAO  优化开发目录(源码下载ssim3-dao.rar
        SSH笔记四  整合struts2(源码下载ssim4-struts2.rar
         SSH笔记五  整合Tiles2(源码下载ssim5-tiles2.rar

      
  今天的任务是添加Tiles2的支持,完成之后效果如下:
        直接开始今天的内容了
        1.在pom.xml中添加Tiles2的依赖
< dependency >
            
< groupId > org.apache.struts </ groupId >  
            
< artifactId > struts2-tiles-plugin </ artifactId >  
            
< version > 2.1.6 </ version >  
        
</ dependency >
        2.添加Tiles2的配置文件tiles-def.xml,并添加tiles-jsp.tld文件(找到对应版本)(注意:DOCTYPE tiles-definitions PUBLIC 声明要注意版本,比如Tiles包是2.1的要申明为2.1,这里我们用的Tiles2.0.x,所以申明用2.0的配置文件

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

  
< definition  name ="welcome"  template ="/template/layout.jsp" >
     
< put-attribute  name ="title"  value ="Welcome" />
     
< put-attribute  name ="head"  value ="/template/head.jsp" />
     
< put-attribute  name ="content"  value ="/template/content.jsp" />
     
< put-attribute  name ="foot"  value ="/template/foot.jsp" />
  
</ definition >
</ tiles-definitions >
        3.创建Tiles模版以及需要的内容页
layout.jsp

<% @ page language = " java "  contentType = " text/html; charset=UTF-8 "
    pageEncoding
= " UTF-8 " %>
<% @ taglib uri = " /WEB-INF/tiles-jsp.tld "  prefix = " tiles "   %>
<% @ taglib uri = " /struts-tags "  prefix = " s "   %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< HTML >
< HEAD >
< title >< tiles:insertAttribute  name ="title"   /></ title >
< META  http-equiv =Content-Type  content ="text/html; charset=utf-8" >
</ HEAD >
< BODY >
< table >
< tbody >< tr >
< td >< tiles:insertAttribute  name ="head" /></ td ></ tr >
< tr >
< td >< tiles:insertAttribute  name ="content" /></ td ></ tr >
< tr >
< td >< tiles:insertAttribute  name ="foot" /></ td ></ tr >
</ tbody ></ table >
</ BODY >
</ HTML >
head.jsp
<% @ page language = " java "  contentType = " text/html; charset=UTF-8 "
    pageEncoding
= " UTF-8 " %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd" >
this is head
content.jsp
<% @ page language = " java "  contentType = " text/html; charset=UTF-8 "
    pageEncoding
= " UTF-8 " %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd" >

< table  height ="420px" >
< tr >< td > this is body </ td >
</ tr >
</ table >
foot.jsp
<% @ page language = " java "  contentType = " text/html; charset=UTF-8 "
    pageEncoding
= " UTF-8 " %>

this is foot
        4.修改struts.xml文件,使用Tiles解析
user.xml
<? xml version="1.0" encoding="UTF-8"  ?>
<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>

< struts >  
    
< package  name ="user"  namespace ="/user"  extends ="tiles-default" >     
        
< action  name ="login"  class ="userAction"  method ="login" >
            
< result  name ="input" > /login.jsp </ result >
            
< result  name ="success"  type ="tiles" > welcome </ result >
        
</ action >  
    
</ package >  
</ struts >
        Tiles的支持到此完成。

        这个系列到此也基本结束,后面有根据时间可能会加上随机码验证,以及MD5编译密码或者Fckeditor录入文章等常用内容,谢谢关注



你可能感兴趣的:(SSH笔记五 整合Tiles2)