Integration of the Thymeleaf template engine and the Shiro framework

Source: Internet
Author: User

Shiro permissions framework, front-end validation is designed for JSP, where the tag can only be used for the JSP series template engine. The recent project uses Thymeleaf as the front-end template engine, using HTML files, unable to introduce Shiro's tag lib, if you want to use Shiro, you can introduce Thymeleaf-extras-shiro.jar This expansion package curve realizes the Shiro front-end verification.

Add the following dependencies to the Pom.xml:

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

For versions, you can query in the MAVEN repository: http://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro/1.0.2

Thymeleaf-extras-shiro.jar at the address of the Git hub: Https://github.com/theborakompanioni/thymeleaf-extras-shiro, which has specific instructions for use.

After introducing the jar package, we will simply set up the Thymeleaf engine:

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


Or, if you are using Java code configuration:

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

You can then use the Thymeleaf Shiro tag in the page to verify the front end.

Shiro the label itself is limited, there is no hasanypermission tag, I wrote in a previous article, "Custom Shiro label" has written the JSP tag version, in fact, if you use the thymeleaf here, You can also customize the extension of Shiro Hasanypermission.

Will download the Thymeleaf-extras-shiro.jar open, there will be a shiro-dialect.xml file, which defines the Thymeleaf Shiro properties, we can also be based on the example of the simple extension.
This is in fact the expansion of the Thymeleaf, the official documents have detailed instructions and simple examples, compared to the document, learning will be faster.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.