我有PrimeFaces 3.5,JSF 2.2,Hibernate 4网络应用程序。
home.xhtml包含一个tabview,其中包含3个实体(用户,计算机,应用程序)和添加/编辑/删除按钮的嵌套dataTable。每个按钮都应该调用带有表单和提交/取消按钮的对话框。
问题是当我尝试以与设置相同的方式编辑它时,我无法从selectedApplication显示正确的布尔值(licenseRequired),我收到消息:
/pages/home.xhtml @424,149 value="#{homeBean.selectedApplication.licenseRequired}": Target Unreachable, 'selectedApplication' returned null
如果我删除了负责licenseRequired字段的代码段,那么一切正常,我得到其他选定的值没有任何问题:
这是对话框表单,它会在此字段上导致NPE:
<p:dialog id="editAppDialog" header="Edit" draggable="true" closable="false" modal="true"
widgetVar="dlg6" width="600" >
<h:form id="dlg6form">
<h:panelGrid id="panel6" columns="3">
<h:outputLabel for="appName2" value="Name: "/>
<p:inputText id="appName2" required="true" value="#{homeBean.selectedApplication.appName}"
label="Name">
<f:validator binding="#{uniqueApplicationValidator}"/>
</p:inputText>
<p:message for="appName2"/>
<h:outputLabel for="vendorName2" value="Vendor: "/>
<p:inputText id="vendorName2" value="#{homeBean.selectedApplication.vendorName}"
label="Vendor" required="true" />
<p:message for="vendorName2"/>
<h:outputLabel for="appLicense2" value="Requires license: "/>
<p:selectOneMenu id="appLicense2" required="true" style="width: 80px;" value="#{homeBean.selectedApplication.licenseRequired}" >
<f:selectItem itemLabel="true" itemValue="#{true}" />
<f:selectItem itemLabel="false" itemValue="#{false}" />
</p:selectOneMenu>
<p:message for="appLicense2"/>
</h:panelGrid>
<p:commandButton type="button" value="Cancel" onclick="dlg6.hide()" />
<p:commandButton value="OK" process="@form" update=":dlg6form:panel6, :tab:applications" action="#{homeBean.editApplication}"
oncomplete="if (args && !args.validationFailed) dlg6.hide()"/>
</h:form>
</p:dialog>
Application.java:
@Entity
@Table(name="applications", catalog="adminportal")
public class Application implements Serializable {
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="app_id", unique=true, nullable=false)
private Integer appId;
@Column(name="app_name", nullable=false)
private String appName;
@Column(name="vendor_name", nullable=false)
private String vendorName;
@Column(name="license_required", nullable=false, columnDefinition = "BIT", length = 1)
private boolean licenseRequired;
@OneToMany(fetch=FetchType.LAZY, mappedBy="applications")
private Set<ComputerApp> computerApps = new HashSet<>(0);
public Integer getAppId() {
return appId;
}
public void setAppId(Integer appId) {
this.appId = appId;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getVendorName() {
return vendorName;
}
public void setVendorName(String vendorName) {
this.vendorName = vendorName;
}
public Set<ComputerApp> getComputerApps() {
return computerApps;
}
public void setComputerApps(Set<ComputerApp> computerApps) {
this.computerApps = computerApps;
}
public boolean isLicenseRequired() {
return licenseRequired;
}
public void setLicenseRequired(boolean licenseRequired) {
this.licenseRequired = licenseRequired;
}
}
HomeBean.java:
package com.infostroy.adminportal.controller.bean;
import com.infostroy.adminportal.bean.BaseBean;
import com.infostroy.adminportal.model.Application;
import com.infostroy.adminportal.model.Computer;
import com.infostroy.adminportal.model.User;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.annotation.PostConstruct;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("session")
public class HomeBean extends BaseBean {
private static final String editUserBtn = "tab:form1:editUser";
private static final String deleteUserBtn = "tab:form1:deleteUser";
private static final String editCompBtn = "tab:form2:editComp";
private static final String deleteCompBtn = "tab:form2:deleteComp";
private static final String editAppBtn = "tab:form3:editApp";
private static final String deleteAppBtn = "tab:form3:deleteApp";
private List<User> users;
private List<Computer> computers;
private List<Application> applications;
private User selectedUser, newUser;
private Computer selectedComputer, newComputer;
private Application selectedApplication, newApplication;
private String deleteUserMsg, deleteCompMsg, deleteAppMsg;
private RequestContext rc;
@PostConstruct
public void init() {
setUsers(hibernateDBManager.getAllUsers());
setComputers(hibernateDBManager.getAllComputers());
setApplications(hibernateDBManager.getAllApplications());
newUser = new User();
newComputer = new Computer();
newApplication = new Application();
rc = RequestContext.getCurrentInstance();
}
public void addUser() throws NoSuchAlgorithmException {
if (hibernateDBManager.insertUser(newUser)) {
users.add(newUser);
newUser = new User();
updateUserButtons();
}
}
public void editUser() throws NoSuchAlgorithmException {
if (hibernateDBManager.updateUser(selectedUser)) {
users.set(users.indexOf(selectedUser), selectedUser);
selectedUser = null;
updateUserButtons();
}
}
public void deleteUser() throws IOException {
if (selectedUser != null) {
if (hibernateDBManager.deleteUserById(selectedUser.getUserId()) > 0) {
users.remove(selectedUser);
selectedUser = null;
updateUserButtons();
}
}
}
public void addComputer() {
if (newComputer != null && hibernateDBManager.insertComputer(newComputer)) {
computers.add(newComputer);
newComputer = new Computer();
updateCompButtons();
}
}
public void editComputer() {
if (hibernateDBManager.updateComputer(selectedComputer)) {
computers.set(computers.indexOf(selectedComputer), selectedComputer);
selectedComputer = null;
updateCompButtons();
}
}
public void deleteComputer() {
if (selectedComputer != null) {
if (hibernateDBManager.deleteComputerById(selectedComputer.getComputerId()) > 0) {
computers.remove(selectedComputer);
selectedComputer = null;
updateCompButtons();
}
}
}
public void addApplication() {
if (newApplication != null && hibernateDBManager.insertApplication(newApplication)) {
applications.add(newApplication);
newApplication = new Application();
updateAppButtons();
}
}
public void editApplication() {
if (hibernateDBManager.updateApplication(selectedApplication)) {
applications.set(applications.indexOf(selectedApplication), selectedApplication);
selectedApplication = null;
updateAppButtons();
}
}
public void deleteApplication() {
if (selectedApplication != null) {
if (hibernateDBManager.deleteApplicationById(selectedApplication.getAppId()) > 0) {
applications.remove(selectedApplication);
selectedApplication = null;
updateAppButtons();
}
}
}
public void onUserRowSelect(SelectEvent event) {
setSelectedUser((User) event.getObject());
setDeleteUserMsg("Are you sure you want to delete user "
+ selectedUser.getFirstName() + " " + selectedUser.getLastName() + "?");
}
public void onCompRowSelect(SelectEvent event) {
setSelectedComputer((Computer) event.getObject());
deleteCompMsg = "Are you sure you want to delete computer "
+ selectedComputer.getComputerName()
+ " (" + selectedComputer.getIpAddress() + ") ?";
}
public void onAppRowSelect(SelectEvent event) {
setSelectedApplication((Application) event.getObject());
deleteAppMsg = "Are you sure you want to delete application "
+ selectedApplication.getAppName() + "?";
}
protected void updateUserButtons() {
rc.update(editUserBtn);
rc.update(deleteUserBtn);
}
protected void updateCompButtons() {
rc.update(editCompBtn);
rc.update(deleteCompBtn);
}
protected void updateAppButtons() {
rc.update(editAppBtn);
rc.update(deleteAppBtn);
}
public String getDeleteUserMsg() {
return deleteUserMsg;
}
public void setDeleteUserMsg(String deleteUserMsg) {
this.deleteUserMsg = deleteUserMsg;
}
public String getDeleteCompMsg() {
return deleteCompMsg;
}
public void setDeleteCompMsg(String deleteCompMsg) {
this.deleteCompMsg = deleteCompMsg;
}
public String getDeleteAppMsg() {
return deleteAppMsg;
}
public void setDeleteAppMsg(String deleteAppMsg) {
this.deleteAppMsg = deleteAppMsg;
}
//Other getters/setters
}
我需要做的是制作一个selectBoolean / selectOneMenu或者simmilar,它只能从2个选项中选择(true / false)。与用户表(角色字段)类似,后者设置为当前selectedUser的角色:
如果我更换
<p:selectOneMenu id="appLicense2" required="true" style="width: 80px;" value="#{homeBean.selectedApplication.licenseRequired}" >
<f:selectItem itemLabel="true" itemValue="#{true}" />
<f:selectItem itemLabel="false" itemValue="#{false}" />
</p:selectOneMenu>
简单
<p:inputText id="appLicense2" value="#{homeBean.selectedApplication.licenseRequired}"/>
然后一切正常,我显示了正确的值。但这不合适,因为我只需要从2个允许的值中选择,而不是输入随机文本。
任何想法如何解决这个问题?每个答案都非常感谢!
谢谢。
答案 0 :(得分:0)
只是一个假设,但也许它是一个布尔空转换错误。
尝试初始化变量,如下所示:
@Column(name="license_required", nullable=false, columnDefinition = "BIT", length = 1)
private boolean licenseRequired = false;