Customizing a tag mainly includes three steps:
1. Write a java class and inherit the TagSupport class;
2. Create a tld file and allud to the tag name and tag java class;
3. The jsp page introduces tld.
Example: Custom drop-down box label
If there is a drop-down selection box on the page, the best solution is usually to use a data dictionary, as there are potential for multiple pages
Use the same drop-down box to facilitate unified maintenance in the background.
Custom Tag class
import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.TagSupport;public class DictionaryOptionTaget extends TagSupport {private static final long serialVersionUID = 1L;private String index; // Field index, the value passed back through the tag attribute on the page @SuppressWarnings("unchecked")@Overridepublic int doEndTag() throws JspException {JspWriter jspw = this.pageContext.getOut();StringBuffer options = new StringBuffer();/*** You need to query the option whose database field index is SEX, here is to write it dead*/if ("SEX".equals(index)) {options.append("<option value=''>-Please select-</option>");options.append("<option value='1'>Male</option>");options.append("<option value='0'>Female</option>");}try {jspw.println(options); //Output} catch (IOException e) {e.printStackTrace();}return 0;}@Overridepublic int doStartTag() throws JspException {return 0;}public String getIndex() {return index;}public void setIndex(String index) {this.index = index;}}Define tld
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE taglibPUBLIC "-//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><jsp-version>1.2</jsp-version><short-name>tagSample</short-name><uri>/hellotag</uri><tag><!-- Check out an option list from the data dictionary --><name>OptionDictionary</name><tag-class>com.itmyhome.DictionaryOptionTaget</tag-class><body-content>empty</body-content><attribute><name>index</name><!-- Field index name--><required>true</required><!-- Whether it is required-><rtexprvalue>false</rtexprvalue><!-- Whether it can pass the value in ${}--></attribute></taglib>It should be noted that: when <rtexprvalue>true</rtexprvalue>, you can use JSP expressions
The attribute value representing the custom tag can be dynamically passed in ${}.
Use custom tags
<%@ taglib uri="/WEB-TAG/platForm.tld" prefix="PF"%> <select><PF:OptionDictionary index="SEX"/></select>
Page output:
Struts2 tag-two ways to write drop-down list
The first way to write
<s:set name="selList" value="# {'1':'Quality','2':'Cost','3':'Progress'}"></s:set><s:select list="#selList" listKey="key" listValue="value" name="columnName" headerKey="0" headerValue="--Please select--"></s:select>The second way of writing:
<s:select name="columnName" list="{'Quality','Cost','Progress'}" headerKey="-1" headerValue="--Please select --" emptyOption="true" multiple="false"/>