【发布时间】:2019-11-22 19:41:57
【问题描述】:
我正在做一个学校项目,我需要使用 JSF 来制作 CRUD 应用程序。 我正在使用 MySQL 数据库,并设法制作了所有对象的列表,删除按钮,但我的编辑按钮有问题。
当我单击编辑时,它会将我重定向到 edit.xhtml 页面,获取 id 并根据该 id 填写所有字段。 当我点击编辑页面上的更新按钮时,它总是更改 id=0 的客户。
我有一个包含 getter、setter 和方法的 java 文档 有两个视图 index.xhtml 和 edit.xhtml 以及一页与数据库的连接方法。
所有其他方法都可以正常工作,除了更新。
客户.java
@ManagedBean
@RequestScoped
public class Customer {
private int id;
private String username;
private String adress;
private int quantity;
private double price;
private Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
public String edit() {
FacesContext fc = FacesContext.getCurrentInstance();
Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
String primarId = params.get("action");
System.out.println(primarId);
try {
DatabaseConnection dbc = new DatabaseConnection();
Connection connection = dbc.getConnection();
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("select * from customer where customer_id=" + primarId);
Customer customer = new Customer();
rs.next();
customer.setUsername(rs.getString("username"));
customer.setAdress(rs.getString("adress"));
customer.setQuantity(rs.getInt("quantity"));
customer.setPrice(rs.getDouble("price"));
sessionMap.put("editcustomer", customer);
} catch (SQLException ex) {
System.out.println(ex);
}
return "/edit.xhtml?faces-redirect=true";
}
public String updateCustomer() {
FacesContext fc = FacesContext.getCurrentInstance();
Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
String fieldId = params.get("action");
System.out.println(fieldId);
try {
DatabaseConnection dbc = new DatabaseConnection();
Connection connection = dbc.getConnection();
PreparedStatement ps = connection.prepareStatement("update customer set username=?,adress=?,quantity=?,price=? where customer_id=?");
ps.setString(1, username);
ps.setString(2, adress);
ps.setInt(3, quantity);
ps.setDouble(4, price);
ps.setInt(5, id);
System.out.println(id);
ps.executeUpdate();
} catch (SQLException ex) {
System.out.println(ex);
}
return "/index.xhtml?faces-redirect=true";
}
edit.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<center>
<h:form>
Username: <h:inputText value="#{editcustomer.username}"></h:inputText> <br/>
Adress: <h:inputText value="#{editcustomer.adress}"></h:inputText> <br/>
Quantity: <h:inputText value="#{editcustomer.quantity}"></h:inputText> <br/>
Price: <h:inputText value="#{editcustomer.price}"></h:inputText> <br/><br/>
<h:commandButton value="Update" action="#{editcustomer.updateCustomer()}">
<f:param name="action" value="#{editcustomer.id}" />
</h:commandButton>
</h:form>
</center>
</h:body>
</html>
当我运行此代码时,ID 保持为 0
【问题讨论】:
-
请阅读stackoverflow.com/help/tagging并查看我的编辑
-
哦,请让你的教授教你更好的方法,让他(她)阅读 stackoverflow.com/questions/30639785/… 和 stackoverflow.com/questions/10301363/jpa-entity-as-jsf-bean 并帮助你的 coude 变得更好(通常,与 JSF 无关)。如果你开始错误(就像你做的那样)并且想要重构并且很难,你会倾向于责怪 JSF,而事实上基本的(非 jsf 相关的)方法是错误的)
-
而在现代 JSF 中,您可以执行 `
`,不需要 xhtml 中的参数和在 bean 中检索。更少的代码,更清晰。也请告诉你的教授。
标签: jsf