在FreeMarker框架中使用Shiro的Tag标签

来源:互联网 发布:photoshop mac安装包 编辑:程序博客网 时间:2024/05/19 17:57

可以先了解下shiro

第一步:下载shiro-freemarker-tags GitHub地址:https://github.com/jagregory/shiro-freemarker-tags 

可以先看一下他的说明文档,有一个初步的了解。


第二步:Spring MVC配置

自定义一个ShiroTagFreeMarkerConfigurer继承Spring本身提供的FreeMarkerConfigurer,目的是在FreeMarker的Configuration中添加shiro的配置

[java] view plain copy
 print?
  1. public class ShiroTagFreeMarkerConfigurer extends FreeMarkerConfigurer {  
  2.     
  3.     @Override  
  4.     public void afterPropertiesSet() throws IOException, TemplateException {  
  5.         super.afterPropertiesSet();  
  6.         this.getConfiguration().setSharedVariable("shiro"new ShiroTags());  
  7.     }  
  8.         
  9. }  

第三步:xml文件配置

[html] view plain copy
 print?
  1. <bean id="freemarkerConfig" class="com.xxx.core.shiro.freemarker.ShiroFreeMarkerConfigurer">  
  2.     <property name="templateLoaderPath" value="/WEB-INF"/>  
  3.     <property name="freemarkerVariables">  
  4.         <map>  
  5.         </map>  
  6.     </property>  
  7.     <property name="freemarkerSettings">  
  8.         <props>  
  9.             <prop key="template_update_delay">0</prop>  
  10.             <prop key="defaultEncoding">UTF-8</prop>  
  11.             <prop key="url_escaping_charset">UTF-8</prop>  
  12.             <prop key="locale">zh_CN</prop>  
  13.             <prop key="boolean_format">true,false</prop>  
  14.             <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>  
  15.             <prop key="date_format">yyyy-MM-dd</prop>  
  16.             <prop key="time_format">HH:mm:ss</prop>  
  17.             <prop key="number_format">0.######</prop>  
  18.             <prop key="whitespace_stripping">true</prop>  
  19.             <prop key="auto_import">/ftl/spring.ftl as s</prop>  
  20.         </props>  
  21.     </property>  
  22. </bean>  


第四步:使用Shiro Tag

<@shiro.guest>Hello guest!</@shiro.guest>

原创粉丝点击