嗨,我正在为每个对此 Bean 发出的 ajax 请求获取一个新的 session 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>
最佳答案
如果您不小心从 javax.faces.bean
中导入了 @SessionScoped
,就会发生这种情况包而不是 javax.enterprise.context
包。
您正在使用 @javax.inject.Named
注释,因此您应该从 javax.enterprise.context
包中导入范围。 javax.faces.bean
包中的作用域只能与 @javax.faces.bean.ManagedBean
结合使用注释。
没有有效范围的 CDI bean 将表现得像 @RequestScoped
。没有有效范围的 JSF bean 将表现得像 @NoneScoped
。
关于jsf - 每次我在 JSF2 中发出 Ajax 请求时,我都会得到一个新的 session bean,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7687812/