在PrimeFaces中显示和编辑设置值

紫水晶

我有JSF 2.2PrimeFaces 3.5Spring 3.1.3Hibernate 4.1MySQLweb应用程序。

在home.xhtml上,我有一个tabview,其中包含3个嵌套的dataTables,用于3个实体(用户,计算机,应用程序)和CRUD按钮(请参见屏幕截图)。每个按钮都会调用一个带有窗体的对话框窗口,以添加/编辑/删除实体。

我将提供与问题相关的所有代码(您可能不需要检查所有代码,我只是确保您可能感兴趣的代码的每个部分都在下面。)

在此处输入图片说明

请考虑以下用于创建实体及其对应类的脚本:

CREATE TABLE computers (
computer_id INT AUTO_INCREMENT,
computer_name VARCHAR(15) NOT NULL,
ip_address VARCHAR(15) NOT NULL UNIQUE,
login VARCHAR(20) NOT NULL,
password VARCHAR(20) NOT NULL,
PRIMARY KEY(computer_id)
) ENGINE=InnoDB;

CREATE TABLE applications (
app_id INT AUTO_INCREMENT,
app_name VARCHAR(255) NOT NULL,
vendor_name VARCHAR(255) NOT NULL,
license_required TINYINT(1) NOT NULL,
PRIMARY KEY(app_id)
) ENGINE=InnoDB;

CREATE TABLE computer_app (
computer_id INT,
app_id INT,
FOREIGN KEY (computer_id)
    REFERENCES computers(computer_id)
    ON DELETE CASCADE,
FOREIGN KEY (app_id)
    REFERENCES applications(app_id)
    ON DELETE CASCADE
) ENGINE = InnoDB;

电脑课:

@Entity
@Table(name="computers", catalog="adminportal")
public class Computer  implements Serializable {

    @Id @GeneratedValue(strategy=IDENTITY)
    @Column(name="computer_id", unique=true, nullable=false)
     private Integer computerId;

    @Column(name="computer_name", nullable=false, length=15)
     private String computerName;

    @Column(name="ip_address", unique=true, nullable=false, length=15)
     private String ipAddress;

    @Column(name="login", nullable=false, length=20)
     private String login;

    @Column(name="password", nullable=false, length=20)
     private String password;

    @OneToMany(fetch=FetchType.EAGER, mappedBy="computers")
     private Set<ComputerApp> computerApps = new HashSet<>(0);

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);

ComputerApp.java

@Entity
@Table(name="computer_app" ,catalog="adminportal")
public class ComputerApp  implements Serializable {

    @EmbeddedId
    @AttributeOverrides( {
        @AttributeOverride(name="computerId", column=@Column(name="computer_id") ), 
        @AttributeOverride(name="appId", column=@Column(name="app_id") ) } )
     private ComputerAppId id;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="app_id", insertable=false, updatable=false)
     private Application applications;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="computer_id", insertable=false, updatable=false)
     private Computer computers;

ComputerAppId.java:

@Embeddable
public class ComputerAppId implements Serializable {

    @Column(name = "computer_id")
    private Integer computerId;

    @Column(name = "app_id")
    private Integer appId;

home.xhtml:

<p:tabView id="tab" orientation="left">

        <p:tab title="Users" rendered="#{request.isUserInRole('ROLE_ADMIN')}">

            <h:form id="form1">

                <h:panelGrid columns="9" >

                    <p:commandButton type="button" value="Add" onclick="dlg1.show()" />


                    <p:commandButton id="editUser" type="button" value="Edit" onclick="dlg2.show()" disabled="#{homeBean.selectedUser == null}"/>


                    <p:commandButton id="deleteUser" type="button" onclick="confirmation1.show()" value="Delete" disabled="#{homeBean.selectedUser == null}"/>
                    <p:confirmDialog id="delete1" header="Confirmation" widgetVar="confirmation1" >
                        <p:commandButton value="Yes" update=":tab:users" process="@this" styleClass="ui-confirmdialog-yes" icon="ui-icon-check"
                                         oncomplete="confirmation1.hide()" action="#{homeBean.deleteUser}" />
                        <p:commandButton value="No" onclick="confirmation1.hide()" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close"/>
                        <f:facet name="message">
                            <p:outputPanel>
                                <h:outputFormat value="#{homeBean.deleteUserMsg}"/>
                            </p:outputPanel>
                        </f:facet>
                    </p:confirmDialog>

                </h:panelGrid>

            </h:form>


            <p:dataTable id="users" var="user" value="#{homeBean.users}" 
                         scrollable="true" scrollHeight="250" selectionMode="single"
                         selection="#{homeBean.selectedUser}" rowKey="#{user.userId}"
                         sortMode="single">

                <p:ajax event="rowSelect" listener="#{homeBean.onUserRowSelect}" update=":tab:form1:deleteUser, :tab:form1:editUser, :tab:form1:delete1, :dlg2form:panel2"/> 

                <p:column headerText="Login" sortBy="#{user.login}">  
                    <h:outputText value="#{user.login}"/>
                </p:column>

