我有下一个代码段,它按预期与<h:selectBooleanCheckbox/>
一起使用,如果我使用<p:selectBooleanCheckbox/>
则会失败:
<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:selectBooleanCheckbox id="appLicense2" value="#{homeBean.selectedApplication.licenseRequired}" style="height:22px;width:22px;margin: 0px;" />
<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>
这是输出:
/pages/home.xhtml @424,149 value="#{homeBean.selectedApplication.licenseRequired}": Target Unreachable, 'selectedApplication' returned null
变量,它们的值,方法等都可以,所以我猜没有必要提供它的代码,但如果你愿意,你可以查看我之前的问题,了解所有必要代码的更多细节。 PrimeFaces can't get boolean value from dataTable's selected row
所以,问题是:为什么同一个变量<p:selectBooleanCheckbox/>
抛出NPE而<h:selectBooleanCheckbox/>
没有?如何解决这个问题?我想使用PrimeFaces组件。
谢谢!
// EDITED
HomeBean.java:
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
}
答案 0 :(得分:0)
/pages/home.xhtml @424,149
value="#{homeBean.selectedApplication.licenseRequired}":
Target Unreachable, 'selectedApplication' returned null
这表明selectedApplication
对象为空。修复此问题,然后primefaces booleancheckbox
应该有效。