Ralph
Ralph

Reputation: 120831

How to access an object via a method `get()` in jsp?

I have an container object that contains a google/Guava Optional and I want to access the content of this Optinal in jsp.

import com.google.common.base.Optional;
public class Container {       
   private Optional<User> user;
   public Optional<User> getUser(){return this.user;}
}

public class User{
   private String name;
   public String getName() {return this.name;}
}

A Optional has a method get() to obtain the inner object. http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Optional.html#get%28%29

I have already tried (${container} ins an instance of Container):

<c:out value="${container.user.name}" />
<c:out value="${container.user.get.name}" />
<c:out value="${container.user..name}" />

none of them work (Tomcat 7.42). Does anybody has an idea how to solve this, without adding a new property to the container (getUser2(){return this.user.get();})?

Upvotes: 2

Views: 2499

Answers (1)

Ralph
Ralph

Reputation: 120831

Thanks to Sotirios Delimanolis

since Servlet 3.0 / JSP 2.2 one can use

 <c:out value="${container.user.get().name}" />

Upvotes: 5

Related Questions