Java – JSP bean tag for attributes that may not exist
•
Java
In JSP, I can use tags to reference bean properties
Is there a way to deal with attributes that may not exist? I have a JSP page that needs to handle different types Example:
public class Person {
public String getName()
}
public class Employee extends Person {
public float getSalary()
}
In JSP, I want to display a personnel table with name and salary column If the person is not an employee, the salary should be blank The HTML line might look like this:
<tr>
<td><c:out value="${person.name}"></td>
<td><c:out value="${person.salary}"></td>
</tr>
Unfortunately, if someone is not an employee, they can't find a salary and make mistakes How can I solve this problem in JSP?
Edit: is there a JSP tag language check instance?
Solution
Just use the El null operator. If it is a scope attribute, unfortunately, you must use employee Salary and < C: catch > to surround your expression:
<c:catch var="err">
<c:out value="${employee.salary}"/>
</c:catch>
If you really need instanceof, consider using custom tags
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
