JSP bean tag for property that might not exist

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:

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

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?

Asked by: Roland742 | Posted: 23-01-2022

Answer 1

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}"/>

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

Answered by: Aida875 | Posted: 24-02-2022

Answer 2

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" />

Answered by: Marcus713 | Posted: 24-02-2022

Answer 3

Concise, but unchecked.

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

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.

Answered by: Walter904 | Posted: 24-02-2022

Answer 4

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 '&nbsp;'
<td><c:out value="${salary}"></td>

Answered by: Brad405 | Posted: 24-02-2022

Answer 5

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

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

Of course the Class class already has this...

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

You could also have `isEmployee()' method.

Answered by: Adrian140 | Posted: 24-02-2022

Similar questions

java get property only exist in child class

class Parent { } class Child extends parent { String something; //get and set } Parent parent = getChildObject(); //without casing getChildObject() to Child object. i cannot do child.getSomething(); Can i do something like parent.getProperty("something"); to get the value?

What is the data type for length property for Java arrays?

I want to find out if length property for Java arrays is an int/long or something else.

java - How do I inject a single property value into a string using spring 2.5.x?

I would really like to annotate a method with a reference to a single property in a property file for injection. @Resource("${my.service.url}") private String myServiceUrl; Of course, this syntax does not work ;) Thats why I'm asking here. I am aware that I can inject the full properties file, but that just seems excessive, I dont want the property file - I want the configured valu...

"Holds value of property" in Java code

I got some legacy code with that caption spread out as comment almost in every field. Also appearing is "Setter for property xyz". Just out of curiosity, which Java tool generated this stubs?

java - read property value in Ant

I need to read the value of a property from a file in an Ant script and strip off the first few characters. The property in question is path=file:C:/tmp/templates This property is store in a file that I can access within the ant script via &lt;property file="${web.inf.dir}/config.properties"/&gt; I have two questions: How do I read the single...

java - How to assign bean's property an Enum value in Spring config file?

I have a standalone enum type defined, something like this: package my.pkg.types; public enum MyEnumType { TYPE1, TYPE2 } Now, I want to inject a value of that type into a bean property: &lt;bean name="someName" class="my.pkg.classes"&gt; &lt;property name="type" value="my.pkg.types.MyEnumType.TYPE1" /&gt; &lt;/bean&gt;

Absence of property syntax in Java

C# has syntax for declaring and using properties. For example, one can declare a simple property, like this: public int Size { get; set; } One can also put a bit of logic into the property, like this: public string SizeHex { get { return String.Format("{0:X}", Size); } set { Size = int.Parse(value, NumberStyles.HexNumber); } } ...

java - A better class to update property files?

Though java.util.properties allows reading and writing properties file, the writing does not preserve the formatting. Not surprising, because it is not tied to the property file. Is there a PropertyFile class out there -- or some such -- that preserves comments and blank lines and updates property values in place?

java - In Eclipse RCP, how do I disable a save toolbar button according to the "dirty" property in editor

In my eclipse RCP 3.3 application, I would like to enable or disable a 'save' toolbar button according to current editor dirty flag. I'm trying to use the &lt;

java - Enforce service layer business rules on entity property changes, or hide entity property from clients but not service?

Let's say I have a class Customer, which has a property customerType. I have another class called something like SpecialContract, which has a Customer and some other properties. If customer.customerType == SPECIAL, then there is a SpecialContract which has a reference to this particular Customer. Now I realize that this is a bit hoaky, but I do not want to maintain a relationship from Customer

java - Replace the property value using RegEx

I have config.properties file containing properties in Java Properties format. I need to replace the value of a property with a known name with a new value. The comments and formatting of the file should be preserved. My current approach is to use RegEx to match the property name and then replace its value. However, Java Properties supports multi-line values, which I have hard time matching. Here is an exam...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)