struts1整合tiles详解+demo
1.用myeclipse建一个web工程
?
2.导入struts1.2的框架模板
?
3.创建所需要的java类和jsp文件及tiles-defs.xml
?
4.配置struts-config.xml的plug-in
?
工程图如下:
????
?
5.LogonAction.java
?
package com.cenwave.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
public class LogonAction extends DispatchAction
{
?public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,
???????? HttpServletResponse response)
?{
??DynaActionForm loginActionForm=(DynaActionForm)form;
??String username=(String)loginActionForm.get("username");
??String password=(String)loginActionForm.get("userpass");
??if("test".equals(username)&&"test".equals(password))
??{
??????????? request.setAttribute("username",username);??
???return mapping.findForward("show");
??}
??return null;
?}
}
?
6.basePage.jsp
?
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<html>
?<head>
??<tiles:useAttribute name="title" />
??<title><bean:write name="title" /></title>
?</head>
?<body>
??<div align="center">
???<table border="1" align="center">
????<tr>
?????<td? colspan="2" align="center" valign="top" width="500px" height="100px" bgcolor="#666666">
??????<tiles:insert attribute="header" />
?????</td>
????</tr>
????<tr>
?????<td? align="left" width="100px" height="300px" bgcolor="#ff80c0">
??????<!-- 系统菜-->
??????<tiles:insert attribute="menu" />
?????</td>
?????<td align="right"? width="400px" height="300px" bgcolor="#00ff00">
??????<!-- 面具体的内容-->
??????<tiles:useAttribute name="content" />
??????<tiles:insert attribute="content" />
?????</td>
????</tr>
????<tr>
?????<td? align="center" colspan="2" bgcolor="#FF0066" height="100px">
??????<!-- copyright内容-->
??????<tiles:insert attribute="footer" />
?????</td>
????</tr>
???</table>
??</div>
?</body>
</html>
?
7.footer.jsp? &&? menu.jsp? &&? header.jsp
?
footer.jsp内容:?? <p>footer</p>
?
menu.jsp内容:??
? <div><ul>
?????????? <li><a href="login.do">等一会</a></li>
?????????? </ul>
? </div>
?
header.jsp内容:?? <p>header</p>
?
8.content_1.jsp? &&? content_2.jsp? &&? content_3.jsp
?
content_1.jsp内容:? <center>content_1</center>
?
content_2.jsp内容:
?
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<div>
??? <form action="/s1tiles/logon.do" method="post">
??? ?<table>
??? ??<tr>
??? ????? <td>用户名:</td><td><input type="text" name="username" id="uname"/></td>
??? ??</tr>
??? ??<tr>
??? ????? <td>密? 码:</td><td><input type="password" name="userpass" id="upass"/></td>
??? ??</tr>
??? ??<tr>
??? ????? <td colspan="2"><input type="submit" value="登陆"/></td>
??? ??</tr>
??? ?</table>
??? </form>
</div>
?
content_3.jsp内容:
?
<%@ page language="java" contentType="text/html; charset=UTF-8"?pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<p>
???? 欢迎,热烈欢迎<bean:write name="username" />!<br>
</p>
?
9.struts-config.xml
?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
?<data-sources />
?<form-beans>
??<form-bean name="loginForm"
???type="org.apache.struts.action.DynaActionForm">
???<form-property name="username" type="java.lang.String" />
???<form-property name="userpass" type="java.lang.String" />
??</form-bean>
?</form-beans>
?<global-exceptions />
?<global-forwards />
?<action-mappings>
???? <!-- forward="gologin" gologin 为 definition name="gologin" 定义 -->
??<action forward="gologin" path="/login" />
??<action path="/logon"
????????? input="gologin"
????????? name="loginForm"
????????? scope="request"
????????? type="com.cenwave.action.LogonAction">
????????? <forward name="show" path="logonok" />
?? </action>
?</action-mappings>
?<message-resources parameter="com.cenwave.ApplicationResources" />
?<!-- Tiles plug-in setting -->
?<plug-in className="org.apache.struts.tiles.TilesPlugin">
??<set-property property="definitions-config"
???value="/WEB-INF/tiles-defs.xml" />
??<set-property property="moduleAware" value="true" />
?</plug-in>
</struts-config>
?
10.tiles-def.xml
?
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
?"-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
?"http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
<!-- Definitions for Tiles? -->
<tiles-definitions>
?<!--定义一个复合页面-->
?<definition name="basePage" template="/base/basePage.jsp">
?<!-- <definition name="basePage" path="/base/basePage.jsp"> -->
??<put name="title" value="${title}" />
??<put name="header" value="/common/header.jsp" />
??<put name="menu" value="/common/menu.jsp" />
??<put name="content" value="${content}" />
??<put name="footer" value="/common/footer.jsp" />
?</definition>?
?<!--定义应用的首页面-->
?<definition name="index" extends="basePage">
??<put name="title" value="FistPage" />
??<put name="content" value="/content/content_1.jsp" />
?</definition>
?
?<!--定义登陆页面-->
?<definition name="gologin" extends="basePage">
??<put name="title" value="GoLoginPage" />
??<put name="content" value="/content/content_2.jsp" />
?</definition>
?
?<!--定义登陆成功页面-->
?<definition name="logonok" extends="basePage">
??<put name="title" value="LogonOKPage" />
??<put name="content" value="/content/content_3.jsp" />
?</definition>
</tiles-definitions>
?
最后上传个工程,自己去看效果吧!