如何通过jsp中的`get()`方法访问对象? [英] How to access an object via a method `get()` in jsp?
问题描述
我有一个包含Google/番石榴Optional
的容器对象,并且我想在jsp中访问该Optinal
的内容.
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
具有获取内部对象的方法get()
. http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Optional.html#get%28%29
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
我已经尝试过(${container}
在Container
的实例中):
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}" />
它们都不起作用(Tomcat 7.42).有没有人知道如何解决这个问题,不向容器(getUser2(){return this.user.get();}
)添加新属性?
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();}
)?
推荐答案
感谢Sotirios Delimanolis
Thanks to Sotirios Delimanolis
从Servlet 3.0/JSP 2.2开始就可以使用
since Servlet 3.0 / JSP 2.2 one can use
<c:out value="${container.user.get().name}" />
这篇关于如何通过jsp中的`get()`方法访问对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!