Java Custom Simple Label instance

Source: Internet
Author: User
Tags tag name xmlns
Java custom simple tags can easily output information on the page, and the control of permissions, and for JSP tags and servlet code separation has a good role

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 user Login Save session


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 the role's corresponding permissions


set<privilege> privilege = R.getprivilege ();


for (privilege P:privilege) {


//Get the resources corresponding to the permissions


set<resource> res = P.getresource ();


for (Resource re:res) {


List.add (Re.geturi ());


     }


    }


   }


for (String ur:list) {


Judge 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 that the input is correct and the 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>


<!--label Properties-->


<attribute>


<name>uri</name><!--Property name-->


<required>true</required><!--must be-->


<rtexprvalue>true</rtexprvalue><!--is a dynamic label-->


</attribute>


</tag>


</taglib>


3. Use tags
import tags in jsp page:
<a href= "mailto:% @taglib prefix= ' wxt ' uri= '/wxt '%" >%@ Taglib prefix= "Wxt" uri= "/wxt"%</A>
uses 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.

Related Article

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.