                <p:column headerText="Password" sortBy="#{user.password}">  
                    <h:outputText value="#{user.password}"/>
                </p:column>

                <p:column headerText="Role" sortBy="#{user.role}">  
                    <h:outputText value="#{user.role}"/>
                </p:column>

                <p:column headerText="Name" sortBy="#{user.firstName}">  
                    <h:outputText value="#{user.firstName}"/>
                </p:column>

                <p:column headerText="Surname" sortBy="#{user.lastName}">  
                    <h:outputText value="#{user.lastName}"/>
                </p:column>

            </p:dataTable>

            <ex:exporter target=":tab:users" fileName="Users"/>

        </p:tab>

        <p:tab title="Computers">

            <h:form id="form2">

                <h:panelGrid columns="9" rendered="#{request.isUserInRole('ROLE_ADMIN')}">

                    <p:commandButton type="button" value="Add" onclick="dlg3.show()"/>


                    <p:commandButton id="editComp" type="button" value="Edit" onclick="dlg4.show()" disabled="#{homeBean.selectedComputer == null}"/>


                    <p:commandButton id="deleteComp" type="button" onclick="confirmation2.show()" value="Delete" disabled="#{homeBean.selectedComputer == null}"/>
                    <p:confirmDialog id="delete2" header="Confirmation"
                                     severity="alert" widgetVar="confirmation2">
                        <p:commandButton value="Yes" update=":tab:computers" process="@this" styleClass="ui-confirmdialog-yes" icon="ui-icon-check"
                                         oncomplete="confirmation2.hide()" action="#{homeBean.deleteComputer}"/>
                        <p:commandButton value="No" onclick="confirmation2.hide()" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close"/>
                        <f:facet name="message">
                            <p:outputPanel>
                                <h:outputFormat value="#{homeBean.deleteCompMsg}"/>
                            </p:outputPanel>
                        </f:facet>
                    </p:confirmDialog>

                </h:panelGrid>

            </h:form>


            <p:dataTable id="computers" var="computer" value="#{homeBean.computers}" 
                         scrollable="true" scrollHeight="250" selectionMode="single"
                         selection="#{homeBean.selectedComputer}" rowKey="#{computer.computerId}"
                         sortMode="single" >
                <p:ajax event="rowSelect" listener="#{homeBean.onCompRowSelect}" update=":tab:form2:editComp, :tab:form2:deleteComp, :tab:form2:delete2, :dlg4form:panel4"/> 

                <p:column headerText="Login" sortBy="#{computer.login}">  
                    <h:outputText value="#{computer.login}"/>  
                </p:column>

                <p:column headerText="Password" sortBy="#{computer.password}">  
                    <h:outputText value="#{computer.password}"/>  
                </p:column>

                <p:column headerText="Name" sortBy="#{computer.computerName}" > 
                    <h:outputText value="#{computer.computerName}"/>  
                </p:column>

                <p:column headerText="IP address" sortBy="#{computer.ipAddress}">  
                    <h:outputText value="#{computer.ipAddress}"/>  
                </p:column>

            </p:dataTable>

            <ex:exporter target=":tab:computers" fileName="Computers"/>

        </p:tab>

        <p:tab title="Applications">


            <h:form id="form3" rendered="#{request.isUserInRole('ROLE_ADMIN')}">

                <h:panelGrid columns="9">

                    <p:commandButton type="button" value="Add" onclick="dlg5.show()"/>


                    <p:commandButton id="editApp" type="button" value="Edit" onclick="dlg6.show()" disabled="#{homeBean.selectedApplication == null}"/>


                    <p:commandButton id="deleteApp" type="button" onclick="confirmation3.show()" value="Delete" disabled="#{homeBean.selectedApplication == null}"/>
                    <p:confirmDialog id="delete3" message="Are you sure you want to delete this application?" header="Confirmation"
                                     severity="alert" widgetVar="confirmation3">
                        <p:commandButton value="Yes" update=":tab:applications" process="@this" styleClass="ui-confirmdialog-yes" icon="ui-icon-check"
                                         oncomplete="confirmation3.hide()" action="#{homeBean.deleteApplication}"/>
                        <p:commandButton value="No" onclick="confirmation3.hide()" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close"/>
                        <f:facet name="message">
                            <p:outputPanel>
                                <h:outputFormat value="#{homeBean.deleteAppMsg}"/>
                            </p:outputPanel>
                        </f:facet>
                    </p:confirmDialog>

                </h:panelGrid>

            </h:form>

            <p:dataTable id="applications" var="app" value="#{homeBean.applications}" 
                         scrollable="true" scrollHeight="250" selectionMode="single"
                         selection="#{homeBean.selectedApplication}" rowKey="#{app.appId}"
                         sortMode="single" >

                <p:ajax event="rowSelect" listener="#{homeBean.onAppRowSelect}" update=":tab:form3:editApp, :tab:form3:deleteApp, :tab:form3:delete3, :dlg6form:panel6"/>

                <p:column headerText="Name" sortBy="#{app.appName}">  
                    <h:outputText value="#{app.appName}"/>  
                </p:column>

