我想使用“jsp:usebean”从 JSP 调用 Java 类的 getter 方法(返回字符串值),但它返回空值。我不明白的是为什么它不能返回更新后的值。
有人可以阐明这一点吗?
我应该使用
Cookie
从 JSP 获取值吗?
最佳答案
我不确定您使用的是什么(Struts、普通 Servlet 等),但本质上您需要向 ServletRequest 添加一个属性喜欢:
class Person {
private String firstName;
// other fields, getters, setters
}
public void method(HttpServletRequest httpServletRequest) {
Person p = new Person();
p.setFirstName("Obama");
httpServletRequest.setAttribute("person", p);
}
在你的 JSP 中:
<jsp:getProperty object="person" property="firstName" />
或者如果您使用 JSTL :
<c:out value="${person.firstName}"/>
关于Java/Java EE : to invoke getter method from JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4688304/