stuts2中tiles2配置


 首先,我声明我用的是Struts-2.1.6而不是Struts-2.0.x,2.1和2.0是有些不同的,希望大家注意。下面进行配置步骤:

1、导入包,把Struts-2.1.6文件夹lib里的jar包复制到项目文件WEB-INF/lib里,一共13个:

1)struts2-core-2.1.6.jar

2)xwork-2.1.2.jar

3)freemarker-2.3.13.jar

4)ognl-2.6.11.jar

5)commons-logging-1.0.4.jar

6)commons-fileupload-1.2.1.jar

7)commons-beanutils-1.7.0.jar

8)commons-collections-3.2.jar


9)commons-digester-1.8

10)sruts2-tiles-plugin-2.1.6.jar

11)tiles-api-2.0.6.jar

12)tiles-core-2.0.6.jar

13)tiles-jsp-2.0.6

注意:6、7、8三项是2.1和2.0不同的地方,一定要记得导入,不然刚启动Tomcat就会报错。

2、在web.xml中配置Tiles2框架的监听器,形如下文:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter> 
    <filter-name>struts2 </filter-name> 
     <filter-class> 
      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter //这个过滤器发生变化了,

                                                                                                          //据说这个过滤器对i18n不怎么支持了
     </filter-class> 
</filter>
<filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
     <listener> 
        <listener-class> 
            org.apache.struts2.tiles.StrutsTilesListener 
        </listener-class> 
    </listener> 
    <context-param> //原文中没有加这个,所以运行不成功
       <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
       <param-value>/WEB-INF/tiles.xml</param-value>
    </context-param>


<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

注意:有些老版本2.0的配置中,黑体的字类为 org.apache.struts2.tiles.web.startup.TilesListener,2.1的这个类是2.0那个类的子类。

3、在WEN-INF文件夹下添加tiles.xml文件,形式如下:

<?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="index" template="/siteLayout.jsp">
   <put-attribute name="header" value="/header.jsp"></put-attribute>
   <put-attribute name="content" value="/content.jsp"/> 
        <put-attribute name="footer" value="/footer.jsp"/>
</definition>
<definition name="success" template="/siteLayout.jsp">
   <put-attribute name="header" value="/header.jsp"></put-attribute>
   <put-attribute name="content" value="/success.jsp"/> 
        <put-attribute name="footer" value="/footer.jsp"/>
</definition>
</tiles-definitions> 


PS:原文中为 http://struts.apache.org/dtds/tiles-config_2_0.dtd

注意划线部分的区别,若是原文中这种的话在定义definition的时候就只能用<put>而不是

<put-attribute>

注意:使用过Tiles1框架的朋友也许会犯错,在1里,templante为path,put-attribute为put。

4、配置struts.xml文件,这个文件在src文件夹下是常识,毋需多说:

<?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="tiles" extends="tiles-default" > 
        <action name="login" class="actions.LoginAction" method="login"> 
          <result name="success" type="tiles">success</result>
          <result name="input" type="tiles">index</result> 
        </action>    
    </package> 
</struts> 


 

5、写jsp代码,形式如下:

//index.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>

<tiles:insertTemplate template="/siteLayout.jsp" flush="true">
<tiles:putAttribute name="title" type="string" value="Get Rick Hightower Stock Quote"/>
     <tiles:putAttribute name="header" value="/header.jsp" />
     <tiles:putAttribute name="footer" value="/footer.jsp" />
     <tiles:putAttribute name="content" value="/indexContent.jsp"/>
</tiles:insertTemplate>


//success.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:form action="Lookup"> 
   这是登录成功界面
</s:form>


//siteLayout.jsp布局页面

<%@ page language="java" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<html>

   <head>
     <title>
         <tiles:getAsString name="title" ignore="true"/>
     </title>
   </head>

   <body>
     <table width="500" border="0" cellspacing="0" cellpadding="0">

       <tr bgcolor="#36566E"> 
         <td height="68" width="48%"> 
           <div align="left">
             <img src="images/hp_logo_rickhightower.gif" 
                  width="220" height="74">
           </div>
         </td>
       </tr>

       <tr>
         <td height="68" width="2000">
         <tiles:insertAttribute name="header" ignore="true">
         <tiles:putAttribute name="title"></tiles:putAttribute>
         </tiles:insertAttribute> 
            
          </td>   
       </tr>
       <tr>
         <td>
             <div align="center">
             <tiles:insertAttribute name="content"/>
             </div>
         </td>
       </tr>
       <tr>
         <td>
             <tiles:insertAttribute name="footer" ignore="true"/>
         </td>
       </tr>
       
     </table>
     
   </body>
</html>


//indexContent.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:form action="login"> 
   <s:textfield name="user.username" label="用户名" />
<s:textfield name="user.password" label="输入密码" />
<s:submit value="submit" />
</s:form>


//header.jsp

<div id="header">
<span style="float: right"><a href="#">设为首页</a>|<a href="#">联系我们</a>
</span>
</div>
<div style="width: 936px; height: 200px;" " />


//footer.jsp

<div id="footer">
     <p><a href="#">hei</a> <a href="#">long</a></p>
     <p>2008 © jiang</p>
</div> 


为了简化后台,来个loginAction处理下:

package actions;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

   public String execute(){
      return SUCCESS;
   }

}


 

PS: http://blog.csdn.net/eidolon8/article/details/7004380这里说的很好,例子也更好

你可能感兴趣的:(html,struts,header,action,login,encoding)