gpt4 book ai didi

mysql - 在 PrimeFaces 中显示和编辑设置值

转载 作者:行者123 更新时间:2023-11-29 13:15:13 25 4
gpt4 key购买 nike

我有 JSF 2.2PrimeFaces 3.5Spring 3.1.3Hibernate 4.1 MySQL 网络应用程序。

在 home.xhtml 上,我有一个选项卡 View ,其中包含 3 个实体(用户、计算机、应用程序)的 3 个嵌套数据表和 CRUD 按钮(参见屏幕截图)。每个按钮都会调用一个带有表单的对话框窗口来添加/编辑/删除实体。

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

enter image description here

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

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

应用程序.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> element 作为我的问题中的最佳方法。

关于mysql - 在 PrimeFaces 中显示和编辑设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21557113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com