Java Custom Simple Label instance _java

Source: Internet
Author: User

The following will take control of permissions as an example to customize a label:
One, label type

Copy Code code as follows:

<wxt:per uri= "${pagecontext.request.contextpath}/privilege/list" ></wxt:per>

steps:
1. Customize a Class Perssiontag inheritance Simpletagsupport (custom tags will generally inherit this class)
Copy Code code 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 Label
*/
public class Perssiontag extends Simpletagsupport {

Custom Label properties, for label incoming parameters
Private String URI;

Receive label incoming parameters
public void Seturi (String uri) {
This.uri = URI;
}
@Override
public void Dotag () throws Jspexception, IOException {
Get the session that the user saved after logging 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 rights
list<string> list = new arraylist<string> ();
Get the user's role
set<role> role = User.getrole ();
for (role R:role) {
Get Permissions for roles
set<privilege> privilege = R.getprivilege ();
for (privilege P:privilege) {
To obtain a resource corresponding to a permission
set<resource> res = P.getresource ();
for (Resource re:res) {
List.add (Re.geturi ());
}
}
}
for (String ur:list) {
To determine the permissions of a user
if (Ur.equals (URI)) {
This.getjspbody (). Invoke (null); have permission to output tag body content
}
}
}
}
}

2. Create a TLD file description tag under Web-inf.
Copy Code code 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 includes a library of custom tags.
The tags interact with the framework ' s validation and internationalization features;
To ensure, 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><!--Label name-->
<tag-class>cn.com.liveuc.privilege.tag.PerssionTag</tag-class>
<body-content>scriptless</body-content>
<!--Tag Properties-->
<attribute>
<name>uri</name><!--Property name-->
Whether the <required>true</required><!--must be-->
<rtexprvalue>true</rtexprvalue><!--is a dynamic label-->
</attribute>
</tag>
</taglib>

3. Use the label
To import a label on a 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>
User rights that contain URI resources will output the label content.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.