【问题标题】:Why does id remain zero? [duplicate]为什么 id 保持为零? [复制]
【发布时间】: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

【问题讨论】:

标签: jsf


【解决方案1】:

我认为问题在于 bean 中的 @RequestScoped 注释。使用此注释数据仅“存在”在当前页面中。当您重定向到另一个 URL(尽管它是同一个 URL)时,您会丢失所做的更改(更多地考虑到您将客户置于会话映射中。请尝试改用 @SessionScoped

希望它有效。

【讨论】:

猜你喜欢
  • 2021-11-11
  • 2013-05-06
  • 2018-06-11
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 2012-02-20
  • 2018-09-14
  • 2017-11-12
相关资源
最近更新 更多