创建权限标签(类似<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));
}
}