First, create a new class, write a static method in the class
1 Public classPrivilegeutils {2 Public StaticBoolean checkprivilegebyname (user user, String name) {3 if(ISAdmin (User.getloginname ())) {4 return true;5 }6 for(Role r:user.getroles ()) {7 for(Privilege p:r.getprivileges ()) {8 if(Name.equals (P.getname ())) {9 return true;Ten } One } A } - return false; - } the}
Then create a new TLD file under the Web-inf folder (Note: You can put it in your own new folder, but not in the Lib and classess directories)
The contents of the TLD are as follows
<?XML version= "1.0" encoding= "UTF-8"?><taglibversion= "2.0"xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"> <tlib-version>1.0</tlib-version> <Short-name>MyFunction</Short-name> <URI>Http://www.privilege.demo.com/myFunction</URI> <function> <name>Checkprivilegebyname</name> <Function-class>Com.demo.privilege.utils.PrivilegeUtils</Function-class> <function-signature>Java.lang.Boolean checkprivilegebyname (Com.demo.privilege.entity.User, java.lang.String)</function-signature> </function></taglib>
Next, the JSP's head is introduced
<% @ taglib URI = " http://www.privilege.demo.com/myFunction " prefix = " myFunction " %>
The last is the use of
${myfunction:checkprivilegebyname (Loginuser, Privilegetop.name)}
Invoking a Java method using an El expression