PrimeFaces无法从dataTable的选定行中获取布尔值 [英] PrimeFaces can't get boolean value from dataTable's selected row

查看:54
本文介绍了PrimeFaces无法从dataTable的选定行中获取布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有PrimeFaces 3.5,JSF 2.2,Hibernate 4 Web应用程序.

I have PrimeFaces 3.5, JSF 2.2, Hibernate 4 web app.

home.xhtml包含一个带有3个实体(用户,计算机,应用程序)和添加/编辑/删除按钮的嵌套dataTables的选项卡视图.每个按钮应该调用一个带有表单并提交/取消按钮的对话框.

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.

问题是,当尝试以与设置相同的方式对其进行编辑时,我无法从selectedApplication中正确显示布尔值(licenseRequired),我得到了以下消息:

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

如果我删除了负责licenseRequired字段的代码段,则一切正常,并且我得到了其他选定的值,而没有任何问题:

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:

这是对话框窗体,该对话框在此字段上导致NPE:

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 &amp;&amp; !args.validationFailed) dlg6.hide()"/>
    </h:form>

</p:dialog>

Application.java:

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:

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或类似的东西,它只能从2个选项中选择(真/假).就像在用户表(角色字段)上一样,该表设置为当前selectedUser的角色:

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:

如果我替换

                <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}"/>

然后一切正常,并且我显示了正确的值.但这不合适,因为我只需要从两个允许的值中选择,而不要输入随机文本.

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!

谢谢.

推荐答案

只是一个假设,但它可能是布尔值空转换错误.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