博客
关于我
Struts2学习之旅二 tiles布局和权限管理
阅读量:443 次
发布时间:2019-03-06

本文共 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/

你可能感兴趣的文章
ASP.NET MVC Action Filters
查看>>
Windows SharePoint Services 3.0 Service Pack 2
查看>>
CodePlex站点的Wiki引擎已开源
查看>>
兰州大学百年校庆--风雨百年萃英路
查看>>
Eucalyptus企业云计算
查看>>
Service Broker 无法工作的问题修复
查看>>
Windows Server 2008 R2 Server Core
查看>>
WCF WebHttp Services in .NET 4
查看>>
使用Sysinternals工具定时休眠Windows Server 2008 R2
查看>>
ASP.NET MVC的Action Filter
查看>>
MonoMac 1.0正式发布
查看>>
Shawn Wildermuth的《Architecting WP7 》系列文章
查看>>
2011 ASP.NET/IIS MVP
查看>>
[腾讯社区开放平台].NET SDK基于New BSD协议开源
查看>>
使用Topshelf 5步创建Windows 服务
查看>>
Powershell中禁止执行脚本解决办法
查看>>
Caliburn Micro for Windows Phone 7
查看>>
私有代码存放仓库 BitBucket
查看>>
Redis Web界面管理工具
查看>>
用Sysctl 调整Linux操作系统的性能
查看>>