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入門
—————————————————————————————————————————————————–