PrimeFaces无法从dataTable的所选行中获取布尔值 [英] PrimeFaces can't get boolean value from dataTable's selected row
问题描述
home.xhtml包含一个用于3个实体(用户,计算机,应用程序)和添加/编辑/删除按钮的嵌套数据表的Tabview。每个按钮都应该用一个表单来调用一个对话框并提交/取消按钮。
问题是我无法从selectedApplication显示正确的布尔值(licenseRequired)尝试以与设置相同的方式编辑它,我收到消息:
/pages/home.xhtml @ 424,149 value =#{homeBean.selectedApplication.licenseRequired}:目标无法访问,selectedApplication返回null
如果我删除了负责licenseRequired字段的代码,一切正常,我得到其他选定的值没有任何问题:
这是一个对话框,导致NPE在这个字段上: / p>
< p:dialog id =editAppDialogheader =Editdraggable =trueclosable =falsemodal = true
widgetVar =dlg6width =600>
< h:form id =dlg6form>
< h:panelGrid id =panel6columns =3>
< h:outputLabel for =appName2value =Name:/>
< p:inputText id =appName2required =truevalue =#{homeBean.selectedApplication.appName}
label =Name>
< f:validator binding =#{uniqueApplicationValidator}/>
< / p:inputText>
< p:message for =appName2/>
< h:outputLabel for =vendorName2value =Vendor:/>
< p:inputText id =vendorName2value =#{homeBean.selectedApplication.vendorName}
label =Vendorrequired =true/>
< p:message for =vendorName2/>
< h:outputLabel for =appLicense2value =需要许可证:/>
< p:selectOneMenu id =appLicense2required =truestyle =width:80px; value =#{homeBean.selectedApplication.licenseRequired}>
< f:selectItem itemLabel =trueitemValue =#{true}/>
< f:selectItem itemLabel =falseitemValue =#{false}/>
< / p:selectOneMenu>
< p:message for =appLicense2/>
< / h:panelGrid>
< p:commandButton type =buttonvalue =取消onclick =dlg6.hide()/>
< p:commandButton value =OKprocess =@ formupdate =:dlg6form:panel6,:tab:applicationsaction =#{homeBean.editApplication}
oncomplete =if(args& amp; amp;!args.validationFailed)dlg6.hide()/>
< / h:form>
< / p:dialog>
Application.java:
@Entity
@Table(name =applications,catalog =adminportal)
public class应用程序实现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设置< 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用户selectedUser,newUser;
私人电脑selectedComputer,newComputer;
私人应用程序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;
updateUserButton();
}
}
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) {
computer.add(newComputer);
newComputer = new Computer();
updateCompButtons();
}
}
public void editComputer(){
if(hibernateDBManager.updateComputer(selectedComputer)){
computers.set(computers.indexOf 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(你确定要删除用户
+ selectedUser.getFirstName()++ selectedUser.getLastName()+?);
}
public void onCompRowSelect(SelectEvent event){
setSelectedComputer((Computer)event.getObject());
deleteCompMsg =你确定要删除电脑
+ selectedComputer.getComputerName()
+(+ selectedComputer.getIpAddress()+)?
}
public void onAppRowSelect(SelectEvent event){
setSelectedApplication((Application)event.getObject());
deleteAppMsg =您确定要删除应用程序
+ selectedApplication.getAppName()+?;
}
protected void updateUserButtons(){
rc.update(editUserBtn);
rc.update(deleteUserBtn);
}
protected void updateCompButtons(){
rc.update(editCompBtn);
rc.update(deleteCompBtn);
}
保护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;
}
//其他getters / setters
}
我需要做的是创建一个selectBoolean / selectOneMenu或一些simmilar,它只允许从2个选项(true / false)中进行选择。喜欢用户表(角色字段),它被设置为当前selectedUser的角色:
如果我替换
code>< p:selectOneMenu id =appLicense2required =truestyle =width:80px; value =#{homeBean.selectedApplication.licenseRequired}>
< f:selectItem itemLabel =trueitemValue =#{true}/>
< f:selectItem itemLabel =falseitemValue =#{false}/>
< / p:selectOneMenu>
简单
code>< p:inputText id =appLicense2value =#{homeBean.selectedApplication.licenseRequired}/>
然后一切正常,我显示正确的值。但是这并不适合,因为我只需要从2个允许的值中选择,而不是输入随机文本。
任何想法如何解决这个问题?每一个答案都非常感谢!
谢谢。
只是一个假设,是一个布尔空转换错误。
尝试初始化变量,如下所示:
@Column(name =license_required,nullable = false,columnDefinition =BIT,length = 1)
private boolean licenseRequired = false;
I have PrimeFaces 3.5, JSF 2.2, Hibernate 4 web app.
home.xhtml contains a tabview with nested dataTables for 3 entities (Users, Computer, Applications) and add/edit/delete buttons. Each button is supposed to call a dialog with a form and submit/cancel buttons.
The problem is that I can't display correctly boolean value (licenseRequired) from selectedApplication when trying to edit it in the same way as it was set, I get the message:
/pages/home.xhtml @424,149 value="#{homeBean.selectedApplication.licenseRequired}": Target Unreachable, 'selectedApplication' returned null
If I remove the piece of code that is responsible for licenseRequired field, everything works correctly and I get other selected values without any problems:
Here's the dialog form, which causes NPE on this field:
<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
}
What I need to do - is to make a selectBoolean/ selectOneMenu or something simmilar which lets to choose only from 2 options (true/ false). Like on users table (role field), which is set to the current selectedUser's role:
If I replace
<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>
with simple
<p:inputText id="appLicense2" value="#{homeBean.selectedApplication.licenseRequired}"/>
then everything is working OK and I have the right values displayed. But this doesn't fit because I need to choose only from 2 allowed values, not enter random text.
Any ideas how to solve this issue? Every answer is highly appreciated!
Thank you.
just a supposition, but maybe it is a boolean null conversion error.
try initializing the variable, like this:
@Column(name="license_required", nullable=false, columnDefinition = "BIT", length = 1)
private boolean licenseRequired = false;
这篇关于PrimeFaces无法从dataTable的所选行中获取布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!