Java custom simple tag instances
- 2020-04-01 02:10:03
- OfStack
The following will take the control of permissions as an example to customize a label:
One, label type
<wxt:per uri="${pageContext.request.contextPath }/privilege/list"></wxt:per>
Steps:
1. Custom PerssionTag class inherits SimpleTagSupport (custom tags typically inherit from this class)
package cn.com.liveuc.privilege.tag;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import cn.com.liveuc.privilege.model.Privilege;
import cn.com.liveuc.privilege.model.Resource;
import cn.com.liveuc.privilege.model.Role;
import cn.com.liveuc.privilege.model.User;
public class PerssionTag extends SimpleTagSupport {
//Custom tag properties for tag passing parameters
private String uri;
//Receive the parameters passed in by the label
public void setUri(String uri) {
this.uri = uri;
}
@Override
public void doTag() throws JspException, IOException {
//Gets the Session saved after the user logs in
PageContext page = (PageContext) this.getJspContext();
User user = (User) page.getSession().getAttribute("login");
//If the user logs in
if(user != null) {
//User login to determine user permissions
List<String> list = new ArrayList<String>();
//Gets the user's role
Set<Role> role = user.getRole();
for(Role r:role) {
//Gets the permissions for the role
Set<Privilege> privilege = r.getPrivilege();
for(Privilege p:privilege) {
//Gets the resource corresponding to the permission
Set<Resource> res = p.getResource();
for(Resource re:res) {
list.add(re.getUri());
}
}
}
for(String ur:list) {
//Determine the user's permissions
if(ur.equals(uri)) {
this.getJspBody().invoke(null); //Has permission to output the tag body content
}
}
}
}
}
2. Create the TLD file description label under web-inf.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
<description><![CDATA["To make it easier to access dynamic data;
the Apache Struts framework includes a library of custom tags.
The tags interact with the framework's validation and internationalization features;
to ensure that input is correct and output is localized.
The Struts Tags can be used with JSP FreeMarker or Velocity."]]></description>
<display-name>"Struts Tags"</display-name>
<tlib-version>2.2.3</tlib-version>
<short-name>s</short-name>
<uri>/wxt</uri>
<tag>
<name>per</name><!-- Tag name -->
<tag-class>cn.com.liveuc.privilege.tag.PerssionTag</tag-class>
<body-content>scriptless</body-content>
<!-- Tag attributes -->
<attribute>
<name>uri</name><!-- The attribute name -->
<required>true</required><!-- Whether must -->
<rtexprvalue>true</rtexprvalue><!-- Is it a dynamic tag -->
</attribute>
</tag>
</taglib>
Use labels
Import the tag in the Jsp page:
< A href="mailto:%@taglib prefix=' WXT 'uri='/ WXT' %"> % @ taglib prefix = "WXT" uri = "/ WXT" % < / A>
Use of labels:
< WXT: per uri = "${pageContext. Request. ContextPath} / user/list" >
< A href = "${pageContext. Request. ContextPath} / user/list" target = "reight >" User management < / a>
< / WXT: per>
User permissions that contain uri resources will output label content.