创建权限标签(类似<s:if>标签),如<p:permit module="Sysuser" operate="add"><input type="button" value="test"/></p:permit>
1.创建tld文件(可参照struts-tags.tld),将其存在classpath下:
<display-name>"Custom Tag"</display-name>
<tlib-version>1.0</tlib-version>
<short-name>p</short-name>
<uri>/mylib-tags</uri>
<tag>
<description><![CDATA[Permit tag]]></description>
<name>permit</name>
<tag-class>com.kevin.tag.PermissionTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<description><![CDATA[Expression to determine if user have permission to display this content]]></description>
<name>module</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<description><![CDATA[Expression to determine if user have permission to display this content]]></description>
<name>operate</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<dynamic-attributes>false</dynamic-attributes>
</tag>
2.创建permissionTag:
public class PermissionTag extends ComponentTagSupport {
private static final long serialVersionUID = 1L;
String module;
String operate;
@Override
public Component getBean(ValueStack stack, HttpServletRequest req,
HttpServletResponse res) {
return new PermissionComponent(stack, req);
}
protected void populateParams() {
((PermissionComponent) getComponent()).setModule(module);
((PermissionComponent) getComponent()).setOperate(operate);
}
public void setModule(String module) {
this.module = module;
}
public void setOperate(String operate) {
this.operate = operate;
}
}
3.创建PermissionComponent:
@StrutsTag(name = "permit", tldTagClass = "com.kevin.tag.PermissionTag", description = "Permit tag")
public class PermissionComponent extends Component {
public static final String ANSWER = "com.kevin.tag.permissiontag.answer";
Boolean answer = Boolean.FALSE;
String module;
String operate = "";
HttpServletRequest req;
@StrutsTagAttribute(description = "Expression to determine permit body of tag is to be displayed", type = "String", required = true)
public void setModule(String module) {
this.module = module;
}
@StrutsTagAttribute(description = "Expression to determine permit body of tag is to be displayed", type = "String", required = false)
public void setOperate(String operate) {
this.operate = operate;
}
public PermissionComponent(ValueStack stack, HttpServletRequest req) {
super(stack);
this.req = req;
}
public boolean start(Writer writer) {
if (module != null) {
Sysuser sysuser = (Sysuser) req.getSession().getAttribute("user");
Map<String, String> permissionMap = sysuser.getPermissionMap();
String sessionOperate = permissionMap.get(module);
if (sessionOperate != null) {
if (operate == null) {
answer = Boolean.TRUE;
} else {
operate = convertOperate(operate);
if (sessionOperate.contains(operate))
answer = Boolean.TRUE;
}
}
}
stack.getContext().put(ANSWER, answer);
return answer.booleanValue();
}
public boolean end(Writer writer, String body) {
stack.getContext().put(ANSWER, answer);
return super.end(writer, body);
}
private String convertOperate(String operate) {
StringBuffer result = new StringBuffer(",");
Operation[] operates = Operation.values();
for (Operation o : operates) {
if (o.name().equals(operate)) {
result.append(o.getValue());
break;
}
}
result.append(",");
return result.toString();
}
public static void main(String[] args) {
String test = "kevin";
System.out.println(test.contains(null));
}
}
分享到:
相关推荐
通过struts2 自定义标签实例,可以用这个对项目中的实现自定义标签。从而减少代码。
Struts2自定义标签,附带配置文件及后台代码,从工作当中整理
Struts2中实现自定义标签很简单,主要分为3步: 1.创建taglib文件(.tld),编写标签声明。 2.编写自定义标签类。 3.在页面中使用标签。 下面以一个自定义的分页标签为例,进行说明。 其实,开发自定义标签并不...
Struts2自定义分页标签很详细了,新手可以少走弯路,节约时间 资源包括了后台如何操作。
基于struts2的自定义分页标签实现的一个demo,连接数据库为Oracle
附件是一个简单的自定义标签,提供给大家参考,例子很简单,希望对大家有帮助,基于eclipse 开发
tag struts2 自定义标签 实例 tag struts2的自定义标签实例
Struts 2 自定义标签分页,有简单上下分页和数字分页
struts实现自定义标签
STRUTS自定义标签STRUTS自定义标签STRUTS自定义标签STRUTS自定义标签STRUTS自定义标签STRUTS自定义标签STRUTS自定义标签STRUTS自定义标签STRUTS自定义标签STRUTS自定义标签STRUTS自定义标签STRUTS自定义标签STRUTS...
自定义标签,直接代码,直接模仿开发即可 public class ButtonTag extends AttributeTag { //tld中定义的属性 public String label; public String url; public String click; public String confirm; public ...
自定义标签主要包括三个步骤: 1、编写java类,继承TagSupport类; 2、创建tld文件,影射标签名和标签的java类; 3、jsp页面引入tld。 博文地址:http://blog.csdn.net/itmyhome1990/article/details/50718282
举例解说Bean Tags,HTML Tags 以及Logic Tags的使用方法。
struts2的自定义标签写法,并且有一个例子代码。
Struts2_自定义标签的方法