问题描述
嗨我正在为这个Bean发出的每个ajax请求获得一个新的会话bean ...你们有没有告诉我为什么?
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 ....
每次我提出这个请求时,我都会得到一个新的SessionBean我知道我应该一遍又一遍地得到同一个人。
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>
推荐答案
如果您意外导入来自包而不是 package。
That can happen if you accidently imported @SessionScoped
from the javax.faces.bean
package instead of the javax.enterprise.context
package.
您正在使用注释,因此您应该从 javax.enterprise.context
包中导入范围。 javax.faces.bean
包中的范围仅与注释。
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.
没有有效范围的CDI bean的行为类似于 @RequestScoped
。没有有效范围的JSF bean的行为类似于 @NoneScoped
。
A CDI bean without a valid scope will behave like @RequestScoped
. A JSF bean without a valid scope will behave like @NoneScoped
.
这篇关于每次我在JSF2中发出Ajax请求时,我都会得到一个新的会话bean,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!