Custom labels can greatly facilitate the authoring of our page controls, and can achieve a lot of unified management.
0, first need to introduce the support of custom label jar package, there are two ways,
One is to introduce dependencies directly into Maven:
<dependency> <groupId>javax.servlet.jsp</groupId> <artifactId> javax.servlet.jsp-api</artifactid> <version>2.3.1</version> <scope>provided </scope> </dependency>
The other is directly with your Tomcat JSP-API.
You can take one of two ways.
1. Custom method labels via TLD files
This usage of TLD allows us to use the methods defined in the class directly in the JSP
Introduction to an example of a TLD tag file directly on the JSP:
<%@ taglib prefix= "FNS" uri= "/web-inf/tlds/fns.tld"%>
Example (filename fns.tld, where the location is defined in the URI above):
<?xml version= "1.0" encoding= "UTF-8"? ><taglib xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "http// Www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ Ns/j2ee/web-jsptaglibrary_2_0.xsd "version=" 2.0 "> <description>jstl 1.1 Functions library</description& Gt <display-name>jstl functions sys</display-name> <tlib-version>1.1</tlib-version> < Short-name>fns</short-name> <uri>http://java.sun.com/jsp/jstl/functionss</uri> <!-- Dictutils-<function> <description> get a list of Dictionary objects </description> <name>getdictlist</nam E> <function-class>com.sdyy.base.sys.utils.DictUtils</function-class> <function-signature> Java.util.List getdictlist (java.lang.String) </function-signature> <example>${fns:getdictlist ( TypeCode)}</example> </function> <function> <desCription> get a list of Dictionary objects </description> <name>getDictListJson</name> <function-class> Com.sdyy.base.sys.utils.dictutils</function-class> <function-signature>java.lang.string Getdictlistjson (java.lang.String) </function-signature> <example>${fns:getdictlistjson (TypeCode)}</example> </function> <function> <description> object Change json</description> <name& Gt;tojsonstring</name> <function-class>com.alibaba.fastjson.JSON</function-class> < Function-signature>java.lang.string tojsonstring (java.lang.Object) </function-signature> </function ></taglib>
Function-class is the classpath of the entity of the method,
Function-signature is the method name of the method, and it is worth mentioning that this method must be a static method.
Example is an example of how to use
2. Customize the control label via the tag file (thank you for commenting on the first floor)
Such tags, so that we can in the JSP, by passing in some parameters, dynamic generation of JSP code
Examples of ingestion methods:
<%@ taglib prefix= "sys" tagdir= "/web-inf/tags/sys"%>
Note that the introduction of this is a path
So tags directory under the sys directory of all tags ( thanks to comment area on the first floor) will automatically be loaded, tag tag example (in fact, similar to JSP):
<%@ tag language= "java" pageencoding= "UTF-8"%><%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C" %><%@ taglib prefix= "FNS" uri= "/web-inf/tlds/fns.tld"%><%@ attribute name= "TypeCode" type= " Java.lang.String "required=" true "description=" Dictionary code "%><%@ attribute name=" DefaultValue "type=" Java.lang.String "Description=" is selected by default "%><%@ attribute Name=" style "type=" java.lang.String "description=" Default check "% ><%@ attribute Name= "CLS" type= "java.lang.String" description= "Default check"%><%@ attribute name= "name" Type= " Java.lang.String "Description=" is selected by default "%><select style=" ${style} "class=" ${cls} "Name=" ${name} "id=" ${name} "> <option value= "" > Please select ... </option> <c:if test= "${not Empty TypeCode}" > <c:foreach item S= "${fns:getdictlist (TypeCode)}" var= ' Dict ' > <option value= ' ${dict. VALUE} ' ${defaultvalue==dict. VALUE? ' Selected ': '}>${dict. Text}</option> </c:forEach> </c:if>≪/select>
Here I introduced two taglib, in order to use them in this file, attribute is equivalent to parameters, define these parameters in order to use the dynamic pass-through, note that there are some keywords, such as the "CLS" Here, because "class" is the keyword
So, the tag name is the tag name, for example, this tag is called select. Tag( Thank you for commenting on the first floor of the comment area), then its usage is
<sys:cls= "Formselect" name= "Module_type" typecode= "Home_module_type" defaultvalue= "${record. Module_type} "/>
"SYS" in "Sys:select" is the Prefix,select and "Select" that we defined in the "Introduction to Example" above. The red part of tag " is the corresponding
3, through the TLD file custom judgment label, control label (Steal a lazy, write a piece)
The introduction method is the same as 1, and the example is as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE taglib Public "-//sun Microsystems, Inc.//dtd JSP Tag Library 1.2//en" "Http://java.sun.com/dtd/web-jsptagli Brary_1_2.dtd "><taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</ Jsp-version> <short-name>bgt</short-name> <!--Backgroundtag--and <uri>http://www.sdyy. tag</uri> <tag> <name>hasUrlPerm</name> <tag-class>com.sdyy.common.tags.has urlpermissiontag</tag-class> <attribute> <name>link</name> <requir Ed>false</required> <rtexprvalue>true</rtexprvalue><!--support for harsh expressions--< ;type>java.lang.string</type> <description> Example:acapplication/formain.do</description> </attribute> </tag></taglib>
A, "Judgment label" Hasurlpermissiontag label is a judgment label, through the label to determine whether the content of the label is displayed, the following wording:
Package Com.sdyy.common.tags;import Javax.servlet.http.httpservletrequest;import javax.servlet.jsp.JspException; Import Javax.servlet.jsp.tagext.bodytagsupport;import com.sdyy.common.spring.interceptor.permissioninterceptor;/ * * * @ClassName: Hasurlpermissiontag * @Description: Determine permissions tag according to URL * @author: Liuyx * @date: December 21, 2015 morning 11:15:32 */publ IC class Hasurlpermissiontag extends Bodytagsupport {private String link;//acapplication/formain.do @Override public int doStartTag () throws Jspexception {//starting at the beginning of the label the Method HttpServletRequest request= (httpservletrequest ) Pagecontext.getrequest (); Gets the permissions that are stored in the session//determines if there is permission to access the IF (permissioninterceptor.isopercanaccess (request)) { Allow access to tag body return bodytagsupport.eval_body_include;//returns this executes the contents of the tag body, Skip_body does not execute} else { return bodytagsupport.skip_body; }} @Override public int doendtag () throws Jspexception {return BodytagSupport.eval_body_include; } public String GetLink () {return link; } public void Setlink (String link) {this.link = link; }}
How to use in JSP:
<bgt:hasurlperm link= "Abc.do" > <div>tttttttttttttttttest</div> </bgt:hasurlperm >
B, "Control label", this label directly returns a control, but the content of the control generated through Java code, writing example:
Package Com.sdyy.common.tags;import Java.io.ioexception;import Java.util.arraylist;import java.util.List;import Javax.servlet.jsp.jsptagexception;import Javax.servlet.jsp.jspwriter;import Javax.servlet.jsp.tagext.bodytagsupport;/*import Com.sdyy.base.ac.ac_permission.model.acpermission;*/public Class Buttonurltag extends Bodytagsupport {private static final long serialversionuid = -7811902545513255473l; Label attribute user name private String user = null; Tag Property action URL private String URL = null; Tag attribute js method private String Jsmethod = null; Label properties image button Picture Private String image = null; Label attribute alt hint private String alt = NULL; Tag Property operation value button text Private String value = null; /* Tag Initial method */public int doStartTag () throws jsptagexception{return super. Eval_body_include; }/* Label End method */public int doendtag () throws jsptagexception{Boolean B = false; List List = new ArrayList (); /*acpermission p = new Acpermission (); */ /*jdbchibernate JDBCA = new Jdbchibernate (); */try {/*list = jdbca.gethaveurlbyusernamelist (user) ; */} catch (Exception E1) {//TODO auto-generated catch block E1.printstacktrace (); } for (int i = 0;i < List.size (); i++) {/*p = (acpermission) list.get (i); */if (1==1) {//p. GETURL (). Trim (). Equals (Url.trim ())) {B = true; If the Jsmethod property is not null then the hyperlink href is changed to call JS if (jsmethod!=null) {url = Jsmethod; }}} JspWriter out = Pagecontext.getout (); if (b) {try {//have permission to display the action button out.println ("<a href= '" +url+ "' class= ' regular ' > "+ value +" </a> "); } catch (IOException e) {e.printstacktrace (); }} return super. Skip_body; }/* FREE resources */public void RElease () {super.release (); } public String GetUser () {return user; The public void SetUser (String user) {this.user = user; } public String GetUrl () {return URL; } public void SetUrl (String url) {this.url = URL; } public String GetImage () {return image; The public void setimage (String image) {this.image = image; } public String Getalt () {return alt; } public void Setalt (String alt) {this.alt = alt; } public String GetValue () {return value; The public void SetValue (String value) {this.value = value; } public String Getjsmethod () {return jsmethod; } public void Setjsmethod (String jsmethod) {this.jsmethod = Jsmethod; }}
In general, the label is generally defined by TLD and tag, this article is only a few of the basic usage list, I hope to be helpful to everyone.
Category: JSP
JSP Custom Labels