Implementing a drop-down box with a data dictionary through a custom label

Source: Internet
Author: User
Tags tld

When the page needs to use more than one drop-down box, the data dictionary combined with the custom label can bring us a lot of convenience. Custom labels can be reused, equivalent to a framework that simplifies our code.

Here's a summary of my routines. First, the database tables are as follows:

Then there must be a way to query the field, below I have my Code (folding form) attached, for your reference only.

 Public Interface Idictionarymanager {    List<Dictionary> dictionaries (String groupvalue);     void adddatatomemory ();    Dictionary Dictionary (int  itemkey, String groupvalue);}
Idictionarymanager
//@Component Because The @PostConstruct would load data to Dictionary, so don ' t add the @Component (would load the Times) 
     Public classDefaultdictionarymanagerImplementsIdictionarymanager {Private Static FinalLogger Looger = Loggerfactory.getlogger (Defaultdictionarymanager.class); //Logger Info    Private Static FinalString logger_info_load_dic = "Loading Dictionary to cache."; Private Static FinalString Logger_info_cache_with_map_group_by_groupkey = "CACHE The dictionary with MAP GROUP by Groupkey."; Private Static FinalString logger_info_cache_with_groupkey_success = "CACHE The dictionary with map group by Groupkey SUCCESS."; //Logger Error    Private Static FinalString logger_error_no_dictionary_cached = "NO DICTIONARY CACHED in the map, please sure your Groupkey"; @AutowiredPrivateidictionaryrepository repository; Private Static Finalmap<string, list<dictionary>> CACHE =Maps.newhashmap (); Private StaticList<dictionary> dictionaries =NULL; Private StaticDefaultdictionarymanager Manager =NewDefaultdictionarymanager (); PrivateDefaultdictionarymanager () {} Public StaticDefaultdictionarymanager getinstance () {//determines whether an instance of manager is empty, if an instance is created empty, the part returns an instance without being empty. Optional<defaultdictionarymanager> Optional =optional.fromnullable (manager); if(!optional.ispresent ()) {            return NewDefaultdictionarymanager (); }        returnManager; } @PostConstruct Public voidInitcache () {dictionaries=Repository.selectall ();    Adddatatomemory (); }    /*** Get the value of the corresponding group in the data dictionary * *@paramGroupkey Groupkey in the data dictionary *@returnIf this group is present, returns the value in this group if it does not exist and returns an empty list*/     PublicList<dictionary>dictionaries (String groupkey) {List<Dictionary> dictionaries =Cache.get (Groupkey); Optional<List<Dictionary>> Optional =optional.fromnullable (dictionaries); if(!optional.ispresent ()) {Dictionaries=NewArraylist<dictionary>(); }        returndictionaries; }    //each set of data encapsulated in the list is repackaged into a different list by the Groupvalue category and then encapsulated in a map     Public voidadddatatomemory () { for(Dictionary dictionary:dictionaries) {String Groupvalue=Dictionary.getgroupvalue (); List<Dictionary> Groupdic =Cache.get (Groupvalue); Optional<List<Dictionary>> Optional =optional.fromnullable (groupdic); if(!optional.ispresent ()) {Groupdic=NewArraylist<dictionary>();            } groupdic.add (dictionary);        Cache.put (Groupvalue, groupdic); }    }     PublicDictionary Dictionary (intItemkey, String groupvalue) {List<Dictionary> dictionaries =Cache.get (Groupvalue);  for(Dictionary dictionary:dictionaries) {if(Dictionary.getitemkey () = =Itemkey) {                returndictionary; }        }        return NewDictionary (); }}
Defaultdictionarymanager

Now with the background support, you can do the JSP operation. In this I have customized the dicselect tag so that I can use a custom label every time I use the dropdown box, eliminating the step of repeating the drop-down box. Here you have to say. Tag and. tld files. The former belongs to the JSP file and the latter is the XML file.

1. For tag files
<%@ taglib prefix= "UI" Tagdir= "/web-inf/tags"%>
The tags are a directory, there are several tag files.
However, when using <ti:XXXX>, directory web-inf/tags, there must be a xxxx.tag file corresponding to it.

2. for TLD files
A TLD file can be referenced in a JSP, such as
<%@ taglib uri= "http://struts.apache.org/tags-html" prefix= "html"%>
But what does this http://struts.apache.org/tags-html correspond to?
The JSP will find all TLD files under the \web-inf of the current directory, confirming which TLD file the URL corresponds to.
When the Struts-html.tld file is found, it is found to correspond to the URL within it.
However, when using

When the user uses the label in the JSP page, the system will first go to the "Taglib-uri" and "taglib-location" in the <taglib> tag in the Web. xml file to find the corresponding extension to the TLD file, and then The mappings in the TLD file then find the corresponding Taglib class.

Below is the attached my configuration code:

<% @tag pageencoding= "UTF-8"%><%@ taglib prefix= "dict" uri= "Http://www.lgb.com/tags/dic"%><%@ Attribute name= "id" type= "java.lang.String" required= "false"%><%@ attribute name= "name" type= "java.lang.String "Required=" true "%><%@ attribute Name=" key "type=" Java.lang.String "required=" true "%><%@ attribute Name=" Value "type=" Java.lang.String "required=" true "%><%@ taglib prefix=" C "uri=" Http://java.sun.com/jsp/jstl/core " %><%String tagId=ID; String TagName=name; if(id = =NULL) {tagId=TagName; } request.setattribute ("TagId", tagId); Request.setattribute ("TagName", tagName);%><selectclass= "Form-control" id= "${tagid}" Name= "${name}" > <c:foreach items= "${dict:list (Key)}" var= "Dict" varstatus= " Status > <c:choose> <c:when test= "${status.first eq true}" > <c:choose&                       Gt <c:when test= "${dict.itemkey eq value}" > <option selected= "selected" Value= "${dict.itemkey                           } ">${dict.itemValue}</option> </c:when> <c:otherwise> <option value= "${dict.itemkey}" >${dict.itemValue}</option> </                   c:otherwise> </c:choose> </c:when> <c:otherwise> <c:choose> <c:when test= "${dict.itemkey eq value}" > < Option selected= "selected" Value= "${dict.itemkey}" >${dict.itemValue}</option> </c:when&   Gt                    <c:otherwise> <option value= "${dict.itemkey}" >${dict.itemvalue} </option> </c:otherwise> </c:choose> </c:otherw Ise> </c:choose> </c:forEach></select>
Dicselect.tag
<?xml version= "1.0" encoding= "UTF-8"? ><taglib xmlns= "Http://java.sun.com/xml/ns/j2ee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd "version= "2.0" > <tlib-version>1.0</tlib-version> < Short-name>dictionarytag</ Short-name> <uri>http://www.lgb.com/tags/dic</uri><function> <name>list</name> <function-class>com.lgb.arc.web.tag.dictionarytag</function-class> <function-signature>java.util.list List (java.lang.String) </function-signature> </function&gt    ; <function> <name>show</name> <function-class>com.lgb.arc.web.tag.dictionarytag</function-class> <function-signature>java.lang.string Show (java.lang.String,int) </function-signature> </function></taglib>
Dictionary.tld
<% @tag pageencoding= "UTF-8"%><%@ taglib prefix= "dict" uri= "Http://www.lgb.com/tags/dic"%><%@ Attribute name= "Groupvalue" type= "java.lang.String" required= "true"%><%@ attribute name= "Itemkey" type= " Java.lang.Integer "required=" true "%><%@ taglib prefix=" C "uri=" Http://java.sun.com/jsp/jstl/core "%> ${ Dict:show (Groupvaule, Itemkey)}
Diclabel.tag
<% @tag pageencoding= "UTF-8"%><%@ taglib prefix= "dict" uri= "Http://www.lgb.com/tags/dic"%><%@ Attribute name= "Groupvalue" type= "java.lang.String" required= "true"%><%@ attribute name= "Itemkey" type= " Java.lang.Integer "required=" true "%><%@ taglib prefix=" C "uri=" Http://java.sun.com/jsp/jstl/core "%><td >${dict:show (Groupvalue, Itemkey)}</td>
Dictd.tag

And also configure it in the Web. xml file, with my configuration code attached below:

<jsp-config>
<taglib>
<taglib-uri>http://www.lgb.com/tags/dic</taglib-uri>
<taglib-location>/WEB-INF/tags/dictionary.tld</taglib-location>
</taglib>
</jsp-config>

This completes the custom label. We can use our custom tags in JSP pages.

(This code is for reference only)

Roar it! Youth!

Implementing a drop-down box with a data dictionary through a custom label

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.