【发布时间】:2016-07-02 12:12:03
【问题描述】:
我想将 CDI SessionScoped bean 注入 JSP 页面。
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
@SessionScoped
public class UserSessionBean implements Serializable {
private String email = "email";
public UserSessionBean(){}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
当我以这种方式使用 bean 时,它工作正常,并且我在 JSP 页面上看到了初始值。
<jsp:useBean id="userSessionBean" class="package.UserSessionBean"/>
<jsp:getProperty name="userSessionBean" property="email"/>
当我将同一个 bean 注入到我从 API 中的另一个 servlet 调用的服务中时,就会出现问题。在这种情况下,我不会在 JSP 页面上获得更新的值。看起来我在 JSP 页面和使用 @Inject 注释的服务内部得到了不同的 bean
谁能建议如何在 JSP 和从 servlet 访问的服务层中使用相同的 SessionScoped bean?
【问题讨论】:
-
AFAIK,你是否用
@Named("foo")注释你的类,bean 将存储在名为“foo”的会话属性中。 jsp:useBean 和 jsp:getProperty 在 1999 年很棒。现在它们已经完全过时了。