1. 程式人生 > >thymeleaf模板整合shiro標籤

thymeleaf模板整合shiro標籤

一、引入依賴

<dependency>
	<groupId>org.thymeleaf</groupId>
	<artifactId>thymeleaf-spring4</artifactId>
	<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.github.theborakompanioni</groupId>
    <artifactId>thymeleaf-extras-shiro</artifactId>
    <version>2.0.0</version>
</dependency>
二、配置
<bean id="templateResolver"
	class="z3.dbus.web.mvc.Z3ThymeleafViewResolver">
	<property name="prefix" value="/WEB-INF/view/" />
	<property name="suffix" value=".html" />
	<!-- HTML is the default value, added here for the sake of clarity. -->
	<property name="templateMode" value="HTML" />
	<!-- Template cache is true by default. Set to false if you want -->
	<!-- templates to be automatically updated when modified. -->
	<property name="cacheable" value="true" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
	<property name="templateResolver" ref="templateResolver" />
	<property name="enableSpringELCompiler" value="true" />
	<property name="additionalDialects">  
	    <set>  
	      <bean class="at.pollux.thymeleaf.shiro.dialect.ShiroDialect"/>  
	    </set>  
    </property> 
</bean>

三、標籤使用

1、加入html名稱空間

<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"  
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">  
2、加入shiro標籤
<shiro:guest>  
<a>登入</a> <a>註冊</a>
</shiro:guest>  
<shiro:user>  
歡迎<shiro:principal property="name"/>
</shiro:user>