About JSP The benefits of labels are no longer wordy
The data dictionary is the drop-down box used , As long as you define which dictionary to use, the available contents of this dictionary will be displayed
When displaying a dictionary, you can display the displayed value of the dictionary by defining the dictionary and attribute value
First, in the web.xml Define the reference of custom label loading in , The two attributes are referenced URI And load path
<?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>
stay web-html.tld Define your own label in , For data dictionary application, we need a tag library , Three labels . Namely ,select label ,options label , And the label of the real data dictionary , 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> <!-- Required by the tag library JSP Specification version --> <short-name>html</short-name> <!-- JSP Page authoring tools can be used to create optional names for helpers --> <tag> <name>select</name> <tag-class>com.SelectTag</tag-class> <body-content>JSP</body-content> <attribute> <name>name</name> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>style</name> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>options</name> <tag-class>com.OptionsTag</tag-class> <body-content>JSP</body-content> <attribute> <name>collection</name> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>selectDisplay</name> <tag-class>com.SelectDisplay</tag-class> <body-content>JSP</body-content> <attribute> <name>collection</name> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>name</name> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>value</name> <rtexprvalue>true</rtexprvalue> </attribute> </tag></taglib>
Implementation class
The function of the implementation class is to splice the required in the background HTML Label content , Then from JSP For the output
The two main methods of implementing classes , When one encounters this tag, it outputs , One is output at the end
If you need to define attributes , You can refer to the implementation class to define attributes , And in TLD In the definition of , stay JSP When using the tag in, the shortcut key can show this attribute
First of all select Code of label :
package com;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspTagException;import javax.servlet.jsp.tagext.BodyTagSupport;/** * TagSupport And BodyTagSupport The difference between : * It mainly depends on whether the label processing class wants to read the content of the label body and change the content returned by the label body , Use it if you don't need it TagSupport, Otherwise it will be used. BodyTagSupport * use TagSupport Implemented tags , Both can be used. BodyTagSupport To achieve , because BodyTagSupport Inherited 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 contains drop-down content , So you can only write when you encounter the end tag select end results.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.name = name; } /** doStartTag() Method is the method that will be called at the beginning of the tag , Its legal return value is EVAL_BODY_INCLUDE And SKIP_BODY, The former indicates that the text between labels will be displayed , The latter means that the text between labels is not displayed doEndTag() Method is the method called at the end of the tag , Its legal return value is EVAL_PAGE And SKIP_PAGE, The former means that after processing the label, continue to execute the following JSP Webpage , The latter means not to process the following JSP Webpage doAfterBody(), This method is called after displaying the text between tags , The return values are EVAL_BODY_AGAIN And SKIP_BODY, The former will display the text between labels again , The latter proceeds to the next step of label processing EVAL_BODY_INCLUDE: hold Body Read into the existing output stream ,doStartTag() Functions are available EVAL_PAGE: Continue processing the page ,doEndTag() Functions are available SKIP_BODY: Ignore right Body To deal with ,doStartTag() and doAfterBody() Functions are available SKIP_PAGE: Ignore the processing of the remaining pages ,doEndTag() Functions are available EVAL_BODY_BUFFERED: Request buffer , from setBodyContent() Function to get BodyContent Object to handle tag Of body, If the class implements BodyTag, that doStartTag() You can use , Otherwise it's illegal EVAL_BODY_AGAIN: Request to continue processing body, Return from doAfterBody(), This return value is in the loop you make tag It is very useful when The predetermined processing order is :doStartTag() return SKIP_BODY,doAfterBodyTag() return SKIP_BODY,doEndTag() return EVAL_PAGE If inherited TagSupport after , If there is no way to rewrite , The execution order of label processing is :doStartTag() -> Don't show text ->doEndTag()-> Execute the next page If you rewrite doStartTag(), The return value must be specified , If you specify EVAL_BODY_INCLUDE, Then the order of execution is :doStartTag()-> According to the text ->doAfterBodyTag()->doEndTag()-> Execute the following page */}
About return parameters , You can also return specific numbers , Don't worry too much
Then there is 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\"> Please select </option>"); results.append("<option value=\"1\"> male </option>"); results.append("<option value=\"2\"> Woman </option>"); } pageContext.getOut().write(results.toString()); } catch (IOException ex) { throw new JspTagException(" error "); } return EVAL_PAGE; } // collection Just pass a logo , Whether the specific drop-down value content is obtained from the database or from the request is a different specific implementation protected String collection; public String getCollection() { return collection; } public void setCollection(String collection) { this.collection = collection; }}
How to store and query your dictionary data from the database , You can customize the implementation
Displayed label implementation , In order to get the tag content value on the page in the future , We define hidden fields to store attribute values , 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(" Woman "); } else { results.append(" Please select "); } results.append("</span>"); } pageContext.getOut().write(results.toString()); } catch (IOException ex) { throw new JspTagException(" error "); } return EVAL_PAGE; } // collection Just pass a logo , Whether the specific drop-down value content is obtained from the database or from the request is a different specific implementation protected String collection; // Value delivered protected String value; // Name of the 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; }}
JSP I quote , Directly in index.jsp I quote
The corresponding label content needs to be introduced , The way of introduction is JSP Head quote
The properties of the tag can be set or not , The use of labels and HTML The use of labels is the same , Define attributes
<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="/tld/web-html" prefix="html"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>JSP Implementation of custom label </title> </head> <body> Please select : <html:select name="sex" style="width:100px"> <html:options collection="SEX"></html:options> </html:select> Show gender : <html:selectDisplay collection="SEX" value="1" name="sex"></html:selectDisplay> </body></html>
The latter
Visit the project to see the effect
If you want to design a large tag library by yourself , You can design a parent class , Contains some main properties , for example name,id,style Equal attribute . Then define your own unique attributes in subclasses
This implementation is just for learning JSP Custom labels are used HelloWorld Program , Then it includes practical examples of dictionary application , The procedure is simple , For reference only
I recommend you to read more about “ jsp Custom tag The data dictionary ” The article