【发布时间】:2012-06-01 08:54:23
【问题描述】:
我有两条不同的路径,而这只发生在另一条路径中:
SignUp.Xhtml-->SigninUpOkView-->Login.xhtml-->Welcome.xhtml-->UsersController.prepareCreateParty()--->createParty.xhtml-->UsersController.createParty()SignUp.Xhtml-->SigninUpOkView-->Login.xhtml-->Logout-->Login.xhtml-->Welcome.xhtml-->UsersController.prepareCreateParty()--->createParty.xhtml-->UsersController.createParty()
那么会发生什么:我在控制器的方法 prepareCreateParty() 中创建了一个新的派对对象,并将其插入到用户对象中,这样当用户转到 CreateParty-xhtml 页面时,派对不为空,但它仅在以防万一的情况下工作2 我在 Signin up 和 createParty 之间注销。 UserController 是 managedBean 并且 UsersController 在 SessionScoped 上。我仍然在会话中使用名称为用户的用户,并将其放在登录过滤器中。 Welcome.xhtml---->UsersController.CreateParty() 之间的情况相同,但发生了一些非常奇怪的事情。我尝试在 createParty-method 中将默认名称插入派对,在第一种情况下,即使用户对象仍然存在,它也会消失,只有用户内的派对属性为 NULL。情况二,当我注销和登录时,它仍然存在并且一切正常。什么会导致这种情况?我已经尝试了一切,我开始感到痛苦和沮丧。
在用户中:
@JoinColumn(name = "party_id", referencedColumnName = "party_id")
@ManyToOne
private Party partyId;
在用户控制器(SessionScoped ManagedBean)中: 公共用户控制器(用户用户实体){ this.currentUser = userEntity; }
public Users getSelected() {
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
if (currentUser == null) {
currentUser = (Users) session.getAttribute("user");
if(currentUser==null){
currentUser = new Users();
selectedItemIndex = -1;
}
}
public String prepareCreateParty() {
if (currentUser == null) {
currentUser = this.getUsersFromSession();
}
Party party = new Party();
party.setName("Testing default name of the party");
currentUser.setPartyId(party);
return "createParty";
在 CreateParty.xhtml 页面中:
<p:row>
<p:column> <h:outputLabel value="#{bundle.CreatePartyLabel_name}" for="name" /></p:column>
<p:column><p:inputText id="name" value="#{usersController.selected.partyId.name}" title="#{bundle.CreatePartyTitle_name}" required="true" requiredMessage="#{bundle.CreatePartyRequiredMessage_name}"/></p:column>
<p:column><p:tooltip for="name" showEvent="focus" hideEvent="blur" /></p:column>
<p:column><h:outputLabel value=" "/></p:column>
</p:row>
在 LoginFilter.doFilter() 中:
if (userPrincipal != null && session.getAttribute("user") == null) {
UsersFacade test = new UsersFacade();
Users userEntity = test.findByUsername(userPrincipal.getName());
session.setAttribute("user", userEntity);
UsersController userController = new UsersController(userEntity);// Unnecessary constructor call!!
玻璃鱼 3.1.2 莫哈拉 2.1.6 PrimeFaces 3.2 疯狂1.100%
到底是什么原因造成的,非常感谢您的帮助!我希望你能理解我的解释,即使英语是这样的。 萨米人
【问题讨论】:
-
这真的很难弄清楚。这类错误通常可以用调试器隔离。在这两种情况下逐步调试您的代码并找出差异。
-
我做了一个口香糖解决方案:在 prepareParty-method 中,我将聚会添加到 session 和 getSelected-method 我从那里读取它并将其添加到 currentUser 并且一切正常。公共用户 getSelected() { FacesContext facesContext = FacesContext.getCurrentInstance(); HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);派对 = (派对)session.getAttribute("派对"); if(party !=null){ currentUser.setPartyId(party); }
-
现在我调试了它,但没有解决方案。 CurrentUser 中的 Party 属性在 createParty() 和 getSelected() 方法之间变为 null,并且这两者之间没有代码。在 xhtml-page 中,我使用了像 UsersController.selected 这样的选择方法......用户名等其他一切都还可以,因此 currentUser 对象不为空,只是在其中参与。
-
您是否从
javax.faces.bean.SessionScoped为您的 bean 导入了正确的会话范围? -
导入 javax.faces.bean.SessionScoped;是的,这是在每个 managedBean Controller 类中。
标签: java session jsf properties mojarra