JAVA Freemarker (6)--Custom directives

Source: Internet
Author: User

1. Create instruction Template

public class Roledirectivemodel implements Templatedirectivemodel {
    @Override public
    void execute (Environment Env, MAP params, templatemodel[] loopvars, templatedirectivebody body) throws Templateexception, IOException {
        Templatescalarmodel user = (Templatescalarmodel) params.get ("user");
        Templatescalarmodel role = (Templatescalarmodel) params.get ("role");
        Tom has Amdin permissions and lists the specific contents of the permission
        if ("Tom". Equals (User.getasstring ()) && "Amdin". Equals (Role.getasstring ()) ) {
           loopvars[0] = templatebooleanmodel.true;
        }
        and lists the specific contents of the permission
        list<string> otherrights = new arraylist<string> ();
        Otherrights.add ("Add");
        Otherrights.add ("delete");
        Otherrights.add ("Update");
        Otherrights.add ("Query");
        LOOPVARS[1] = new SimpleSequence (otherrights);

        Body.render (Env.getout ());
    }

2, configuring in Springmcv.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:p= "http://www.springframework.org/schema/p" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" Xmlns:contex t= "Http://www.springframework.org/schema/context" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc.xsd "> <mvc:annotation-driven/> <context:component-scan base-package=" Com.cloud.wysch "A" > <context:exclude-filter type= "annotation" expression= "Org.springframework.stereotype.Service"/> & Lt;/context:component-scan> <!--set Freemarker configuration filePath--> <bean id= "freemarkerconfiguration" class= " Org.springframework.beans.factory.config.PropertiesFactoryBean "> <property name=" Location "value=" Classpath : Conf/freemarker.properties "/> </bean> <!--Configure Freemarker template path--> <bean id=" Fmxmlescape "cl ass= "Freemarker.template.utility.XmlEscape"/> <bean id= "Roledirectivemodel" Com.cloud.common.freemarker.RoleDirectiveModel "/> <bean id=" freemarkerconfig "class=" Org.springframew Ork.web.servlet.view.freemarker.FreeMarkerConfigurer "> <property name=" freemarkersettings "ref=" Freemarkerc Onfiguration "/> <property name= templateloaderpath" value= "/web-inf/ftl/"/> <property name= " 
                 Freemarkervariables "> <map> <entry key=" Xml_escape "value-ref=" Fmxmlescape "/>"
     <!--role definition--> <entry key= "Roles" value-ref= "Roledirectivemodel"/>       </map> </property> </bean> <!--configuring Freemarker View parser--> <bean id= "vie WRESOLVERFTL "class=" Org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver "> <property nam E= "ContentType" value= "text/html;  Charset=utf-8 "/> <property name=" Cache "value=" true "/> <property" name= "suffix". html " /> <property name= "exposerequestattributes" value= "true"/> <property "name=" Ibutes "value=" true "/> <property name=" Exposespringmacrohelpers "value=" true "/> <property na Me= "Requestcontextattribute" value= "request"/> <property name= "order" value= "0"/><!--This familiar setting represents a feeling in multiple views conditions, the order of precedence matches.
        --> </bean> <bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/views/" ></property> <property name= "suffix" Value= ". jsp" ></property> <property name= "Viewclass" value= "Org.springframework.web.servlet.view.Inter Nalresourceview "/> <property name=" Order "value=" 1 "/> </bean> <bean id=" Multipartreso Lver "class=" Org.springframework.web.multipart.commons.CommonsMultipartResolver "> <property name=" maxupload Size "value=" 104857600 "/> <property name=" maxinmemorysize "value=" 4096 "/> <property name=" de Faultencoding "value=" UTF-8 "></property> </bean> <bean class=" org.springframework.web.servlet.h Andler. Simplemappingexceptionresolver > <!--define the default exception handling page that uses--> <property name= when registering for this exception type Defaulterro Rview "value=" error ></property> <!--define the name of the variable that the exception handling page uses to get the exception information, the default name is exception--> <property n 
        Ame= "Exceptionattribute" value= "Exception" ></property> <!--define exceptions that require special handling, with the class name or full pathname as the key, and the exception page name as the value--> <propeRty name= "Exceptionmappings" > <props> <prop key= "Java.lang.Exception" >error< /prop> <prop key= "java.lang.Throwable" >error</prop> </props> < /property> </bean> </beans>

3, go to the page controller

@Controller
@RequestMapping ("/freemark") public
class Freemarkcontroller extends Basecontroller {

    @ Requestmapping ("/role.action") public
    Modelandview role (HttpServletRequest request, HttpServletResponse Response) {return
        new Modelandview (' role ');
    }

4, page display

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

————————————————————————————————————————————————— – Java Architect Project Combat, high concurrency cluster distributed, large data high availability video tutorials, total 760G

Download Address:

https://item.taobao.com/item.htm?id=555888526201

01. High-level architect 42 Phases
02.Java Advanced System Training Architecture Course 148 hours
03.Java Senior Internet Architect Course
04.Java Internet Architecture Netty, Nio, Mina, etc.-Video tutorials
05.Java Advanced Architecture Design 2016 finishing-video tutorials
06. Architect Foundation, advanced film
07.Java Architect Required Linux Operation series courses
08.Java Advanced System Training Architecture Course 116 hours
+
Hadoop series tutorials, Java design patterns and data structures, Spring Cloud Micro service, Springboot Primer

—————————————————————————————————————————————————–

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.