Java JSP Custom Tags

Source: Internet
Author: User
Tags html form tld

To: http://blog.csdn.net/jiangwei0910410003/article/details/23915373

http://blog.csdn.net/jiangwei0910410003/article/details/23915373

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

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= "optionslist" 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>
Note here that the tag file can only be placed in the following location:
1.web-inf/tags
Sub-directories for 2.web-inf/tags
Meta-inf/tags of jar packages in 3.web-inf/lib
Subdirectories under Meta-inf/tags for jar packages in 4.web-inf/lib
The tag file in the 5.jar package requires a TLD
Add Jstl.jar and Standard.jar to the Web-inf/lib directory, and one point is the red part: Do not use http://java.sun.com/jstl/core this URL, Otherwise, the Item property in foreach is reported as having a problem

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 Labels

JSP tags have two sets of APIs
Jsptag->simpletag->simpletagsupport
Jsptag->tag->iterationtag->bodytag
The second group is classic, relatively early use of the way, doStartTag (), Doendtag () has n multiple return value of the kind, and really inconvenient to use, today learned another use of the first set of API way, let people great satisfaction, stickers
Example is a select tag that supports dynamic property settings
1. Writing label classes

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 no, the code is so concise, dynamic property configuration is also very convenient, do not write n multiple setter and getter method.

2. Writing TLD files 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"%><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>

Java JSP Custom Tags

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.