Struts2通过自定义标签实现权限控制的方法
作者:陈夏明 时间:2021-07-19 11:06:22
近期在开发中遇到一种需求:根据用户的权限决定是否显示某操作按钮。
例如:若用户拥有删除数据的权限,则在界面中显示“删除”按钮;若用户无该权限,则界面中不显示相应按钮。
这样,就需要用到自定义标签了。
要定义Struts2的自定义标签,只需三步:
1.定义一个Component类,并继承自org.apache.struts2.components.Component;
2.定义一个Tag类,并继承自import org.apache.struts2.views.jsp.ComponentTagSupport;
3.在WEB-INF目录下创建相应的LTD文件
下面就来逐一实现:
step1:定义Component类
Component,顾名思义是“组件”。这其中封装了标签需要的处理逻辑。
我们定义的Component类需要继承org.apache.struts2.components.Component;。在父类中,有两个方法比较重要,分别是:start()方法和end()方法,这两个方法分别对应了开始标签和结束标签,我们可以通过这两个方法来对标签进行操作。
此外,若标签需要属性,则应该在本类中定义相应属性,并提供相应的get()和set()方法,用于封装这些属性。
然后,我们就可以对标签及属性进行处理了。
这里给出我的代码,实现的功能是:将当前每一个要显示的按钮的URL地址与用户所拥有的权限对应的URL比较,若用户有权限访问该URL,则显示按钮,否则将不显示任何内容。
package qdgxy.tag;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.components.Component;
import qdgxy.domain.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.Writer;
public class AComponent extends Component {
private String actionURL;
private String value;
private String onclick = "";
private HttpServletRequest request;
@Override
public boolean start(Writer writer) {
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
if (user.hasPrivilegeByURL(actionURL)) {
try {
writer.write("<a href='" + formatURL(actionURL) + "' onClick='" + onclick + "'>" + value + "</a>");
} catch (IOException e) {
e.printStackTrace();
}
return true;
} else {
return super.start(writer);
}
}
private String formatURL(String url) {
int index = url.indexOf('?');
if (index != -1) {
String params = url.substring(index);
url = url.substring(0, index) + ".action" + params;
} else {
url = url + ".action";
}
return url;
}
public AComponent(ValueStack stack, HttpServletRequest request) {
super(stack);
this.request = request;
}
public String getActionURL() {
return actionURL;
}
public void setActionURL(String actionURL) {
this.actionURL = actionURL;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getOnclick() {
return onclick;
}
public void setOnclick(String onclick) {
this.onclick = onclick;
}
}
step2:定义Tag类
Tag类,说白了就是我们所使用的标签。
创建Tag类,首先需要继承org.apache.struts2.views.jsp.ComponentTagSupport类。
然后,重写getBean()方法和populateParams()方法。
getBean()方法用于返回标签组件的实例,即我们step1中创建的组件。
populateParams()方法用于填充属性,即:将我们标签中的属性封装到标签类中。因此,我们在Tag类中也需要定义相应属性并提供get()和set()方法。
这样,我们就可以指定组件对标签进行处理。
可能有人会有疑问:为什么我们不将标签处理逻辑直接写在Tag类中,而是要单独定义一个Component类呢?
这是因为:Struts2支持多种表现层技术,使用Component可以对不同的表现层技术使用不同的处理方式,提高程序的可扩展性。
给出我的代码:
这段代码实现的功能是:使用step1中定义的组件对标签进行处理。
package qdgxy.tag;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ComponentTagSupport;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ATag extends ComponentTagSupport {
private String actionURL;
private String value;
private String onclick = "";
@Override
public Component getBean(ValueStack valueStack, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
return new AComponent(valueStack, httpServletRequest);
}
@Override
protected void populateParams() {
AComponent component = (AComponent) getComponent();
component.setActionURL(actionURL);
component.setValue(value);
component.setOnclick(onclick);
}
public String getOnclick() {
return onclick;
}
public void setOnclick(String onclick) {
this.onclick = onclick;
}
public String getActionURL() {
return actionURL;
}
public void setActionURL(String actionURL) {
this.actionURL = actionURL;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
step3:在WEB-INF目录下创建TLD文件
TLD文件描述了标签的语法,如:标签有哪些属性,标签的属性是否支持表达式等内容。
我们在JSP中使用标签前,都需要引入相应的标签库,所以我们需要定义TLD文件,使JSP中能使用我们的自定义标签。
给出我定义的TLD文件,其中有几个简单的属性。
<?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>oa</short-name>
<uri>/WEB-INF/OATag.tld</uri>
<tag>
<name>a</name>
<tag-class>qdgxy.tag.ATag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>actionURL</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>onclick</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
其中:
short-name:相当于前缀
uri:就是<%@taglib>中的uri
tag:一个标签
tag-class:标签类,就是我们的Tag类
body-content:标签体中能写的内容
attribute:属性
name:属性名
required:是否必须
rtexprvalue:是否支持表达式
TLD文件中的内容还有很多,更多内容可以Google一下就能获得,在此就不再赘述。
至此,我们就可以在JSP中使用自定义标签了。
使用的方式:
1. 引入标签库:<%@ taglib prefix="oa" uri="/WEB-INF/OATag.tld" %>
2. 使用标签:<oa:a actionURL="user_editUI?id=${id}" value="修改"/>
这样,在用户查看当前页面时,就能够根据用户的权限来选择是否显示操作按钮。
来源:http://blog.csdn.net/u012325167/article/details/51382350
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java使用Apache.POI中HSSFWorkbook导出到Excel的实现方法
![](https://img.aspxhome.com/file/2023/3/63323_0s.png)
Android M(6.x)使用OkHttp包解析和发送JSON请求的教程
Java中Map与JSON数据之间的互相转化
![](https://img.aspxhome.com/file/2023/5/104065_0s.jpg)
使用SSM+Layui+Bootstrap实现汽车维保系统的示例代码
![](https://img.aspxhome.com/file/2023/6/60416_0s.jpg)
Java针对封装数组的简单复杂度分析方法
![](https://img.aspxhome.com/file/2023/5/113185_0s.png)
解析Java中未被捕获的异常以及try语句的嵌套使用
Java:泛型知识知多少
![](https://img.aspxhome.com/file/2023/8/60188_0s.jpg)
C#学习笔记——基本语法
![](https://img.aspxhome.com/file/2023/7/81107_0s.png)
Java Spring Dubbo三种SPI机制的区别
C#发送Get、Post请求(带参数)
springboot bootcdn使用示例详解
详解Spring Boot微服务如何集成fescar解决分布式事务问题
![](https://img.aspxhome.com/file/2023/1/67951_0s.png)
C# Dictionary的使用实例代码
C++ boost scoped_ptr智能指针详解
![](https://img.aspxhome.com/file/2023/7/117767_0s.png)
Java遍历Properties所有元素的方法实例
一文详解Java抽象类到底有多抽象
![](https://img.aspxhome.com/file/2023/8/62168_0s.png)
Android实现银行卡号扫描识别功能
Spring Boot启动端口修改方法
C# ref and out的使用小结
![](https://img.aspxhome.com/file/2023/1/106391_0s.png)