Springmvc+freemarker Implement custom Label __java

Source: Internet
Author: User
Tags list of attributes
1. The goal is as part of our target page, because the class list is a part of each page that needs to be displayed, so the list of attributes is extracted, made into a custom label, and directly referencing the label on the page, you can directly show the
2. Write data to provide class, implement Templatedirectivemodel interface
Package com.dmall.mall.directive;
Import java.io.IOException; Import java.util.List; Import Java.util.Map;
Import org.springframework.beans.factory.annotation.Autowired; Import org.springframework.stereotype.Component;
Import Com.dmall.item.entity.Category; Import Com.dmall.item.service.CategoryService; Import com.google.common.base.Strings;
Import freemarker.core.Environment; Import Freemarker.template.TemplateDirectiveBody; Import freemarker.template.TemplateException; Import Freemarker.template.TemplateModel; Import freemarker.template.TemplateModelException; Import Freemarker.template.TemplateDirectiveModel; Import Freemarker.ext.beans.BeansWrapper; Import Freemarker.ext.beans.BeansWrapperBuilder; Import freemarker.template.Configuration; Import Freemarker.template.SimpleScalar; /** * @author wangxuzheng@aliyun.com * */@Component public class Categorytreedirective implements Templatedire ctivemodel{@Autowired private Categoryservice categoryservice; private Long getpid (map<?,? > params) throws templateexception{String id = getparam (params, "pid"); if (Strings.isnullorempty (ID)) {throw new templatemodelexception ("Do not specify to display the class amount PID attribute"); return long.valueof (ID); @Override public void execute (Environment env, MAP params, templatemodel[] loopvars, templatedirectivebody body) throws Templateexception, Ioexception {Long pid = getpid (params); list<category> children = Categoryservice.findchildren (PID); Env.setvariable ("Categories", Createbeanswrapper (). Wrap (children)); Body.render (Env.getout ()); Protected string GetParam (map<?,? > params, string name) {Object value = params.get (name); if (Value instanceof Si Mplescalar) {return (simplescalar) value). getasstring (); Public Beanswrapper Createbeanswrapper () {Return to New Beanswrapperbuilder (configuration.version_2_3_23). build (); 3. Place the custom label class in the Freemarker configuration variable <bean class= " Org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer "> <property name=" defaultencoding " Value= "Utf-8"/> <property name= "Freemarkervariables" > <map> <entry key= "Xml_escape" Fmxmlescape "/> <entry key=" links "value-ref=" links "/> <entry key=" Shiro "><bean class=" Com.jagregory.shiro.freemarker.ShiroTags "/></entry> <!--user Custom label--> <entry key=" CategoryTree "Value-ref= "categorytreedirective" ></entry> </map> </property> <property name= " Templateloaderpath "value="/web-inf/views "/> <property name=" freemarkersettings "ref=" Freemarkerconfiguration "/> </bean> 3. Use a custom label on the page here, we separate each custom label FTL into a separate file so that you can include it in multiple pages, as shown in

Contents of CATEGORY_TREE.FTL File: <!--BEGIN SIDEBAR--> <div class= "SIDEBAR col-md-3 col-sm-4" > <ul class= "list-gr" OUP margin-bottom-25 Sidebar-menu "> < @categoryTree pid=" 1 "> < #list categories as category> < #if category.haschild==1> <li class= "List-group-item clearfix dropdown" > <a href= "javascript:void (0);" > <i class= "fa fa-angle-right" ></i> ${category.name} <i class= "fa fa-angle-down" ></i> </ a> <ul class= "Dropdown-menu" > < @categoryTree pid= "${category.id}" > < #list categories as c> <li& Gt;<a href= "product-list.html" ><i class= "fa fa-circle" ></i> ${c.name}</a></li> </# list> </@categoryTree > </ul> </li> < #else > <li class= "List-group-item clearfix" >< A href= "product-list.html" ><i class= "fa fa-angle-right" ></i> ${category.name}</a></li> </#if > </#list > </@categoryTree > </div> <!--End SIDEBAR--> where @categorytree is the key that we defined in the Freemarker configuration in the previous step, the Freemarker is executed using the component handler class corresponding to the key when parsing. Custom components also support passing parameters, in this case we want to show a tree menu, so we want to pass the Superior menu ID (PID), the component processing class after processing, through the env.setvariable ("categories", Createbeanswrapper (). Wrap (children)); Place the results in the template rendering context so that the processing results provided by the component can be used within the @categorytree tag.

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.