检查 JSP 中是否存在属性

发布于 2024-08-27 04:51:10 字数 465 浏览 6 评论 0原文

我有一些扩展超类的类,并且在 JSP 中我想显示这些类的一些属性。我只想制作一个JSP,但我事先不知道该对象是否有属性。所以我需要一个 JSTL 表达式或一个标签来检查我传递的对象是否具有此属性(类似于 javascript 中的 in 运算符,但在服务器中)。

<c:if test="${an expression which checks if myAttribute exists in myObject}">
    <!-- Display this only when myObject has the atttribute "myAttribute" -->
    <!-- Now I can access safely to "myAttribute" -->
    ${myObject.myAttribute}
</C:if>

我怎样才能得到这个?

谢谢。

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 object has an attribute or not. So I need a JSTL expression or a tag which checks that the object I pass has this attribute (similar to in operator in javascript, but in the server).

<c:if test="${an expression which checks if myAttribute exists in myObject}">
    <!-- Display this only when myObject has the atttribute "myAttribute" -->
    <!-- Now I can access safely to "myAttribute" -->
    ${myObject.myAttribute}
</C:if>

How can I get this?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

仅冇旳回忆 2024-09-03 04:51:10

使用 JSTL c:catch< /代码>

<c:catch var="exception">${myObject.myAttribute}</c:catch>
<c:if test="${not empty exception}">Attribute not available.</c:if>

Make use of JSTL c:catch.

<c:catch var="exception">${myObject.myAttribute}</c:catch>
<c:if test="${not empty exception}">Attribute not available.</c:if>
北方。的韩爷 2024-09-03 04:51:10

您可以轻松创建自定义函数来检查属性,按照 vivin 的博客文章

简而言之,如果您已经拥有自己的 taglib,那么只需创建一个静态“hasProperty”方法...

import java.beans.PropertyDescriptor;
import org.apache.commons.beanutils.PropertyUtils;

...

public static boolean hasProperty(Object o, String propertyName) {
    if (o == null || propertyName == null) {
        return false;
    }
    try
    {
      return PropertyUtils.getPropertyDescriptor(o, propertyName) != null;
    }
    catch (Exception e)
    {
      return false;
    }
}

...并向您的 TLD 添加五行...

<function>
    <name>hasProperty</name>
    <function-class>my.package.MyUtilClass</function-class>
    <function-signature>boolean hasProperty(java.lang.Object,
        java.lang.String)
    </function-signature>
</function>

...并在 JSP 中调用它

<c:if test="${myTld:hasProperty(myObject, 'myAttribute')}">
  <c:set var="foo" value="${myObject.myAttribute}" />
</c:if>

You can readily create a custom function to check for the property, as per vivin's blog post.

In short, if you already have your own taglib its just a matter of creating a static 'hasProperty' method...

import java.beans.PropertyDescriptor;
import org.apache.commons.beanutils.PropertyUtils;

...

public static boolean hasProperty(Object o, String propertyName) {
    if (o == null || propertyName == null) {
        return false;
    }
    try
    {
      return PropertyUtils.getPropertyDescriptor(o, propertyName) != null;
    }
    catch (Exception e)
    {
      return false;
    }
}

...and adding five lines to your TLD...

<function>
    <name>hasProperty</name>
    <function-class>my.package.MyUtilClass</function-class>
    <function-signature>boolean hasProperty(java.lang.Object,
        java.lang.String)
    </function-signature>
</function>

... and calling it in your JSP

<c:if test="${myTld:hasProperty(myObject, 'myAttribute')}">
  <c:set var="foo" value="${myObject.myAttribute}" />
</c:if>
記憶穿過時間隧道 2024-09-03 04:51:10

当我只想测试对象是否有字段但不想输出字段的值时,接受的答案可能会产生一些副作用。在上述情况下,我使用下面的代码片段:

 <c:catch var="exception">
        <c:if test="${object.class.getDeclaredField(field) ne null}">            
        </c:if>
 </c:catch>

希望这会有所帮助。

The accepted answer may have some side effects when I just want to test if the object has a field, but do not want to output the value of the field. In the mentioned case, I use the snippet below:

 <c:catch var="exception">
        <c:if test="${object.class.getDeclaredField(field) ne null}">            
        </c:if>
 </c:catch>

hope this helps.

不知在何时 2024-09-03 04:51:10

只是 BalusC 伟大答案的更详细(典型?)用法

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

--%>
<c:set var="currentAttribute" value="" /> <%-- [1] --%>
<c:if test="${not empty myObject}"> <%-- [2] --%>
    <c:set var="currentAttribute"> <%-- [3] --%>
        <c:catch var="exception">${myObject.myAttribute}</c:catch> <%-- [4] --%>
    </c:set>
</c:if>

<%-- use the "currentAttribute" variable without worry in the rest of the code --%>
currentAttribute is now equal to: ${currentAttribute}

正如 Shervin 在 BalusC 答案的评论中指出的那样,这可能不是最干净的解决方案,但正如 BalusC 回答的那样“这是迄今为止实现奇怪要求的唯一方法” 。

资源

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

--%>
<c:set var="currentAttribute" value="" /> <%-- [1] --%>
<c:if test="${not empty myObject}"> <%-- [2] --%>
    <c:set var="currentAttribute"> <%-- [3] --%>
        <c:catch var="exception">${myObject.myAttribute}</c:catch> <%-- [4] --%>
    </c:set>
</c:if>

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

无悔心 2024-09-03 04:51:10

你的意思是这样的:

<c:if test="${not null myObject.myAttribute}">
   <!-- Now I can access safely to "myAttribute" -->
</C:if>

或其他变体

<c:if test="${myObject.myAttribute != null}">
   <!-- Now I can access safely to "myAttribute" -->
</C:if>

如果它是一个列表你可以做

<c:if test="#{not empty myObject.myAttribute}">

Do you mean something like this:

<c:if test="${not null myObject.myAttribute}">
   <!-- Now I can access safely to "myAttribute" -->
</C:if>

or other variant

<c:if test="${myObject.myAttribute != null}">
   <!-- Now I can access safely to "myAttribute" -->
</C:if>

If it is a list you can do

<c:if test="#{not empty myObject.myAttribute}">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文