博客
关于我
Struts2学习之旅二 tiles布局和权限管理
阅读量:456 次
发布时间: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/

你可能感兴趣的文章
Nacos做配置中心使用
查看>>
Nacos入门过程的坑--获取不到配置的值
查看>>
Nacos原理
查看>>
Nacos发布0.5.0版本,轻松玩转动态 DNS 服务
查看>>
Nacos启动异常
查看>>
Nacos命名空间配置_每个人用各自自己的命名空间---SpringCloud Alibaba_若依微服务框架改造---工作笔记001
查看>>
Nacos和Zookeeper对比
查看>>
Nacos在双击startup.cmd启动时提示:Unable to start embedded Tomcat
查看>>
Nacos基础版 从入门到精通
查看>>
Nacos如何实现Raft算法与Raft协议原理详解
查看>>
Nacos安装教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
Nacos实战攻略:从入门到精通,全面掌握服务治理与配置管理!(上)
查看>>
Nacos实战攻略:从入门到精通,全面掌握服务治理与配置管理!(下)
查看>>
Nacos心跳机制实现快速上下线
查看>>
nacos报错com.alibaba.nacos.shaded.io.grpc.StatusRuntimeException: UNAVAILABLE: io exception
查看>>
nacos服务提供和发现及客户端负载均衡配置
查看>>
Nacos服务注册与发现demo
查看>>
Nacos服务注册与发现的2种实现方法!
查看>>
nacos服务注册和发现原理简单实现案例
查看>>
Nacos服务注册总流程(源码分析)
查看>>