首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

筹建Struts2 与 Tiles 框架整合示例

2012-09-11 
搭建Struts2 与 Tiles 框架整合示例搭建Struts2 与 Tiles 框架整合示例??环境:MyEclipse 8.5 + TOMCAT6 +J

搭建Struts2 与 Tiles 框架整合示例

搭建Struts2 与 Tiles 框架整合示例

?

?

环境:MyEclipse 8.5 + TOMCAT6 +JDK1.6
一、新建web工程。例如:s2tile2。
二、使用向导添加Struts2.1功能:
引入Struts 2 Core Libraries和Struts 2 Tiles Libraries两个类库包。

三、新建s2tile2\WebRoot\index.jsp文件,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <body>
??? <a href="go.action">test</a>
? </body>
</html>


四、修改s2tile2\WebRoot\WEB-INF\web.xml文件,内容如下:
<?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">
?<welcome-file-list>
??<welcome-file>index.jsp</welcome-file>
?</welcome-file-list>

?

?<listener>
??<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
?</listener>
?<context-param>
??<param-name>org.apache.tiles.CONTAINER_FACTORY</param-name>
??<param-value>
????? org.apache.struts2.tiles.StrutsTilesContainerFactory
??? </param-value>
?</context-param>

?<context-param>
??<param-name>
???org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
??<param-value>/WEB-INF/tiles.xml</param-value>
?</context-param>

?
?<filter>
??<filter-name>struts2</filter-name>
??<filter-class>
???org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
?</filter>
?<filter-mapping>
??<filter-name>struts2</filter-name>
??<url-pattern>*.action</url-pattern>
?</filter-mapping>
</web-app>

五、修改s2tile2\src\struts.xml,内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
?<package name="default" extends="tiles-default">
??<action name="go" type="tiles">myapp.homepage</result>
??</action>
?</package>
</struts>???

六、建立s2tile2\src\com\action\MyAction.java类,内容如下:
package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport{
?public String execute() throws Exception {
??super.execute();
??System.out.println("MyAction execute something ...");
??return SUCCESS;
?}
}

七、新建s2tile2\WebRoot\WEB-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="myapp.homepage" template="layout.jsp">
??<put-attribute name="title" value="Tiles tutorial homepage" />
??<put-attribute name="header" value="/tiles/header.jsp" />
??<put-attribute name="menu" value="/tiles/menu.jsp" />
??<put-attribute name="body" value="/tiles/cBody.jsp" />
??<put-attribute name="footer" value="/tiles/footer.jsp" />
?</definition>
</tiles-definitions>

八、从Struts 2 Tiles Libraries中的tiles-jsp-2.0.6.jar包中的META-INF\tld中的tiles-jsp.tld文件释放到s2tile2\WebRoot\WEB-INF\tiles-jsp.tld目录下。

九、新建s2tile2\WebRoot\layout.jsp文件,内容如下:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/WEB-INF/tiles-jsp.tld" prefix="tiles"%>
<html>
??? <head>
?????? <title></title>
??? </head>
??? <body>
?????? <table width="768px" height="800px" border="2" align="center">
?????????? <tr>
????????????? <td colspan="2" align="center" valign="top" width="768px" height="100px" bgcolor="#80ff80">
????????????????? <tiles:insertAttribute name="header" />
????????????? </td>
?????????? </tr>
?????????? <tr>
????????????? <td align="center" width="150px" height="800px" bgcolor="#00ff00">
????????????????? <tiles:insertAttribute name="menu" />
????????????? </td>
????????????? <td align="right" width="618px" height="800px" bgcolor="#ff80c0">
????????????????? <tiles:insertAttribute name="body" />
????????????? </td>
?????????? </tr>
?????????? <tr>
????????????? <td colspan="2" bgcolor="#00ff40" height="100px">
????????????????? <tiles:insertAttribute name="footer" />
????????????? </td>
?????????? </tr>
?????? </table>
??? </body>
</html>

?

标签说明:

1、<tiles:insertAttribute name="body" />:返回"body"对应的页面的内容信息。

2、<tiles:getAsString name="body" />:返回"body"对应的值,即路径信息。

?

十、依次新建下列文件:
s2tile2\WebRoot\tiles\cBody.jsp、
s2tile2\WebRoot\tiles\footer.jsp、
s2tile2\WebRoot\tiles\header.jsp、
s2tile2\WebRoot\tiles\menu.jsp,
以上文件内容任意,仅用于测试。

十一、发布测试:OK。

?

热点排行