JSP custom tag implementation data dictionary instance, jsp instance
Example of Using JSP custom tags to implement data dictionary
1. The benefits of JSP labels are no longer arrogant.
The data dictionary is a drop-down box. The available content of the dictionary is displayed as long as the dictionary is defined.
When displaying a dictionary, you only need to define the dictionary and attribute value to display the dictionary display value.
2. first define the reference for custom tag loading in web. xml. The two attributes are the referenced URI and the loading path respectively.
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <jsp-config> <taglib> <taglib-uri>/tld/web-html</taglib-uri> <taglib-location> /WEB-INF/tlds/web-html.tld </taglib-location> </taglib> </jsp-config> </web-app>
3. define your own labels in the web-html.tld. We need a tag library and three labels for the data dictionary application. They are, select tag, options tag, and real data dictionary tag. Each tag corresponds to a different implementation class.
<? 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-jsptaglibrary_1_2.dtd "> <Taglib> <tlib-version> 1.0 </tlib-version> <! -- Tag library version --> <jsp-version> 1.2 </jsp-version> <! -- JSP standard version required by the tag library --> <short-name> html </short-name> <! -- The JSP page writing tool can be used to create an optional name for the nickname --> <tag> <name> select </name> <tag-class> com. selectTag </tag-class> <body-content> JSP </body-content> <attribute> <name> name </name> <rtexprvalue> true </rtexprvalue> </ attribute> <name> style </name> <rtexprvalue> true </rtexprvalue> </attribute> </tag> <name> options </name> <tag-class> com. optionsTag </tag-class> <body-content> JSP </body-content> <attribute> <name> collection </name> <rtexprvalue> true </rtexprvalue> </ attribute> </tag> <name> selectDisplay </name> <tag-class> com. selectDisplay </tag-class> <body-content> JSP </body-content> <attribute> <name> collection </name> <rtexprvalue> true </rtexprvalue> </ attribute> <name> name </name> <rtexprvalue> true </rtexprvalue> </attribute> <name> value </name> <rtexprvalue> true </rtexprvalue> </attribute> </tag> </taglib>
4. Implementation class
The role of the Implementation class is to splice the HTML Tag content required in the background, and then output by JSP
Two main methods of implementation class: one is output when this label starts, and the other is output at the end.
If you need to define attributes, you can refer to implementation class definition attributes and define them in TLD. This attribute can be obtained by using the shortcut key when using tags in JSP.
The first is the code of the select Tag:
Package com; import java. io. IOException; import javax. servlet. jsp. jspException; import javax. servlet. jsp. jspTagException; import javax. servlet. jsp. tagext. bodyTagSupport;/*** difference between TagSupport and BodyTagSupport: * it mainly depends on whether the tag processing class needs to read the content of the TAG body and change the content returned by the TAG body. If you do not need it, use TagSupport, otherwise, you can use BodyTagSupport * to implement tags implemented by TagSupport, because BodyTagSupport inherits TagSupport */@ SuppressWarnings ("serial") public class SelectTag Extends BodyTagSupport {@ Override public int doStartTag () throws JspException {try {StringBuffer results = new StringBuffer ("<select"); if (name! = Null) {results. append ("name = \" "); results. append (name); results. append (" \ "");} if (style! = Null) {results. append ("style = \" "); results. append (style); results. append ("\" ");} results. append (">"); pageContext. getOut (). write (results. toString ();} catch (IOException ex) {throw new JspTagException ("error");} return EVAL_BODY_INCLUDE ;}@ Override public int doEndTag () throws JspException {try {StringBuffer results = new StringBuffer (""); // because the drop-down list contains the drop-down content, the select end results can only be written when the end label is encountered. append ("</select>"); pageContext. getOut (). write (results. toString ();} catch (IOException ex) {throw new JspTagException ("error");} return EVAL_PAGE;} // style protected String style; // name protected String name; public String getStyle () {return style;} public void setStyle (String style) {this. style = style;} public String getName () {return name;} public void setName (String name) {this. the name = name;}/** doStartTag () method is a method called when a tag starts. The valid return value is EVAL_BODY_INCLUDE and SKIP_BODY. The former indicates that text between tags is displayed, the latter indicates that the text doEndTag () between labels is not displayed. It is called when a Tag ends. The valid return value is EVAL_PAGE and SKIP_PAGE, the former indicates that the following JSP page is executed after the tag is processed, and the latter indicates that the following JSP page doAfterBody () is not processed. This method is called after the text between the tags is displayed, the returned values include EVAL_BODY_AGAIN and SKIP_BODY. The former will display the text between tags again, and the latter will continue to execute the next EVAL_BODY_INCLUDE: Read the Body into the existing output stream, doStartTag () the function can use EVAL_PAGE: To continue processing the page. The doEndTag () function can use SKIP_BODY: to ignore the processing of the Body, and the doStartTag () and doAfterBody () functions can use SKIP_PAGE: to ignore the processing of the remaining page, doEndTag () function available EVAL_BODY_BUFFERED: applies for a buffer. The BodyContent object obtained by setBodyContent () function processes the tag body. If the class implements BodyTag, doStartTag () is available, otherwise, invalid EVAL_BODY_AGAIN: The request continues to process the body and returns the self-doAfterBody (). The returned value is useful when you create a loop tag. the predefined processing order is: doStartTag (), and SKIP_BODY is returned, doAfterBodyTag () returns SKIP_BODY, doEndTag () returns EVAL_PAGE. If TagSupport is inherited and no method is rewritten, the execution sequence of tag processing is: doStartTag () -> do not show text-> doEndTag ()-> execute the following web page. If doStartTag () is rewritten, the return value must be specified. If EVAL_BODY_INCLUDE is specified, the execution sequence is: doStartTag ()-> display text-> doAfterBodyTag ()-> doEndTag ()-> execute the following webpage */}
You can return specific numbers for the returned parameters.
Then the drop-down content implementation class
Package com; import java. io. IOException; import javax. servlet. jsp. jspException; import javax. servlet. jsp. jspTagException; import javax. servlet. jsp. tagext. bodyTagSupport; @ SuppressWarnings ("serial") public class OptionsTag extends BodyTagSupport {@ Override public int doStartTag () throws JspException {return EVAL_BODY_INCLUDE;} @ Override public int doEndTag () throws JspException {try {StringBuffer results = new StringBuffer (""); if ("SEX ". equals (collection) {results. append ("<option value = \" 0 \ "selected = \" selected \ "> select </option>"); results. append ("<option value = \" 1 \ "> male </option>"); results. append ("<option value = \" 2 \ "> female </option>");} pageContext. getOut (). write (results. toString ();} catch (IOException ex) {throw new JspTagException ("error");} return EVAL_PAGE;} // collection only passes an identifier, whether the specific drop-down value content is obtained from the database or from the request for different specific implementations of protected String collection; public String getCollection () {return collection;} public void setCollection (String collection) {this. collection = collection ;}}
You can customize how to store and query Dictionary data from the database.
Display label implementation. In order to get the TAG content value on the page in the future, we define a hidden field to save the attribute value, and then display the content
Package com; import java. io. IOException; import javax. servlet. jsp. jspException; import javax. servlet. jsp. jspTagException; import javax. servlet. jsp. tagext. bodyTagSupport; @ SuppressWarnings ("serial") public class SelectDisplay extends BodyTagSupport {@ Override public int doStartTag () throws JspException {try {StringBuffer results = new StringBuffer (""); pageContext. getOut (). write (results. toString ();} catch (IOException ex) {throw new JspTagException ("error");} return EVAL_BODY_INCLUDE ;}@ Override public int doEndTag () throws JspException {try {StringBuffer results = new StringBuffer (""); if ("SEX ". equals (collection) {results. append ("<span>"); results. append ("<input type = \" "); results. append ("hidden \" name = \ ""); results. append (getName (); results. append ("\" "); results. append ("value = \" "); results. append (getValue (); results. append ("\"> "); if (" 1 ". equals (getValue () {results. append ("male");} else if ("2 ". equals (getValue () {results. append ("female");} else {results. append ("select");} results. append ("</span>");} pageContext. getOut (). write (results. toString ();} catch (IOException ex) {throw new JspTagException ("error");} return EVAL_PAGE;} // collection only passes an identifier, whether the specific drop-down values are obtained from the database or from the request for different implementations of protected String collection; // The passed value protected String value; // The name of this attribute protected String name; public String getCollection () {return collection;} public void setCollection (String collection) {this. collection = collection;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getValue () {return value;} public void setValue (String value) {this. value = value ;}}
5. Reference in JSP directly in index. jsp
The corresponding TAG content needs to be introduced, which is referenced in the JSP Header
The tag attributes can be set or not set. The tag usage is the same as that of the HTML tag. You can define the attribute.
<% @ Page language = "java" pageEncoding = "UTF-8" %> <% @ taglib uri = "/tld/web-html" prefix = "html" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
6. Later
Access the project to see the effect. The attachment is the source code of the project. Import it to MyEclipse to view the effect.
If you want to design a large tag library, you can design a parent class that contains some main attributes, such as name, id, style and so on. Then define your own special attributes in the subclass.
This implementation only describes the HelloWorld program used by JSP custom tags, and then contains the actual examples of dictionary applications. The program is simple and only for reference.
If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!