Checking attribute exists in JSP

后端 未结 5 1475
别那么骄傲
别那么骄傲 2021-02-01 13:15

I have some classes which extends a superclass, and in the JSP I want to show some attributes of these classes. I only want to make one JSP, but I don\'t know in advance if the

5条回答
  •  甜味超标
    2021-02-01 13:44

    Just a more detailed (typical?) usage of BalusC great answer

    <%--
      [1] sets a default value for variable "currentAttribute"
      [2] check if myObject is not null
      [3] sets variable "currentAttribute" to the value of what it contains
      [4] catches "property not found exception" if any
           - if exception thrown, it does not output anything
           - if not exception thrown, it outputs the value of myObject.myAttribute
    
    --%>
     <%-- [1] --%>
     <%-- [2] --%>
         <%-- [3] --%>
            ${myObject.myAttribute} <%-- [4] --%>
        
    
    
    <%-- use the "currentAttribute" variable without worry in the rest of the code --%>
    currentAttribute is now equal to: ${currentAttribute}
    

    As pointed out by Shervin in the comments of BalusC's answer, this is probably NOT the cleanest solution but as replied by BalusC "that's so far the only way to achieve the odd requirement".

    Resources

    • Evaluate empty or null JSTL c tags
    • http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/set.html
    • http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/catch.html

提交回复
热议问题