Struts与Apache Tiles结合实现像HTML一样的Frame框架

来源:互联网 发布:java 反射 私有属性 编辑:程序博客网 时间:2024/05/17 23:10

在费用系统项目中:利用Apache Tiles刚开始一头雾水,压根找不到项目怎么跳转,怎么执行相对应的方法;

借鉴这边文章:

本篇文章主要介绍了"结合struts实现像html一样的fram框架",主要涉及到结合struts实现像html一样的fram框架方面的内容,对于结合struts实现像html一样的fram框架感兴趣的同学可以参考一下。
 1、首先有个登陆页面,

<s:form action="check" namespace="/login" method="post" theme="simple">  <table><tr>           <td class="lboxleft">用户名:</td>           <td class="lboxright"><input type="text" name="loginName" id="loginName" class="inputtext" /></td>        </tr><tr><td> </td></tr>        <tr>           <td class="lboxleft">密码:</td>           <td class="lboxright"><input type="password" maxlength="20" name="password" id="password" class="inputtext" size="22" /></td>        </tr><tr>           <td class="lboxleft"></td>           <td class="lboxright">              <table width="100%" border="0" cellspacing="0" cellpadding="0">                   <tr>                       <td>                          <input  type="submit" id="btn_login" value="登录系统"   />                                      </td>                    </tr>                     </table>           </td>         </tr>  </table>                          </div>       </td>               </table></s:form>

2、将信息提交到check的antion进行用户名和密码的判断,如果存在就进入框架,如果不存在就返回登陆页面。
用户存在的情况下需要:
super.getSession().setAttribute("MAIL_LOGIN_USER", user);
super.getSession().setAttribute("MAIL_LOGIN_ROLE", 0);
向session中放入user和和role两个数据,在后面要用到。

3、登陆成功后会跳转到一个页面,而这个页面也没有做什么处理,只是一个页面的再次跳转,   
<script type="text/javascript">
function redirect() {
parent.window.location.href="${ctx}/frame.action";
}
</script>
  </head>
  <body onload="redirect();">
  </body>
</html>
不这样做也可以,可以直接在struts中直接进行跳转。

注:${ctx}是引入了一个自定义标签<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>只是不想每次都写很长的url

4、现在进入frame.action中看看有什么,

<package name="default" extends="tiles-default" namespace="/"><action name="frame" class="frameAction" method="frame"><!-- member.layout --><result name="success" type="tiles">mail.layout</result></action><action name="frameHeader" class="frameAction" method="frameHeader"><result type="tiles">mail.frameHeader</result></action><action name="frameBottom"><result type="tiles">mail.frameBottom</result></action></package>
首先进入feameAction中而这个antion中也就做一些头部时间,公告之类的初始化工作,当返回成功后就会去找mail.layout,这儿快可能会迷茫,这个mail.layout是什么

5、mail.layout
在WEB-INF下新建文件夹tiles,在titles下新建tiles.xml

<?xml version="1.0" encoding="UTF-8"?>   <!DOCTYPE tiles-definitions PUBLIC        "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"        "<DTDURL>/tiles-config_2_0.dtd"><tiles-definitions>   <!--页面标题框架-->   <definition name="mail.layout" template="/pages/layout/frame.jsp"><!--t跳转到html的frame框架页面上-->        <put-attribute name = "title" value="MailContral V1.0"/>        这里配置每个action        <put-attribute name="topFrame" value="/mail/frameHeader.action"/><put-attribute name="leftFrame0" value="/mail/pages/fileAndProcess/manageMenu.jsp"/><put-attribute name="mainFrame0" value="/mail/fct/queryall.action"/><put-attribute name="leftFrame1" value="/mail/pages/atrategyMange/mailMenu.jsp"/><put-attribute name="mainFrame1" value="/mail/mailtreaty/query.action"/><put-attribute name="bottomFrame" value="/mail/frameBottom.action"/>     </definition>        <definition name = "mail.frameHeader" template="/pages/layout/top.jsp"/>    <definition name = "mail.frameLeft" template="/pages/layout/left.jsp"/>    <definition name = "mail.frameBottom" template="/pages/layout/footer.jsp"/> </tiles-definitions>
在web.xml中注册tiles.xml

<context-param><param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name><param-value>/WEB-INF/tiles/tiles.xml</param-value></context-param>
6、frame.jsp

frame.jsp中的框架代码为:

</head><frameset rows="105,*,40" frameborder="no" border="0" framespacing="0">  <frame src="<tiles:getAsString name="topFrame"/>" name="topFrame" id="topFrame" frameborder="0" scrolling="No" noresize="noresize" id="topFrame" title="topFrame">  <frameset cols="174,0,*" frameborder="no" border="0" framespacing="0"><frame src="<tiles:getAsString name="leftFrame${sessionScope.MAIL_LOGIN_ROLE}"/>" name="leftFrame" id="leftFrame" frameborder="0" noresize title="leftFrame" ><frame src="" frameborder="0" name="mapFrame" scrolling="NO" noresize title="mapFrame" ><frame src="<tiles:getAsString name="mainFrame${sessionScope.MAIL_LOGIN_ROLE}" />" name="mainFrame" id="mainFrame" frameborder="0" id="mainFrame" title="mainFrame">  </frameset>  <frame src="<tiles:getAsString name="bottomFrame"/>" name="bottomFrame" id="bottomFrame" frameborder="0" scrolling="No" noresize="noresize" id="bottomFrame" title="bottomFrame"></frameset><noframes><body onload="load()"></body></noframes></html>
写到这里可以解释下前面那个问题了,前面loginAction中 用户存在的情况下需要
super.getSession().setAttribute("MAIL_LOGIN_USER", user);
super.getSession().setAttribute("MAIL_LOGIN_ROLE", 0);
向session中放入user和和role两个数据,在后面要用到。

