每次我在 JSF2 中发出 Ajax 请求时,我都会得到一个新的会话 bean,为什么?

发布于 2024-12-08 13:34:54 字数 1615 浏览 0 评论 0 原文

您好,我为每个向此 Bean 发出的 Ajax 请求获取一个新的会话 Bean...你们中的任何人都可以告诉我为什么吗?

...... imports ......
@Named(value = "userController")
@SessionScoped
public class UserController implements Serializable {

private User current;
private DataModel items = null;
@EJB
private br.com.cflex.itm.dataaccess.UserFacade ejbFacade;
private PaginationHelper pagination;
private int selectedItemIndex;

public UserController() {
}

public Collection<Project> getMyProjectList(){
    String login = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName();
    User u = ejbFacade.getUserFromLogin(login);
    return u.getProjectCollection();
}    

public User getSelected() {               
    if (current == null) {
        current = new User();
        selectedItemIndex = -1;
    }
    return current;
} 
....... rest of class ....

每次我提出这个请求时,我都会得到一个新的 SessionBean,据我所知,我应该一次又一次地得到同一个人。

    <h:panelGrid columns="2" cellpadding="2">
      <h:form>
       <h:outputText value="#{bundle.FirstName}"/>                                
         <h:inputText id="name" value="#{userController.selected.name}">
          <f:ajax event="keyup" execute="name" render="out" />
<!--       <f:ajax event="keyup" render="out"/>-->
           </h:inputText>
           <p>
            <h:commandButton value="add"></h:commandButton>      
            <h:outputText id="out" value="#{userController.selected.name}"/>
           </p>
      </h:form>
    </h:panelGrid>

Hi I am getting a new session bean for every ajax request made to this Bean... Can any of you tell me why ?

...... imports ......
@Named(value = "userController")
@SessionScoped
public class UserController implements Serializable {

private User current;
private DataModel items = null;
@EJB
private br.com.cflex.itm.dataaccess.UserFacade ejbFacade;
private PaginationHelper pagination;
private int selectedItemIndex;

public UserController() {
}

public Collection<Project> getMyProjectList(){
    String login = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName();
    User u = ejbFacade.getUserFromLogin(login);
    return u.getProjectCollection();
}    

public User getSelected() {               
    if (current == null) {
        current = new User();
        selectedItemIndex = -1;
    }
    return current;
} 
....... rest of class ....

Every time I make this request I am getting a new of this SessionBean I for as far as I know I should be getting the same guy over and over again.

    <h:panelGrid columns="2" cellpadding="2">
      <h:form>
       <h:outputText value="#{bundle.FirstName}"/>                                
         <h:inputText id="name" value="#{userController.selected.name}">
          <f:ajax event="keyup" execute="name" render="out" />
<!--       <f:ajax event="keyup" render="out"/>-->
           </h:inputText>
           <p>
            <h:commandButton value="add"></h:commandButton>      
            <h:outputText id="out" value="#{userController.selected.name}"/>
           </p>
      </h:form>
    </h:panelGrid>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2024-12-15 13:34:54

如果您不小心从 @SessionScoped ,就会发生这种情况="nofollow">javax.faces.bean 包而不是 javax.enterprise.context 包。

您正在使用 @javax.inject.Named 注释,因此您应该从 javax.enterprise.context 包导入作用域。 javax.faces.bean 包中的范围仅与 @javax.faces.bean.ManagedBean 注释。

没有有效范围的 CDI bean 的行为类似于 @RequestScoped。没有有效作用域的 JSF bean 的行为类似于 @NoneScoped

That can happen if you accidently imported @SessionScoped from the javax.faces.bean package instead of the javax.enterprise.context package.

You're using @javax.inject.Named annotation, so you should import the scopes from the javax.enterprise.context package. The scopes from the javax.faces.bean package only works in combination with @javax.faces.bean.ManagedBean annotation.

A CDI bean without a valid scope will behave like @RequestScoped. A JSF bean without a valid scope will behave like @NoneScoped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文