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

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

一、下载shiro-freemarker-tags

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

二、Spring MVC配置

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

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


下面的Spring mvc配置web

<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>


三、使用Shiro Tag

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