本文共 3787 字,大约阅读时间需要 12 分钟。
做了一个CRUD之后,算是迈出了最简单的第一步,现在我们要做一个高级一点的东西,tiles布局和权限管理,有信心和激情去完成它。
1,tiles是神马?用它可以干什么?为什么要使用它?
tiles是一种模版机制,将网页的内容和布局分离;
用它来减少重复的页面编码;
可灵活的跟JSF,Spring,Struts2框架整合到一起。
2,先看看我使用tiles做的一个后台管理系统的主页的效果,太丑陋了,见笑了;
创建的步骤,来个xmind;
下面列出要点:
步骤 要点 必要的依赖 1,在web.xml中增加一个监听器,配置两个常量; <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/tiles.xml, /WEB-INF/tiles/tiles-adminUser.xml </param-value> </context-param>公共的页面代码 简单点内容页面,比如head.jsp,bottom.jsp,leftBox.jsp,举个例子: <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> <div style="background-color:gray;text-align: center;"> Cutter哥哥后台管理系统 </div>模版代码 主要是定义一种布局样式:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> <%@ taglib uri="/WEB-INF/tld/tiles-jsp.tld" prefix="tiles"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>基本模版</title> <style type="text/css"> body { text-decoration: none; width: 100%; height: 100%; text-align: center; }.head {
width: 100%; height: 50px; background-color: green; }.bottom {
width: 100%; height: 30px; background-color: green; }.leftBox {
width: 100px; height: 600px; background-color: green; clear:both; float: left; }.content {
width: 600px; height: 600px; clear: right; } </style> </head> <body> <div class="head"> <tiles:insertAttribute name="head"/> </div> <div class="leftBox"> <tiles:insertAttribute name="leftBox"/> </div> <div class="content"> <tiles:insertAttribute name="content"/> </div> <div class="bottom"> <tiles:insertAttribute name="bottom"/> </div> </body> </html>组件 组件的模版,tiles支持继承; <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" "> <tiles-definitions> <!--首页--> <definition name="adminDefaultLayout" template="/WEB-INF/web/common/home.jsp"> <!-- <put-attribute name="titleKey" value="PageTitle" type="string"/>--> <put-attribute name="head" value="/WEB-INF/web/common/head.jsp"/> <put-attribute name="leftBox" value="/WEB-INF/web/common/leftBox.jsp"/> <put-attribute name="content" value=""/> <put-attribute name="bottom" value="/WEB-INF/web/common/bottom.jsp"/> </definition> </tiles-definitions>来一个实例组件:使用的时候分模块定义tiles组件;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" "> <tiles-definitions> <definition name="adminUserList" extends="adminDefaultLayout"> <put-attribute name="content" value="/manager/index.jsp" /> </definition> </tiles-definitions>urlmap使用 直接贴一个xml的配置文件,要点标红了,如果没有 tiles-default,会报type为tiles的错误;tiles指向的是组件的名称,组件在应用启动的时候已经加载!
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "> <struts> <package name="user" extends="struts-default,tiles-default" namespace="/account"> <action name="login" class="com.cutter.web.account.action.user.LoginOutAction" method="index" > <result name="success">/manager/adminLogin.jsp</result> </action> <action name="loginSubmit" class="com.cutter.web.account.action.user.LoginOutAction" method="login"> <result name="success" type="tiles">adminUserList</result> <result name="input">/manager/adminLogin.jsp</result> </action> </package> </struts>
先搞基本的,下一个随笔次分析下tiles的执行过程和原理;大家多支持。
3,权限管理的基本思想:
下班了,下周再接着写了,祝各位周末愉快···
贴个搞笑的图来结束苦逼的自学,上进的孩纸需要激情:
转载地址:http://xixfz.baihongyu.com/