mercredi 6 septembre 2017

Java Spring Web MVC sending multiple data from one "input" which is increased through "for" loop

I don't know any more clear explanation of my problem to put in title of question. I'm working on one web shop app as practice for learning java web programming, and I got problem, which I am trying to solve for 2 days.

Info about the problem: I have .jsp page which shows the list of all products, and on that page I made something like input tag for each product from list of ALL products. Products are written from database, and now it can be 1 product, tomorrow it can be 101. So I put input tag in "for" loop, iterate through all products and "put" one input tag for each product. Input tag is there so users can write in the quantity of each product they want to buy.

Now problem is I don't know how to recieve all that input tags to my controller, so I can use these inputs to substract that numbers from numbers in Product database -> quantity column.

Note: in storecontroller I didn't finish code, since I don't know what to write there to work, I tried some things, but neither worked.

Here is .jsp page and controller:

store.jsp

...
<body>
    <p>Please select products You want to buy, than press "BUY" button.</p>
    <table border="2px solid" style="width:400px; height:50px;">
        <tr style="width:400px; height:50px; text-align: center;">
            <td>Product</td>
            <td>Price</td>
            <td>Quantity</td>
        </tr>
        <%
            ArrayList<Product> allProducts = Product.allProducts();
            for(Product prod : allProducts) {
        %> 
        <tr>
            <td> <%= prod.getProduct() %> </td>
            <td> <%= prod.getPrice() %> </td>
            <td> <%= prod.getQuantity() %> </td>
        </tr>
        <% } %>
    </table>
    <hr>
    <form action="store.htm" style="border: 1px solid white;">
        <%
            for(Product produ : allProducts) {
        %> 
        <div style="border: 1px solid black; width:25%; display:inline-block;">
            <input type="number" name="quantity" value="<%= produ.getProduct() %>" placeholder="Quantity to buy" style="width:100px; margin-left: 5px; margin-top: 5px;"><br><p style="margin-left: 5px; margin-bottom: 0; padding-bottom: 0;"> Product: <%= produ.getProduct() %>,<br> Price: <%= produ.getPrice() %> </p><br><br>
        </div>
        <% } %>
        <div class="field">
            <hr> 
            <input type="submit" class="buttonSub" value="Buy" />
        </div>
    </form>

</body>

And here is storecontroller.java

...
@Controller
public class StoreController {

@RequestMapping(value = "/store", method = RequestMethod.GET)
public String newForm(Model model) throws ClassNotFoundException {
    model.addAttribute("product", Product.allProducts());

    return "store";
}

@RequestMapping(value = "/store", method = RequestMethod.POST)
public String sum(@ModelAttribute("product")ArrayList<Product> allProducts, BindingResult result, ModelMap model) {

    try (Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/shop", "root", "root");) {

        Statement st = conn.createStatement();
        st.executeQuery("SELECT * FROM ROOT.PRODUCTS");
        ResultSet res = st.getResultSet();
        ArrayList<Product> dbproducts = new ArrayList<Product>();
        while(res.next()) {
                Product p = new Product(res.getString("product"),res.getString("price"),res.getString("quantity"));
                dbproducts.add(p);
        }

        for (Product prod : allProducts) {
            for (Product pro : dbproducts) {

            }
        }

    } catch (SQLException ex) {
        System.out.println("Error in database connection: \n" + ex.getMessage());
    }
    return "store";
}

}




Aucun commentaire:

Enregistrer un commentaire