JAVA Freemarker(6)--自定义指令

来源:互联网 发布:java图形用户界面 编辑:程序博客网 时间:2024/05/17 09:09

1、创建指令模版

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拥有amdin权限,并且列出权限的具体内容        if ("tom".equals(user.getAsString()) && "amdin".equals(role.getAsString())){           loopVars[0] = TemplateBooleanModel.TRUE;        }        //并且列出权限的具体内容        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、在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:context="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.wyscha">        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>    </context:component-scan>    <!-- 设置freeMarker的配置文件路径 -->    <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">        <property name="location" value="classpath:conf/freemarker.properties"/>    </bean>    <!-- 配置freeMarker的模板路径 -->    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />    <bean id="roleDirectiveModel" class="com.cloud.common.freemarker.RoleDirectiveModel" />    <bean id="freemarkerConfig"          class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">        <property name="freemarkerSettings" ref="freemarkerConfiguration" />        <property name="templateLoaderPath" value="/WEB-INF/ftl/" />        <property name="freemarkerVariables">            <map>                <entry key="xml_escape" value-ref="fmXmlEscape" />                 <!-- 角色定义 -->                <entry key="role" value-ref="roleDirectiveModel" />            </map>        </property>    </bean>    <!-- 配置freeMarker视图解析器 -->    <bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">        <property name="contentType" value="text/html; charset=utf-8" />        <property name="cache" value="true" />        <property name="suffix" value=".html" />        <property name="exposeRequestAttributes" value="true" />        <property name="exposeSessionAttributes" value="true" />        <property name="exposeSpringMacroHelpers" value="true" />        <property name="requestContextAttribute" value="request" />        <property name="order" value="0" /><!--这段熟悉设置代表了在多视图的情况下,优先匹配的顺序。-->    </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.InternalResourceView" />        <property name="order" value="1" />    </bean>    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">        <property name="maxUploadSize" value="104857600" />        <property name="maxInMemorySize" value="4096" />        <property name="defaultEncoding" value="UTF-8"></property>    </bean>    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">        <!-- 定义默认的异常处理页面,当该异常类型的注册时使用 -->        <property name="defaultErrorView" value="error"></property>        <!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception -->        <property name="exceptionAttribute" value="exception"></property>        <!-- 定义需要特殊处理的异常,用类名或完全路径名作为key,异常也页名作为值 -->        <property name="exceptionMappings">            <props>                <prop key="java.lang.Exception">error</prop>                <prop key="java.lang.Throwable">error</prop>            </props>        </property>    </bean></beans>

3、转到页面controller

@Controller@RequestMapping("/freemark")public class FreeMarkController extends BaseController {    @RequestMapping("/role.action")    public ModelAndView role(HttpServletRequest request, HttpServletResponse response) {        return new ModelAndView("role");    }}

4、页面展示

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    <title>insert title here</title></head><body><h1>自定义指令</h1><h2>用户tom是否拥有amdin权限,并返回admin的权限</h2><div>    <ul>        <@role user='tom' role='admin';hasAdminPower,adminPowerList>        <#if hasAdminPower>            我的角色是:amdin        </#if>        我的权限:        <#list adminPowerList as item>            ${item}        </#list>        </@role>    </ul></div></body></html>

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

java架构师项目实战,高并发集群分布式,大数据高可用视频教程,共760G

下载地址:

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

01.高级架构师四十二个阶段高
02.Java高级系统培训架构课程148课时
03.Java高级互联网架构师课程
04.Java互联网架构Netty、Nio、Mina等-视频教程
05.Java高级架构设计2016整理-视频教程
06.架构师基础、高级片
07.Java架构师必修linux运维系列课程
08.Java高级系统培训架构课程116课时
+
hadoop系列教程,java设计模式与数据结构, Spring Cloud微服务, SpringBoot入门

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