JSP bean tag for a property that may not exist

In JSP, I can reference the bean property using the $ {Object.property} tag

Is there a way to handle properties that may not exist? I have a JSP page that has 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 name and salary columns. If the person is not an employee, then the salary should be empty. 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 a person is not an employee, he cannot find a salary and an error will occur. How can I solve this in JSP?

Edit: Does instanceof validation exist in the JSP tag language?

+6
java jsp javabeans jsp-tags
source share
5 answers

Just use the EL operator empty, IF this is a scope attribute, unfortunately, you will need to use employee.salary with <c: catch> with your surrounding expression:

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

If you really need an instance, you can consider a custom tag.

+7
source share

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

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

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

Brief but untested.

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

Is there an instance check in the JSP tag language?

Not at the time of this writing. I read somewhere that they reserved it, the instanceof keyword, in EL, maybe for the future. In addition, there is a library that has this particular tag. Take a look at this before deciding to create your own custom tag for yourself. Here is the link, Custom Tag Library .

+3
source share

One of the methods is to create a custom tag library and use polymorphism in it to handle the case when Person is-a Employee .

I have not done this for some time for JSP, but often use a similar method in GSP (Groovy / Grails Server Pages).

Otherwise, you can put some logic in the JSP (not perfect) to check for Employee -ness:

 <% String salary if (person instanceof Employee) { salary = person.salary } else { salary = "" // or '&nbsp;' } %> <td><c:out value="${salary}"></td> 
+1
source share

You can 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 will 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 can also use the isEmployee () method.

0
source share

All Articles