我正在尝试在用户登录我的应用后加载一些数据。我需要用户名来加载特定于用户的数据。我遇到的问题是我有一个SessionScoped
支持bean,其中包含用于登录用户的代码,然后我有一个ViewScoped
页面特定的支持bean(它注入了SessionScoped bean)是加载页面的数据。
目前这是我的代码,我在加载数据时得到一个空指针,因为调用loadData()
方法时用户名不存在。
我很难找到解决这个问题的方法,因为我不想在每个页面上放置登录对话框,如果可能的话,将其保存在template.xhtml文件中。
的template.xhtml
...
<!-- Login Dialog -->
<p:dialog id="loginDialog" header="Login" widgetVar="loginWidget" modal="true" visible="#{!accessBacking.hasAccess}" closable="false">
<h:form id="loginForm">
<p:messages id="loginFormMessages" severity="error" autoUpdate="true" showDetail="true" />
<h:panelGrid columns="2" cellspacing="10" width="300">
<p:outputLabel for="username" value="Username" />
<p:inputText id="username" value="#{accessBacking.username}" required="true" requiredMessage="Username is Required" />
<p:outputLabel for="password" value="Password" />
<p:password id="password" value="#{accessBacking.password}" required="true" requiredMessage="Password is Required" />
<h:panelGroup></h:panelGroup>
<h:panelGroup>
<p:commandButton value="Login" styleClass="ui-priority-primary" actionListener="#{accessBacking.checkViewAccess}" oncomplete="handleAuthenticationRequest(xhr, status, args)" update="loginFormMessages" />
</h:panelGroup>
</h:panelGrid>
</h:form>
</p:dialog>
...
userGroups.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:body>
<f:metadata>
<f:event type="preRenderView" listener="#{userGroupBacking.setCurrentMenu}" />
</f:metadata>
<ui:composition template="/templates/template.xhtml">
<p:dataTable var="user" value="#{userGroupBacking.users}" editable="true" id="userTable">
...
AccessBacking.java
@ManagedBean(name="accessBacking")
@SessionScoped
public class AccessBacking {
private String username;
private String password;
public boolean checkViewAccess() {
Access access = new Access();
if(access.authenticate(username, password)) {
// user is logged in
}
}
}
UserGroupBacking
@ManagedBean(name="userGroupBacking")
@ViewScoped
public class UserGroupBacking {
@ManagedProperty(value="#{accessBacking}")
private AccessBacking accessBacking;
public void setAccessBacking(AccessBacking accessBacking) {
this.accessBacking = accessBacking;
}
@PostConstruct
public void init() {
loadData();
}
/**
* Loads the data for the page
*/
public void loadData() {
Dao dao = new Dao(ds);
users = dao.findAllUsers(accessBacking.getUsername(), accessBacking.getRoles()); // NULL POINTER BECAUSE ACCESSBACKING.GETUSERNAME() IS NULL SINCE THE USER HASN'T LOGGED IN YET.
}
}
答案 0 :(得分:0)
我使用ui:param
找到了一个好的解决方案。基本上我在我的页面特定的xhtml中设置了ui:param
我的支持bean值,然后在我的模板中引用ui:param
。
使用新解决方案更新了问题代码:
的template.xhtml
...
<!-- NOTE the 'myBacking' instead of the 'userGroupBacking' -->
<!-- Login Dialog -->
<p:dialog id="loginDialog" header="Login" widgetVar="loginWidget" modal="true" visible="#{!myBacking.hasAccess}" closable="false">
<h:form id="loginForm">
<p:messages id="loginFormMessages" severity="error" autoUpdate="true" showDetail="true" />
<h:panelGrid columns="2" cellspacing="10" width="300">
<p:outputLabel for="username" value="Username" />
<p:inputText id="username" value="#{accessBacking.username}" required="true" requiredMessage="Username is Required" />
<p:outputLabel for="password" value="Password" />
<p:password id="password" value="#{accessBacking.password}" required="true" requiredMessage="Password is Required" />
<h:panelGroup></h:panelGroup>
<h:panelGroup>
<p:commandButton value="Login" styleClass="ui-priority-primary" actionListener="#{myBacking.checkViewAccess}" oncomplete="handleAuthenticationRequest(xhr, status, args)" update="loginFormMessages" />
</h:panelGroup>
</h:panelGrid>
</h:form>
</p:dialog>
...
userGroups.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:body>
<f:metadata>
<f:event type="preRenderView" listener="#{userGroupBacking.setCurrentMenu}" />
</f:metadata>
<!-- THIS IS PART OF THE SOLUTION -->
<ui:param name="myBacking" value="#{userGroupBacking}" />
<ui:composition template="/templates/template.xhtml">
<p:dataTable var="user" value="#{userGroupBacking.users}" editable="true" id="userTable">
...
AccessBacking.java
@ManagedBean(name="accessBacking")
@SessionScoped
public class AccessBacking {
private String username;
private String password;
public boolean checkViewAccess() {
Access access = new Access();
if(access.authenticate(username, password)) {
// user is logged in
}
}
}
UserGroupBacking
@ManagedBean(name="userGroupBacking")
@ViewScoped
public class UserGroupBacking {
@ManagedProperty(value="#{accessBacking}")
private AccessBacking accessBacking;
public void setAccessBacking(AccessBacking accessBacking) {
this.accessBacking = accessBacking;
}
@PostConstruct
public void init() {
loadData();
}
// CHECKS AGAINST ACCESSBACKING
public boolean isHasAccess() {
return accessBacking.isHasAccess();
}
// CHECKS AGAINST ACCESSBACKING
public boolean checkViewAccess() {
return accessBacking.checkViewAccess();
}
/**
* Loads the data for the page
*/
public void loadData() {
Dao dao = new Dao(ds);
users = dao.findAllUsers(accessBacking.getUsername(), accessBacking.getRoles());
}
}