可能不存在的属性的 JSP bean 标记

发布于 2024-07-08 09:33:47 字数 628 浏览 5 评论 0原文

在 JSP 中,我可以使用标签引用 bean 的属性 ${object.property}

有什么方法可以处理可能不存在的属性吗? 我有一个 JSP 页面需要处理不同的类型。 示例:

public class Person {
    public String getName()
}
public class Employee extends Person {
    public float getSalary()
}

在 JSP 中,我想显示一个包含姓名和薪水列的人员表。 如果此人不是雇员,则工资应为空。 HTML 行可能如下所示:

<tr>
    <td><c:out value="${person.name}"></td>
    <td><c:out value="${person.salary}"></td>
</tr>

不幸的是,如果人员不是员工,那么它无法找到薪水,并且会发生错误。 我该如何在 JSP 中解决这个问题?

编辑: JSP 标记语言中是否有 instanceof 检查?

In JSP I can reference a bean's property by using the tag
${object.property}

Is there some way to deal with properties that might not exist? I have a JSP page that needs to deal with different types. Example:

public class Person {
    public String getName()
}
public class Employee extends Person {
    public float getSalary()
}

In JSP I want to display a table of people with columns of name and salary. If the person is not an employee then salary should be blank. The row HTML might look like:

<tr>
    <td><c:out value="${person.name}"></td>
    <td><c:out value="${person.salary}"></td>
</tr>

Unfortunately if person is not an employee then it can't find salary and an error occurs. How would I solve this in JSP?

Edit: Is there an instanceof check in JSP tag language?

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

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

发布评论

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

评论(5

柒夜笙歌凉 2024-07-15 09:33:47

只需使用 EL 空运算符 IF 它是一个作用域属性,不幸的是,您必须使用带有的employee.salary 来包围您的表达式:

<c:catch var="err">
    <c:out value="${employee.salary}"/>
</c:catch>

如果您确实需要instanceof,那么您可能会考虑自定义标签。

Just use the EL empty operator IF it was a scoped attribute, unfortunately you'll have to go with surrounding your expression using employee.salary with <c:catch>:

<c:catch var="err">
    <c:out value="${employee.salary}"/>
</c:catch>

If you really need instanceof, you might consider a custom tag.

八巷 2024-07-15 09:33:47

如果您想要该类,只需使用 ${person.class} 即可。 您还可以使用 ${person.class.name eq 'my.package.PersonClass'}

您还可以在 c:out 上使用“默认”。

 <c:out value='${person.salary}' default="Null Value" />

If you want the class, just use ${person.class}. You can also use ${person.class.name eq 'my.package.PersonClass'}

You can also use the "default" on c:out.

 <c:out value='${person.salary}' default="Null Value" />
苹果你个爱泡泡 2024-07-15 09:33:47

简洁,但未经检查。

<tr>
    <td>${person.name}</td>    
    <td>${person.class.simpleName == 'Employee' ? person.salary : ''}</td>
</tr>

JSP标签语言中有instanceof检查吗?

目前还没有。 我在某处读到他们在 EL 中保留了它,instanceof 关键字,可能是为了将来。 此外,还有一个具有此特定标签的库。 在决定为自己创建一些自定义标签之前,请先研究一下这一点。 这是链接,非标准标签库

Concise, but unchecked.

<tr>
    <td>${person.name}</td>    
    <td>${person.class.simpleName == 'Employee' ? person.salary : ''}</td>
</tr>

Is there an instanceof check in JSP tag language?

Not at the moment of this writing. I read somewhere that they have reserved it, instanceof keyword, in EL, may be for future. Moreover, there is a library available that has this specific tag. Look into that before deciding to create some custom tag for yourself. Here is the link, Unstandard Tag Library.

鱼忆七猫命九 2024-07-15 09:33:47

一种方法是创建一个自定义标签库,并在其中使用多态性来处理 Person is-a Employee 的情况。

我已经有一段时间没有在 JSP 中这样做了,但经常在 GSP(Groovy/Grails Server Pages)中使用类似的技术。

否则,您可以在 JSP 中放置一些逻辑(不理想)来测试 Employee 状态:

<% 
   String salary
   if (person instanceof Employee) {
       salary = person.salary
   } else {
       salary = "" // or ' '
   }
%>
<td><c:out value="${salary}"></td>

One method would be to create a custom tag library and use polymorphism within it to handle the case where a Person is-a Employee.

I haven't done this in a while for JSP, but frequently use a similar technique in GSP (Groovy/Grails Server Pages).

Otherwise, you could put some logic in the JSP (not ideal) to test for Employee-ness:

<% 
   String salary
   if (person instanceof Employee) {
       salary = person.salary
   } else {
       salary = "" // or ' '
   }
%>
<td><c:out value="${salary}"></td>
追星践月 2024-07-15 09:33:47

你总是可以有一个类型字段。

public class Person {
    public String getType() { return "Person"; }
    public String getName()
}
public class Employee extends Person {
    public String getType() { return "Employee"; }
    public float getSalary()
}

你的 JSP 看起来像

<tr>
    <td><c:out value="${person.name}"></td>
    <td><c:if test="'Employee' eq person.type"><c:out value="${person.salary}"></c:if></td>
</tr>

当然 Class 类已经有这个...

<tr>
    <td><c:out value="${person.name}"></td>
    <td><c:if test="'Employee' eq person.class.simpleName"><c:out value="${person.salary}"></c:if></td>
</tr>

你也可以有 `isEmployee()' 方法。

You could always have a type field.

public class Person {
    public String getType() { return "Person"; }
    public String getName()
}
public class Employee extends Person {
    public String getType() { return "Employee"; }
    public float getSalary()
}

Your JSP would look like

<tr>
    <td><c:out value="${person.name}"></td>
    <td><c:if test="'Employee' eq person.type"><c:out value="${person.salary}"></c:if></td>
</tr>

Of course the Class class already has this...

<tr>
    <td><c:out value="${person.name}"></td>
    <td><c:if test="'Employee' eq person.class.simpleName"><c:out value="${person.salary}"></c:if></td>
</tr>

You could also have `isEmployee()' method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文