Springboot Integrated Freemarker Custom tags gets the data from the dictionary table

Source: Internet
Author: User

Because in the front-end to the data in the dictionary table to convert 1, 2 of these values to the corresponding text interpretation 1. First create a class to implementTemplatedirectivemodel class
@Component Public classDictdirectiveImplementsTemplatedirectivemodel {@Override Public voidExecute (Environment environment, map map, templatemodel[] templatemodels, Templatedirectivebody templatedirectivebody )throwstemplateexception, IOException {defaultobjectwrapperbuilder builder=NewDefaultobjectwrapperbuilder (configuration.version_2_3_25); if(Map.containskey ("type") && map.get ("type")! =NULL) {String type= Map.get ("type"). toString (); List<Dict> dictlist =dictutils.getdictlist (type); if(Map.containskey ("value") && map.get ("value")! =NULL) {String value= Map.get ("Value"). toString (); Dict Dict=NULL;  for(Dict dict1:dictlist) {if(Value.equals (Dict1.getvalue (). toString ())) {Dict=Dict1; }} environment.setvariable ("Dict", Builder.build (). Wrap (dict)); }Else{environment.setvariable ("Dictlist", Builder.build (). Wrap (dictlist)); }        }        if(templatedirectivebody!=NULL) {Templatedirectivebody.render (Environment.getout ()); }    }} 

2. Create a configuration class
@Component  Public class freemarkerconfig {    @Autowired    private  configuration configuration;    @Autowired    private  dictdirective dictdirective;    @PostConstruct    publicvoidthrows  templatemodelexception {        Configuration.setsharedvariable ("Dict_tag", dictdirective);    }}
Then you can call it on the page.
<type= "News_source"  value= "${news.source}">                         ${dict.label}                    </@dict_tag>

The front-end can be arbitrarily passed parameters, such as type, value, all the passed parameters will be stored in the map, the background directly to fetch it.

Springboot Integrated Freemarker Custom tags gets the data from the dictionary table

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.