这个MAIL_LOGIN_ROLE就这这里用到了,在tiles.xml中配置每个antion的时候你可能发现了put-attribute 的name为leftFrame0 leftFrame1,还有在frame.jsp中的<tiles:getAsString name="leftFrame${sessionScope.MAIL_LOGIN_ROLE}"/>,这下应该明白了吧,这样做的目的就是在用户登陆的时候给用户展示的首页面是什么样的。

浏览器对fram.jsp进行解析,解析到<tiles:getAsString name="topFrame"/>"的时候就去tiles.xml中找到这个topFrame,解析到"<tiles:getAsString name="leftFrame${sessionScope.MAIL_LOGIN_ROLE}"就到tiles.xml中找这个frame。

7、top.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 1.01 Transitional//EN"><%@ taglib prefix="s" uri="/struts-tags" %><%@ page contentType="text/html; charset=utf-8" %><%@ include file="/pages/commons/meta.jsp"%><%@ include file="/pages/commons/taglibs.jsp"%><html><head><link href="${ctx}/css/css.css" rel="stylesheet" type="text/css"><link href="${ctx}/css/top.css" rel="stylesheet" type="text/css"><link rel="stylesheet" href="${ctx}/css/css.css" type="text/css"/> <script type='text/javascript' src="${ctx}/script/jquery-1.4.4.min.js"></script><script type='text/javascript' src="${ctx}/script/util.js"></script><script language="javascript">function linkTo(linkType) {if(linkType=='file'){ parent.leftFrame.location.href='${ctx}/pages/fileAndProcess/manageMenu.jsp';//页面左侧当行栏 parent.mainFrame.location.href='${ctx}/fct/queryall.action';//主页面}if(linkType=='mailContral'){ parent.leftFrame.location.href='${ctx}/pages/mailManage/mailMenu.jsp'; parent.mainFrame.location.href='${ctx}/mailtreaty/query.action';}if(linkType=='report'){ parent.leftFrame.location.href='${ctx}/pages/reportMail/reportMenu.jsp'; parent.mainFrame.location.href='${ctx}/reportByTime/queryAllPc.action';}}</script></head><body bgcolor="#C7E4DA"><center><!-- banner area --><table width="100%" height="70px" border="0" cellpadding="0" cellspacing="0" background="${ctx}/imgs/topRbj.jpg" > <tr>   <td>       <table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%">         <tr>                       <td><table width="100%" border="0" cellspacing="0" cellpadding="0">             <tr>               <td valign="top" align="left" width="60%"><div style="font-family:'微软雅黑','黑体','宋体'; color:#FFFFFF; padding-top:5px; padding-left:20px; font-size: 45px; "><h1>邮件过滤系统</h1></div></td>               <td align="right" width="40%" valign="top">   <div class="topinfo">               <table width="100%" border="0" cellpadding="0" cellspacing="0">                         <tr>                           <td class="topL1">                               <table width="70%" border="0" align="right" cellpadding="0" cellspacing="0">                                 <tr>                                   <td align="left">帐  号:admin</td>                                 </tr>                                 <tr>                                   <td align="left" title="${sessionScope.FORT_LOGON_ROLE.roleNameStr}">角  色:管理员</td>                                 </tr>                               </table>                                                   </td>                         <td>                          <input name="" type="button" class="topL2" value="个人设置" />                          </td>                            <td height="30"><input name="input" type="button" class="topL3" value="注销" /></td>                          </tr>                     </table>                 </div>                                 </td>             </tr>           </table></td>           <td width="1%"> </td>         </tr>       </table></td> </tr></table><!-- banner area --><div class="menulist" style="background:url(${ctx}/imgs/dhBj.jpg);repeat-x left;  height:30px; color:#FFFFFF; text-align:left; width:100%;  padding-top: 6px;padding-left: 10px; margin-top:-2px; font-size:18px;">    <a href="javascript:linkTo('file')"  style="color:#FFFFFF;">文件</a><span>|</span>    <a href="javascript:linkTo('mailContral')"   style="color:#FFFFFF;" >邮件</a><span>|</span>    <a href="javascript:linkTo('report')"    style="color:#FFFFFF;">报表</a></div></center></body></html>
8、页面左侧导航栏

部分代码:<ul>
<li id="subnavlist"><a href="${ctx}/mailtreaty/query.action"
target="mainFrame"><img src="${ctx}/imgs/jiantou1.jpg" />&nbsp;数据规则</a>
</li>
</ul>

9、到目前为止已经实现了框架了,还有个小问题就是在session过期后跳出框架的问题,注销的时候首先要去清空session中的值。