Invoking a Java method using an El expression

Source: Internet
Author: User

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

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.