`

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

 
阅读更多

目前Freemarker对Shrio的标签还不支持,不过已经有人贡献出来第三方面解决方案,如下:

1、下载shiro-freemarker-tags
GitHub地址:https://github.com/jagregory/shiro-freemarker-tags
可以先看一下他的说明文档,有一个初步的了解。

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

 

public class ShiroTagFreeMarkerConfigurer extends FreeMarkerConfigurer {
 
    @Override
    public void afterPropertiesSet() throws IOException, TemplateException {
        super.afterPropertiesSet();
        this.getConfiguration().setSharedVariable("shiro", new ShiroTags());
    }
     
}

 下面的Spring mvc配置

 

 

<bean id="freemakerCongfig"
    class="com.xxx.web.freemarker.ShiroTagFreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/views/" />
    <property name="freemarkerSettings">
        <props>
            <prop key="defaultEncoding">UTF-8</prop>
            <prop key="classic_compatible">true</prop>
        </props>
    </property>
</bean>

 

3、使用Shiro Tag

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

 

<@shiro.hasPermission name="auth:role:add">hello world</@shiro.hasPermission>

   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics