struts2利用拦截器和注解进行权限控制 (二) :JSP页面中根据权限显示或隐藏
http://hi.baidu.com/sonmeika/item/a1955a4699327cd3c0a5921e
新建Annotation,Permission.java:
package cn.itcast.action.privilege;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 权限设置
*
*/
@Retention(RetentionPolicy.RUNTIME) // 代表Permission注解保留的阶段
@Target({ElementType.METHOD}) // 只允许标注在方法上
public @interface Permission {
/** 模块 **/
String module();
/** 权限值 **/
String privilege();
}
权限校验标签 PermissionTag.java:
package cn.itcast.web.taglib;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import cn.itcast.bean.product.ProductType;
/**
* 权限校验标签
*
*/
public class PermissionTag extends TagSupport {
private String module;
private String privilege;
public String getModule() {
return module;
}
public void setModule(String module) {
this.module = module;
}
public String getPrivilege() {
return privilege;
}
public void setPrivilege(String privilege) {
this.privilege = privilege;
}
@Override
public int doStartTag() throws JspException {
boolean result = false;
String auth = module+","+privilege;
User login = (User)pageContext.getSession().getAttribute("logintest");
if (null!=login && login.isAuthed(auth)) {
result = true;
}
return result? EVAL_BODY_INCLUDE : SKIP_BODY; // 显示或隐藏标签内的内容
}
}
在web-inf先新建 itcast.tld,内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>itcast babasport permission taglib</description>
<display-name>permission taglib</display-name>
<tlib-version>1.0</tlib-version>
<short-name>itcast</short-name>
<uri>http://www.itcast.cn/babasport</uri>
<tag>
<description>权限校验标签,有权限就显示标签体的内容,否则不显示</description>
<name>permission</name>
<tag-class>cn.itcast.web.taglib.PermissionTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<description></description>
<name>module</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<description></description>
<name>privilege</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
*.tld的写法可以参考standard.jar\META-INF\*.tld
taglib.jsp:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="http://www.itcast.cn/babasport" prefix="itcast"%>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 解决中文乱码问题 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 使用spring解决hibernate因session关闭导致的延迟加载例外问题 -->
<!--
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->
<!-- 配置过滤器 -->
<!--
<filter>
<filter-name>PrivilegeFilter</filter-name>
<filter-class>cn.itcast.web.filter.PrivilegeFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrivilegeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>http://www.itcast.cn/babasport</taglib-uri>
<taglib-location>/WEB-INF/itcast.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
ProductTypeList.jsp:
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/page/share/taglib.jsp"%>
<html>
<head>
<title>权限组显示</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="/css/vip.css" type="text/css">
<SCRIPT language=JavaScript src="/js/FoshanRen.js"></SCRIPT>
</head>
<body bgcolor="#FFFFFF" text="#000000" marginwidth="0" marginheight="0">
<table width="98%" border="0" cellspacing="1" cellpadding="2"
align="center">
<tr ><td colspan="5" bgcolor="6f8ac4" align="right">
<%@ include file="/WEB-INF/page/share/fenye.jsp" %>
</td></tr>
<tr>
<td width="10%" bgcolor="6f8ac4">
<div align="center">
<font color="#FFFFFF">ID</font>
</div>
</td>
<td width="50%" nowrap bgcolor="6f8ac4">
<div align="center">
<font color="#FFFFFF">Name</font>
</div>
</td>
<td bgcolor="6f8ac4">
<div align="center">
<font color="#FFFFFF">Note</font>
</div>
</td>
<td bgcolor="6f8ac4">
<div align="center">
<font color="#FFFFFF">Visible</font>
</div>
</td>
<td width="10%" bgcolor="6f8ac4"></td>
</tr>
<!---------------------------LOOP START------------------------------>
<c:forEach items="${productTypes.resultlist}" var="prodtype">
<tr>
<td bgcolor="f5f5f5">
<div align="center">${prodtype.typeid}</div>
</td>
<td bgcolor="f5f5f5">
<div align="center">${prodtype.name}</div>
</td>
<td bgcolor="f5f5f5">
<div align="center">${prodtype.note}</div>
</td>
<td bgcolor="f5f5f5">
<div align="center">${prodtype.visible}</div>
</td>
<td bgcolor="f5f5f5" align="center">
<itcast:permission module="department" privilege="edit"><!-- 没有权限就不显示“修改”链接-->
<a href="/product/producttype_editUI.do?productType.typeid=${prodtype.typeid }">修改</a>
</itcast:permission>
</td>
</tr>
</c:forEach>
<!----------------------LOOP END------------------------------->
<tr>
<td bgcolor="f5f5f5" colspan="5" align="center"><table
width="100%" border="0" cellspacing="1" cellpadding="4">
<tr>
<td width="5%"></td>
<td width="85%">
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
分享到:
相关推荐
在Struts2中,拦截器扮演着至关重要的角色,它们是实现业务逻辑和控制流程的核心组件。本知识点主要聚焦于如何利用Struts2的拦截器进行权限控制。 **一、Struts2拦截器原理** 在Struts2中,拦截器是基于Java的动态...
Struts2是一个强大的MVC(模型...通过理解和研究Struts2的拦截器源码,我们可以更好地利用这个强大工具,提高应用的可扩展性和灵活性。同时,这也为我们提供了对Struts2框架内部运作的深入了解,有助于优化和调试代码。
3. **配置拦截器**:拦截器可以通过Struts2配置文件或注解进行配置,指定在哪些Action上使用哪些拦截器。 ### 二、单个Action配置拦截器实例 1. **创建拦截器**:首先,我们需要创建一个实现了`Interceptor`接口的...
包中定义了3个主要的拦截器注解类型:`@Before`、`@After`和`@Around`. 这些注解允许你在Action类或者方法上直接定义拦截器,无需在配置文件中声明。 1. `@Before`注解:此注解标记的方法会在Action执行之前被调用...
核心拦截器是Struts2框架的重要组成部分,它们允许开发者在动作执行前后插入自定义逻辑,以实现如日志、权限验证、数据校验等常见功能。下面将详细讲解Struts2的核心拦截器以及如何在项目中使用它们。 1. **Struts2...
SSH2 框架中的 Struts2 拦截器功能允许开发者在特定操作执行前或执行后进行自定义处理,例如登录验证、权限检查等。在本文中,我们将探讨如何使用 Struts2 的拦截器来实现登录验证功能。 首先,我们需要了解 Struts...
这个“Struts2简单的拦截器示例”是为初学者设计的,旨在帮助理解如何在Struts2中创建和使用拦截器。 首先,我们来理解什么是拦截器。在Struts2中,拦截器是一个实现了`Interceptor`接口或者继承自`...
总的来说,Struts2实现权限拦截涉及到创建自定义拦截器、配置拦截器栈、编写权限检查逻辑以及处理异常情况。这是一个涉及多方面技术知识的过程,但通过学习和实践,开发者能够有效地增强系统的安全性,为用户提供...
struts 拦截器实现权限拦截,例子简单易懂,有详细的注释。
1. **Struts拦截器**: - 拦截器是Struts2的核心组件之一,它们是基于Java的动态代理机制实现的。在Action调用前后,拦截器可以执行一系列预定义的任务,如日志记录、权限验证、数据校验等。 - 拦截器链是由多个...
3. **拦截器(Interceptors)**:Struts2的拦截器是其强大功能的一部分,它们可以像过滤器一样在Action调用前后执行额外的任务,如日志记录、权限检查、性能监控等。开发者可以通过自定义拦截器来扩展框架的功能。 ...
拦截器可以理解为一种预处理和后处理机制,它在请求到达控制器之前和离开控制器之后进行操作,例如日志记录、权限检查、性能统计等。本篇文章将深入探讨如何实现自定义拦截器及其在Struts2中的详细配置。 首先,...
拦截器用于在 Action 方法执行前后进行额外的操作,如日志记录、权限验证等。 **Spring 注解配置** Spring 使用注解配置主要是为了简化 Bean 的定义,包括但不限于以下几种: 1. `@Component`, `@Service`, `@...
6. **拦截器(Interceptors)**:拦截器是Struts2中的一个强大特性,它可以拦截Action执行前后的流程,实现日志记录、权限验证、事务管理等功能。常见的拦截器有Params拦截器(处理请求参数),Validation拦截器...
此外,Interceptor(拦截器)是Struts2的重要特性,它可以实现诸如日志记录、权限检查、事务管理等通用功能,并且可以按照特定顺序进行链式调用。 Struts2的配置文件通常为struts.xml,它定义了Action、Result以及...
Struts2是一个强大的MVC框架,它为Java Web应用程序提供了结构和控制机制。在Web开发中,文件上传和下载功能是非常常见的需求,Struts2为此提供了完善的解决方案。本项目主要展示了如何在Struts2框架下实现单个文件...
3. **拦截器(Interceptors)**:拦截器是Struts2的一大特色,它们按照预定义的顺序在Action调用前后执行,可以实现如日志记录、权限验证、性能监控等功能。常见的拦截器有`params`(处理请求参数)、`validation`...
本实例可能会展示如何使用Struts2的拦截器进行权限控制,确保只有登录用户才能访问特定的页面或功能。 除了以上核心功能,这个实例可能还涵盖了其他相关知识点,如JSP、Servlet、数据库操作(可能使用JDBC或ORM框架...
拦截器是Struts2的一个强大特性,可以在Action执行前后进行额外的操作,比如日志记录、权限验证、数据校验等。通过在`struts.xml`中配置拦截器栈,可以定制化Action的执行流程。 **6. OGNL(Object-Graph ...
Struts2的核心设计目标是简化MVC应用的开发,通过提供一系列的插件和拦截器,使得开发者可以方便地处理请求、响应以及业务逻辑。它集成了许多其他开源框架,如FreeMarker和Velocity模板引擎、Tiles布局框架、OGNL...