                <p:column headerText="Vendor" sortBy="#{app.vendorName}" >  
                    <h:outputText value="#{app.vendorName}"/>  
                </p:column>

                <p:column headerText="License required" sortBy="#{app.licenseRequired}">  
                    <h:outputText value="#{app.licenseRequired}"/>  
                </p:column>

            </p:dataTable>

            <ex:exporter target=":tab:applications" fileName="Applications" />

        </p:tab>

    </p:tabView>

    <!--DIALOGS ARE NOT PRESENT SINCE POST IS TOO LONG-->

        </h:body>

</ui:component>

HomeBean.java:

@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);
    }
    //Getters/setters

}

我的目标是找到在数据表中显示设置值(在计算机中设置)的最佳方法,或者以其他方式管理从选定计算机中添加应用程序和从中删除应用程序。

每个答案都受到高度赞赏,并迅速做出回应!

谢谢你。

紫水晶

最后,我将PrimeFaces的<picklist>元素用作本期杂志的最佳方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

显示和编辑存储在 SharedPreferences 中的值

来自分类Dev

在primefaces数据表中显示键和值映射

来自分类Dev

在primefaces数据表中显示键和值映射

来自分类Dev

无法使用JavaScript和Primefaces正确设置值

来自分类Dev

如何在Razor中设置数值编辑器的最小值,最大值和默认值

来自分类Dev

如何在 express EJS 中显示数字和选择字段的编辑值

来自分类Dev

在网格和编辑窗口的组合框中显示条目值。Extjs6

来自分类Dev

Primefaces数据表单元格编辑新值和旧值

来自分类Dev

从 Fetch 中编辑和返回值

来自分类Dev

如何使值的外键显示在primefaces数据表中

来自分类Dev

如何正确设置setState和useEffect以从React中的对象读取和显示值(挂钩)?

来自分类Dev

如何设置编辑器在PrimeFaces中始终处于活动状态?

来自分类Dev

如何从 Primefaces 中的数据表列中获取预编辑的值?

来自分类Dev

即使设置了值,Chrome和Opera也会在日期输入中显示占位符

来自分类Dev

在编辑菜单中从模型设置下拉列表值

来自分类Dev

编辑和删除在带有Primefaces的CRUD中不起作用

来自分类Dev

在编辑页面中显示复选框值

来自分类Dev

在Laravel 5中编辑Restful表单时无法显示值

来自分类Dev

在react-admin中显示已编辑的值

来自分类Dev

在Laravel 5中编辑Restful表单时无法显示值

来自分类Dev

如何在编辑页面中显示选定的值

来自分类Dev

PHP - 值未显示在编辑表单中

来自分类Dev

如何在表单中编辑和保存设置?

来自分类Dev

JavaScript和编辑值

来自分类Dev

在delphi中的dbgrid中编辑和更新值

来自分类Dev

是否可以在Primefaces中编辑树表?

来自分类Dev

Angular JS在textarea中显示和编辑模型

来自分类Dev

单独的UIHints可在mvc4中显示和编辑

来自分类Dev

Angular JS在textarea中显示和编辑模型

Related 相关文章

  1. 1

    显示和编辑存储在 SharedPreferences 中的值

  2. 2

    在primefaces数据表中显示键和值映射

  3. 3

    在primefaces数据表中显示键和值映射

  4. 4

    无法使用JavaScript和Primefaces正确设置值

  5. 5

    如何在Razor中设置数值编辑器的最小值,最大值和默认值

  6. 6

    如何在 express EJS 中显示数字和选择字段的编辑值

  7. 7

    在网格和编辑窗口的组合框中显示条目值。Extjs6

  8. 8

    Primefaces数据表单元格编辑新值和旧值

  9. 9

    从 Fetch 中编辑和返回值

  10. 10

    如何使值的外键显示在primefaces数据表中

  11. 11

    如何正确设置setState和useEffect以从React中的对象读取和显示值(挂钩)?

  12. 12

    如何设置编辑器在PrimeFaces中始终处于活动状态?

  13. 13

    如何从 Primefaces 中的数据表列中获取预编辑的值?

  14. 14

    即使设置了值,Chrome和Opera也会在日期输入中显示占位符

  15. 15

    在编辑菜单中从模型设置下拉列表值

  16. 16

    编辑和删除在带有Primefaces的CRUD中不起作用

  17. 17

    在编辑页面中显示复选框值

  18. 18

    在Laravel 5中编辑Restful表单时无法显示值

  19. 19

    在react-admin中显示已编辑的值

  20. 20

    在Laravel 5中编辑Restful表单时无法显示值

  21. 21

    如何在编辑页面中显示选定的值

  22. 22

    PHP - 值未显示在编辑表单中

  23. 23

    如何在表单中编辑和保存设置?

  24. 24

    JavaScript和编辑值

  25. 25

    在delphi中的dbgrid中编辑和更新值

  26. 26

    是否可以在Primefaces中编辑树表?

  27. 27

    Angular JS在textarea中显示和编辑模型

  28. 28

    单独的UIHints可在mvc4中显示和编辑

  29. 29

    Angular JS在textarea中显示和编辑模型

热门标签

归档