java--Custom Label (tag, TLD two kinds)

Source: Internet
Author: User
Tags html form tld

1. A good Java custom label article address: http://gaoshu2006.blog.sohu.com/113222643.html

2.sun Company Java Custom Label original address: http://java.sun.com/developer/technicalArticles/xml/WebAppDev3/

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------

JSP custom tag tag file version

Implement a Select Label feature similar to the previous article
1. In Web-inf/tags/select.tag

<%@ tag body-content= "Empty"%>
<%@ tag dynamic-attributes= "Tagattrs"%>
<%@ attribute Name= "O Ptionslist "type=" java.util.List "required= true" rtexprvalue= "true"%>
<%@ attribute name= "name" required= " True "%>
<%@ attribute name=" Size "required=" true "%>
<%@ taglib prefix=" C " uri=" http://" Java.sun.com/jstl/core_rt " %>


<select name= "${name}" size= "${size}"
<c:foreach var= "Attrentry" items= "${tagattrs}" >
${attrentry.key}= "${attrentry.value}"
</c:forEach>
>
<c:foreach var= "option" items= "${optionslist}" >
<option value= "${option}" >${option}</option>
</c:forEach>
</select>
Notice here that the tag file can only be placed in the following locations:
1.web-inf/tags
Subdirectories of 2.web-inf/tags
Meta-inf/tags of jar packages in 3.web-inf/lib
Sub-directory under Meta-inf/tags of JAR packages in 4.web-inf/lib
The tag file in the 5.jar package requires TLD
Add Jstl.jar and Standard.jar to the Web-inf/lib directory, and one more is the red section above: Do not use the Http://java.sun.com/jstl/core URL, Otherwise there is a problem with the Item property in the Foreach

2. Use in JSP

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<%@ page import= "java.util.*"%>
<%@ taglib prefix= "Formtag" tagdir= "/web-inf/tags"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
<%
list<string> colorlist = new arraylist<string> ();
Colorlist.add ("Red");
Colorlist.add ("Blue");
Colorlist.add ("white");
Request.setattribute ("ColorList", colorlist);
%>

<form action= "" method= "POST" >
<formtag:select name= "Color" size= "1" optionslist= "${requestscope.colorlist}" style= "width:140px"/>
</form>
</body>

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------

JSP Custom Label

JSP tags have two sets of APIs
Jsptag->simpletag->simpletagsupport
Jsptag->tag->iterationtag->bodytag
The second group is classic, the earlier use of the way, doStartTag (), Doendtag () has more than the return of the value of the type, it is really inconvenient to use, and today learned another use of the first set of API way, let people great satisfaction, paste code
Example is a select tag that supports dynamic property settings
1. Write Label class

public class Selecttaghandler extends Simpletagsupport implements Dynamicattributes {
private static final String attr_template = "%s= '%s '";
private static final String option_template = "<option value= '%1 $ S ' >%1$s</option>";
Private List optionslist;
private String name;
private String size;
Private map<string, object> tagattrs = new hashmap<string, object> ();

public void SetName (String name) {
THIS.name = name;
}

public void SetSize (String size) {
this.size = size;
}

public void Setoptionslist (List optionslist) {
This.optionslist = optionslist;
}

@Override
public void Dotag () throws Jspexception, IOException {
PageContext PageContext = (PageContext) getjspcontext ();
JspWriter out = Pagecontext.getout ();
Out.print ("<select");
Out.print (String.Format (attr_template, "name", THIS.name));
Out.print (String.Format (attr_template, "size", this.size));
For (String AttrName:tagAttrs.keySet ()) {
String attrdefinition = String.Format (Attr_template, Attrname, Tagattrs.get (attrname));
Out.print (attrdefinition);
}
Out.print (">");

for (Object option:this.optionsList) {

String Optiontag = String.Format (Option_template, option.tostring ());
Out.println (Optiontag);
}
Out.println ("</select>");
}

@Override
public void Setdynamicattribute (string uri, string name, Object value) throws Jspexception {
Tagattrs.put (name, value);
}
}
See, the code is so concise, dynamic property configuration is also very convenient, do not write n multiple setter and getter methods. 2. write TLD file Webroot/tld/select.tld

<?xml version= "1.0" encoding= "UTF-8"?>

<taglib xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
Xmlns:xsi= "Http://www.w3g.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
version= "2.0" >
<tlib-version>1.2</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>forms taglib</short-name>
<uri>http://hi.baidu.com/tags/forms</uri>
<description>
An example tab library of replacements for the HTML form tags.
</description>

<tag>
<name>select</name>
<tag-class>com.baidu.hi.tag.SelectTagHandler</tag-class>
<body-content>empty</body-content>

<attribute>
<name>optionsList</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.util.List</type>
</attribute>

<attribute>
<name>name</name>
<required>true</required>
</attribute>

<attribute>
<name>size</name>
<required>true</required>
</attribute>

<dynamic-attributes>true</dynamic-attributes>
</tag>
</taglib>
3. Use in JSP

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<%@ page import= "java.util.*"%>
<%@ taglib prefix= "formtags" uri= "/tld/select.tld"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<% @page  import= "Java.util.ArrayList"%><HTML>
<meta http-equiv= " Content-type "content=" text/html; Charset=utf-8 ""
<title>insert title Here</title>
<body>
<%
 List<String> colorlist = new arraylist<string> ();
    Colorlist.add ("Red");
    colorlist.add ("Blue");
    Colorlist.add ("white");
    request.setattribute ("ColorList", colorlist);
%>
<form action= "" method= "post"
 <formtags:select name= "Color" size= "1" optionslist= "$ {requestscope.colorlist} " style=" width:140px/>
</form>
</body>

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.