thymeleaf模板引擎shiro集成框架


shiro权限框架。前端验证jsp设计。间tag它只能用于jsp系列模板引擎。

使用最近项目thymeleaf作为前端模板引擎,采用HTML档,未出台shiro的tag lib,假设你想利用这段时间shiro如果,可以引入 thymeleaf-extras-shiro.jar这个拓展包来曲线实现shiro的前端验证。

在pom.xml中增加例如以下依赖:

		<dependency>
			<groupId>com.github.theborakompanioni</groupId>
			<artifactId>thymeleaf-extras-shiro</artifactId>
			<version>1.0.2</version>
		</dependency>

关于版本号,能够在maven仓库中查询:http://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro/1.0.2

thymeleaf-extras-shiro.jar在git hub的地址:https://github.com/theborakompanioni/thymeleaf-extras-shiro。上面有详细的使用说明。

引入jar包后,我们要简单设置一下thymeleaf的引擎:

<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
  <property name="templateResolver" ref="templateResolver" />
  <property name="additionalDialects">
    <set>
      <bean class="at.pollux.thymeleaf.shiro.dialect.ShiroDialect"/>
    </set>
  </property>
</bean>


或者假设使用的是Java代码配置的话:

	public SpringTemplateEngine templateEngine() {
		SpringTemplateEngine templateEngine = new SpringTemplateEngine();
		templateEngine.setTemplateResolver(templateResolver());
		 
		Set<IDialect> additionalDialects = new HashSet<IDialect>();
		
		additionalDialects.add(new ShiroDialect());
		
		templateEngine.setAdditionalDialects(additionalDialects);
		
		return templateEngine;
	}

然后就能够在页面中使用thymeleaf化的shiro标签来进行前端验证了。

shiro本身的标签有限,没有hasAnyPermission标签,我在之前的一篇文章中《自己定义shiro标签》有写过jsp标签的版本号。事实上,在这里假设使用的是thymeleaf,也能够自己定义拓展一下shiro的hasAnyPermission。

将下载下来的thymeleaf-extras-shiro.jar打开,会有一个shiro-dialect.xml的文件,这里定义了thymeleaf的shiro属性,我们也能够依据这里面的样例进行简单拓展。
这里事实上是在拓展thymeleaf,官方文档中有具体的说明和简单的例子,对比文档,学习会更快。



版权声明:本文博主原创文章,博客,未经同意不得转载。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM