-1

I'm working on one school project, where i need to use JSF to make CRUD app. I'm using MySQL database, and managed to make list of all objects, delete button, and i have trouble with edit button.

When i click on edit it redirect me to edit.xhtml page, get id and fill all fields based on that id. When i click update button on edit page, it always change customer with id=0.

I have one java doc with getter, setter and methods have two views index.xhtml and edit.xhtml and one page with method for connection with database.

All other methods work fine, except update.

Customer.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>

when I run this code the ID stays 0

Kukeltje
  • 11,924
  • 4
  • 19
  • 44
  • Please read https://stackoverflow.com/help/tagging and see my edit – Kukeltje Nov 23 '19 at 07:44
  • Oh and please have your professor teach you better approaches, have him (her) read https://stackoverflow.com/questions/30639785/jsf-controller-service-and-dao and https://stackoverflow.com/questions/10301363/jpa-entity-as-jsf-bean and help your coude becoming better (generically, not specifically JSF related). If you start wrong (like you do) and want to refactor and it is hard, you'd be inclined to blame JSF while in fact the basic (non jsf related) approach is wrong) – Kukeltje Nov 23 '19 at 07:51
  • And in modern JSF you can do ` `, no need for the param in the xhtml and retrieval in the bean. Less code, more clear. Please also tell this to your professor. – Kukeltje Nov 23 '19 at 07:59
  • and also https://stackoverflow.com/questions/7862700/best-practice-to-get-entitymanagerfactory – Kukeltje Nov 23 '19 at 08:15

1 Answers1

0

I think the problem is the @RequestScoped annotation in the bean. With this annotation data only "lives" in the current page. As you are redirecting to another URL (altough it is the same one), you lose your changes (more taking into account that you are putting the customer in the session map. Try using @SessionScoped instead.

Hope it works.

nimsraw
  • 108
  • 4
  • There are other 'in between' scopes as well via extensions that are imo better suited. The `ViewAccessscope` from DeltaSpike come https://deltaspike.apache.org/documentation/jsf.html#Scopes – Kukeltje Nov 23 '19 at 07:57