Java custom simple labels can easily output information on pages, and control permissions, and play a good role in separating Jsp labels from servlet code.
The following uses permission control as an example to customize a tag:
I. Tag type
Copy codeThe Code is as follows:
<Wxt: per uri = "$ {pageContext. request. contextPath}/privilege/list"> </wxt: per>
Steps:
1. Customize a class PerssionTag to inherit SimpleTagSupport (custom tags generally inherit this class)
Copy codeThe Code is as follows:
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;
/**
*
* @ Description: custom tag
*/
Public class PerssionTag extends SimpleTagSupport {
// Custom tag attributes, used for tag input parameters
Private String uri;
// Receives the parameters passed in by the tag.
Public void setUri (String uri ){
This. uri = uri;
}
@ Override
Public void doTag () throws JspException, IOException {
// Obtain 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 logon to determine User Permissions
List <String> list = new ArrayList <String> ();
// Obtain the role of a user
Set <Role> role = user. getRole ();
For (Role r: role ){
// Obtain the permissions of the role
Set <Privilege> privilege = r. getPrivilege ();
For (Privilege p: privilege ){
// Obtain 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); // you have the permission to output the TAG body content.
}
}
}
}
}
2. Create a tld file description tag under the WEB-INF.
Copy codeThe Code is as follows:
<? 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 has des a library of M 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> <! -- Property name -->
<Required> true </required> <! -- Required? -->
<Rtexprvalue> true </rtexprvalue> <! -- Whether it is a dynamic tag -->
</Attribute>
</Tag>
</Taglib>
3. Use tags
Import tags on the Jsp page:
<A href = "mailto: % @ taglib prefix = 'wxt 'uri = '/wxt' % ">%@ taglib prefix =" wxt "uri ="/wxt "% </A>
Use tags:
<Wxt: per uri = "$ {pageContext. request. contextPath}/user/list">
<A href = "$ {pageContext. request. contextPath}/user/list" target = "reight"> user management </a>
</Wxt: per>
If the user permission contains the uri resource, the TAG content is output.