The following will take permission control as an example to customize a tag:
1. Tag type
<wxt:per uri="${pageContext.request.contextPath }/privilege/list"></wxt:per>
Steps:
1. Customize a class PerssionTag that inherits SimpleTagSupport (custom tags generally Inherit this class)
package cn.com.liveuc.privilege.tag; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Set; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.SimpleTagSupport; import cn.com.liveuc.privilege.model.Privilege; import cn.com.liveuc.privilege.model.Resource; import cn.com.liveuc.privilege.model.Role; import cn.com.liveuc.privilege.model.User; /** * * @说明 自定义标签 */ public class PerssionTag extends SimpleTagSupport { //自定义标签属性,用于标签传入参数 private String uri; //接收标签传入的参数 public void setUri(String uri) { this.uri = uri; } @Override public void doTag() throws JspException, IOException { //获取用户登陆后保存的Session PageContext page = (PageContext) this.getJspContext(); User user = (User) page.getSession().getAttribute("login"); //如果用户登陆 if(user != null) { //用户登陆判断用户权限 List<String> list = new ArrayList<String>(); //获取用户的角色 Set<Role> role = user.getRole(); for(Role r:role) { //获取角色对应的权限 Set<Privilege> privilege = r.getPrivilege(); for(Privilege p:privilege) { //获取权限对应的资源 Set<Resource> res = p.getResource(); for(Resource re:res) { list.add(re.getUri()); } } } for(String ur:list) { //判断用户的权限 if(ur.equals(uri)) { this.getJspBody().invoke(null); //有权限输出标签体内容 } } } } }
2. Create a tld file description tag under WEB-INF.
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"> <description><![CDATA["To make it easier to access dynamic data; the Apache Struts framework includes a library of custom tags. The tags interact with the framework's validation and internationalization features; to ensure that input is correct and output is localized. The Struts Tags can be used with JSP FreeMarker or Velocity."]]></description> <display-name>"Struts Tags"</display-name> <tlib-version>2.2.3</tlib-version> <short-name>s</short-name> <uri>/wxt</uri> <tag> <name>per</name><!-- 标签名 --> <tag-class>cn.com.liveuc.privilege.tag.PerssionTag</tag-class> <body-content>scriptless</body-content> <!-- 标签属性 --> <attribute> <name>uri</name><!-- 属性名称 --> <required>true</required><!-- 是否必须 --> <rtexprvalue>true</rtexprvalue><!-- 是否为动态标签 --> </attribute> </tag> </taglib>
3. Use tags
Import tags in Jsp pages:
% @taglib prefix="wxt" uri="/wxt" %
Use tag:
User permissions that include uri resources will output the tag content.
For more articles related to Java custom simple tag examples, please pay attention to the PHP Chinese website!

How to correctly configure apple-app-site-association file in Baota nginx? Recently, the company's iOS department sent an apple-app-site-association file and...

How to understand the classification and implementation methods of two consistency consensus algorithms? At the protocol level, there has been no new members in the selection of consistency algorithms for many years. ...

mybatis-plus...

The difference between ISTRUE and =True query conditions in MySQL In MySQL database, when processing Boolean values (Booleans), ISTRUE and =TRUE...

How to avoid data overwriting and style loss of merged cells when using EasyExcel for template filling? Using EasyExcel for Excel...

How to switch from Java programmers to audio and video development? Learning Paths and Resources Recommendations If you are a Java programmer and are participating in a video project, �...

How to efficiently count the number of node services in MYSQL tree structure in Java? When using MYSQL database, how to count the number of nodes in the tree structure...

How do newcomers choose Java project management tools for backends? Newbie who are just starting to learn back-end development often feel confused about choosing project management tools. Special...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment