在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>
相關文章
相關標籤/搜索