Struts2 Custom dropdown box tag label _java

Source: Internet
Author: User
Tags tld stringbuffer

A custom label consists of three main steps:

1, the writing Java class, inherits the TagSupport class;

2, the creation of TLD files, mapping signed and tagged Java class;

3, JSP page introduced TLD.

Example: Customizing the dropdown box label

If you have a Drop-down selection box on the page, the best solution is to use a data dictionary, because it's possible that multiple pages

Use the same Drop-down box for easy background maintenance.

Custom Tag Class

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 in Dex Field index, the value passed back by the Label property on the page @SuppressWarnings ("unchecked") @Override public int Doendtag () throws jspexception {JspWriter jsp
W = This.pageContext.getOut ();
StringBuffer options = new StringBuffer (); /** * Need to query database field index for SEX option content, here is write dead/if ("SEX". Equals (Index)) {options.append ("<option value=" >-Please select-</
Option> ");
Options.append ("<option value= ' 1 ' > Male </option>");
Options.append ("<option value= ' 0 ' > </option>");
try {jspw.println (options);//output} catch (IOException e) {e.printstacktrace ();} return 0; @Override public int doStartTag () throws jspexception {return 0;} public String GetIndex () {return index; ID setindex (String index) {this.index = index;}}

Define TLD

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE taglib public
"-//sun Microsystems, Inc.//dtd JSP Tag Library 1.2//en"
" Web-jsptaglibrary_1_2.dtd ">
< jsp-version>1.2</jsp-version>
<uri>/ hellotag</uri>
<tag><!--Check out an option list from the data dictionary-->
<name>optiondictionary</name >
< body-content>empty</body-content>
<name>index</name><!-- Field index name-->
<required>true</required><!--are required-->
<rtexprvalue>false</ rtexprvalue><!--whether the ${} mode can be passed-->

Note that when:<rtexprvalue>true</rtexprvalue>, you can use a JSP expression

The property value representing the custom label can be dynamically passed by using the ${} method.

Use a custom Label

<%@ taglib uri= "/web-tag/platform.tld" prefix= "PF"%> 
<pf:optiondictionary index= " SEX "/>

Page output:

STRUTS2 tags-two ways to spell a drop-down list

The first form of writing

<s:set name= "Sellist" value= "# {' 1 ': ' Quality ', ' 2 ': ' Cost ', ' 3 ': ' Progress '} ' ></s:set>
<s:select" #selList " listkey= "Key" listvalue= "value" Name= "ColumnName" headerkey= "0" headervalue= "--Please Choose--" ></s:select>

The second way:

<s:select name= "ColumnName" list= "{' Quality ', ' cost ', ' progress '}" headerkey= "-1" headervalue= "--Please select-" emptyoption= "true" Multiple= "false"/>

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: 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.