JSP Custom Select Tag Dictionary table data __javase/javaee

Source: Internet
Author: User
Tags stringbuffer

In the development process, it is often used repeatedly in the Select Load Dictionary table data, such as loading jobs, education and so on, each time to use a separate acquisition and then traverse the front end will be cumbersome, and inconsistent with development principles. Therefore, you can encapsulate it by customizing the label. Examples are as follows:

Ynbytag.tld

<?xml version= "1.0" encoding= "UTF-8"?> <taglib xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= "HTT" P://www.w3.org/2001/xmlschema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http:// Java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd "version=" 2.1 "> <description>ynbyUniform Custom Label</description> <display-name>ynby core</display-name> <tlib-version>1.1</tlib-version&
    Gt <short-name>ynby</short-name> <tag> <name>demo</name> <tag-class> Com.rdpm.sysm.service.taglib.demotag</tag-class> <body-content>empty</body-content> </tag > <tag> <!--define the name of the property. The attribute name must be unique for each label. --> <name>selectTag</name> <tag-class>com.rdpm.sysm.service.taglib.dictselecttag</
            Tag-class> <body-content>empty</body-content> <!--system code--> <attribute> <name>systemCode</name> <!--Specify whether the property is mandatory or optional, if set to false as optional. --> <required>true</required> <!--declares that the Label property is valid when an expression is run. --> <rtexprvalue>true</rtexprvalue> </attribute> <!--Select's name value--&G
      T  <attribute> <name>selectName</name> <!--Specify whether the property is mandatory or optional, if set to false as optional. --> <required>true</required> <!--declares that the Label property is valid when an expression is run.
        --> <rtexprvalue>true</rtexprvalue> </attribute> <!--value selected-->
            <attribute> <name>value</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <!--default selected value--> <a
            Ttribute> <name>selectedValue</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <!--The method that is triggered when selecting any item--> <
            Attribute> <name>onChange</name> <required>false</required> <rtexprvalue>true</rtexprvalue>;/attribute> <!--selection mode Radio/multiple selection--> <attribute> <name>multiple</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attr Ibute> <!--Select is not selected when the display content such as: "Please select" or "All"--> <attribute> &LT;NAME&GT;NULLNAME&L
        T;/name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <!--Select the location at the top or bottom of the selection when--> <attribute> <name&gt ;nullnameplace</name> <required>false</required> <rtexprvalue>true</rte Xprvalue> </attribute> <!--have the next level--> <attribute> <name>hasn Ext</name> <required>false</required> <rtexprvalue>true</rtexprvalue&gt
        ; </attribute> <!--superior id--> <attribute> <name>parentId</name> <re
        Quired>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <!--additional display layer parameters--> <!--style class--> <attribute> <name>cssclass</name&gt
            ;
        <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <!--inline style--> <attribute> <name>styleClass</name> <requir ed>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </t
 Ag> </taglib>

Dictselecttag.java
Package com.rdpm.sysm.service.taglib;
Import Com.rdpm.sysm.dao.BaseParameterMapper;
Import Com.rdpm.sysm.entity.BaseParameter;
Import Com.rdpm.sysm.util.SpringContextUtil;
Import Org.apache.commons.lang3.StringUtils;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.context.ApplicationContext;

Import Org.springframework.stereotype.Service;
Import Javax.annotation.Resource;
Import javax.servlet.jsp.JspException;
Import Javax.servlet.jsp.JspWriter;
Import Javax.servlet.jsp.tagext.TagSupport;
Import java.io.IOException;

Import java.util.List;
 /** * Created by XJM on 2016-07-12. */public class Dictselecttag extends TagSupport {//extra some display layer parameters private StringCssClass;//Style class private StringStyleClass;//inline style private StringSystemcode;//Parameter system code private StringSelectname;//select's name value is private StringNullname;//Prompt message option when not selected private Stringvalue;/The value selected is private StringSelectedValue;/The value selected is private StringOnChange;//The method called when changing private Stringmultiple//Select multiple selection/radio @Override public int Doendtag () throws Jspexception {
Springcontextutil at the bottom baseparametermapper baseparametermapper= (baseparametermapper) SpringContextUtil.getBean (
        "Baseparametermapper"); List<baseparameter> list= Baseparametermapper.selectparambyparamtypecode (Systemcode);
        StringBuffer sb = new StringBuffer (); JspWriter out =PageContext. Getout ();
        Sb.append ("<select name=\" "+this.getselectname () +" "); if (!
        Stringutils.isempty (This.getcssclass ())) {Sb.append ("class=\" "+ this.getcssclass () +" "); } if (!
        Stringutils.isempty (This.getstyleclass ())) {Sb.append ("style=\" "+ this.getstyleclass () +" "); } if (!
        Stringutils.isempty (This.getmultiple ())) {Sb.append ("multiple=\" "+ this.getmultiple () +" "); } if (!
        Stringutils.isempty (This.getonchange ())) {Sb.append ("onchange=\" "+ this.getonchange () +" ");

        } sb.append (">"); if (! Stringutils.isempty (This.getnullname ())) {Sb.append ("<option value=\" \ ">--" +this.getnullname () + "--</o
        Ption> ");
                for (Baseparameter dc:list) {if (Dc.getdictvalue (). Equals (This.getselectedvalue ())) {
            Sb.append ("<option value=\" "+dc.getdictvalue () +" \ "selected>");
      }else {          Sb.append ("<option value=\" "+dc.getdictvalue () +" ">");
        } sb.append (Dc.getdictname () + "</option>");
        } sb.append ("</select>");
        try {out.write (sb.tostring ());  catch (IOException e) {//TODO auto-generated Catch block throw new Jspexception (e); Return TagSupport.Eval_page; Public String Getcssclass () {returnCssClass; } public void Setcssclass (String cssClass) {this.CssClass= CssClass; Public String Getstyleclass () {returnStyleClass; } public void Setstyleclass (String styleClass) {this.StyleClass= StyleClass; Public String Getsystemcode () {returnSystemcode; } public void Setsystemcode (String systemcode) {this.Systemcode= Systemcode; Public String Getselectname () {returnSelectname; } public void Setselectname (String selectname) {this.Selectname= Selectname; Public String Getnullname () {returnNullname; } public void Setnullname (STR
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.