【问题标题】:Collect input from a form in second page without losing input from form in first page从第二页的表单收集输入,而不会丢失第一页表单的输入
【发布时间】:2015-12-01 17:26:56
【问题描述】:

我试图让用户输入某个项目代码,然后将他们带到一个页面,在那里他们可以根据他们输入的代码从数据库中查看代码和项目信息。在第二页上,我希望用户能够使用另一个提交按钮输入另一个项目代码(在表格下方),该按钮将使用他们添加的从数据库中获取的另一个项目更新表格。如何在不丢失表上以前数据的情况下执行此操作?

到目前为止的代码:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Scanned Items</title>
    </head>
    <body> 
    <h1 align="center"><u><font color="blue">List of Scanned Items</font></u></h1>

    <table align="center">
        <tr>
            <th> Item Code </th>
            <th> Name </th>
            <th> Price </th>
            <th> Taxable </th>
            <th> Tax </th>
        </tr>
                <tr>
                    <td> ${productcatalogue.code}</td>
                    <td> ${productcatalogue.name}</td>
                    <td> $${productcatalogue.price}</td>
                    <td> ${productcatalogue.taxable}</td>
                    <td> </td>
                </tr>
    </table>
</body>
</html>

Servlet 代码:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

    DBConnection dbConn = (DBConnection)request.getServletContext()
        .getAttribute("dbConn");

    ProductCatalogueDAO productcatalogueDAO = new ProductCatalogueDAO();
    String itemcode = request.getParameter("itemcode");
    ProductCatalogue productcatalogue = productcatalogueDAO.getItems(dbConn.getConnection(), itemcode);
    request.setAttribute("productcatalogue", productcatalogue);

    RequestDispatcher view = request.getRequestDispatcher
    (response.encodeURL("scannedItems.jsp"));
    view.forward(request, response);  
}

【问题讨论】:

    标签: jsp servlets jstl


    【解决方案1】:

    我可以给你概述来处理它,我只是没有使用数据库来处理它 存储它,但剩下的事情我做了。

    只要按照这个顺序,你就会得到你的解决方案,

    只需创建一个 jsp 文件,在我的例子中是 a1.jsp

    ------------------------ a1.jsp 代码---------------- -----------------

    <%@page import="java.util.ArrayList"%>
    <%@page import="com.stackoverflow.ItemBean"%>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    
        <table border="2">
            <tr>
                <th>Item Code</th>
                <th>Name</th>
                <th>Price</th>
                <th>Taxable</th>
                <th>Tax</th>
                <th>Action</th>
            </tr>
    
            <%
            ArrayList<ItemBean> list = new ArrayList<ItemBean>();
               if(session.getAttribute("allitems") != null){
                   list =  (ArrayList<ItemBean>)session.getAttribute("allitems");   
               }
    
                for(ItemBean ib : list){
                    %>
                    <tr>
                        <td><%= ib.getItemCode() %></td>
                        <td><%= ib.getItemname() %></td>
                        <td><%= ib.getPrice() %></td>
                        <td><%= ib.getTaxable() %></td>
                        <td><%= ib.getTax() %></td>
                        <td><i>Saved</i></td>
                    </tr>   
    
                    <%}%>
    
            <form action="ItemSubmit.do" method="post">
            <tr>
                <td><input type="text" name="itemCode" value="" /></td>
                <td><input type="text" name="itemname" value="" /></td>
                <td><input type="text" name="price" value="" /></td>
                <td><select name="taxable">
                        <option value="yes">Yes</option>
                        <option value="no">No</option>
                </select></td>
                <td><input type="text" name="tax" value="" /></td>
                <td><input type="submit" value="Add" /></td>
            </tr>
            </form>
        </table>
    
    
    </body>
    </html>
    

    --------------- ItemSubmit.java servlet 代码 -----

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
            HttpSession session = request.getSession();
            ArrayList<ItemBean> listBean = new ArrayList<ItemBean>();
    
            String itemCode = request.getParameter("itemCode");
            String itemname = request.getParameter("itemname");
            double price = 0;
            String taxable = request.getParameter("taxable");
            double tax = 0;
            String remark = "saved";
    
            try {
                price = Double.parseDouble(request.getParameter("price")) ;
                tax =   Double.parseDouble(request.getParameter("tax"));
            } catch (Exception e) {
                remark = e.getMessage();
            }
    
            if(session.getAttribute("allitems") != null){
                listBean = (ArrayList<ItemBean>)session.getAttribute("allitems");
            }
    
            ItemBean itemBean = new ItemBean(itemCode, itemname, price, taxable, tax,remark);
            //save itemBean to DB-if needed then...
            listBean.add(itemBean);
            session.setAttribute("allitems",listBean);
    
            request.getRequestDispatcher("/a1.jsp").forward(request, response);
    
        }
    

    -------- ItemBean.java 普通 java 类 ---------------

    package com.stackoverflow;
    
    public class ItemBean {
            String itemCode;
            String itemname;
            double price;
            String taxable;
            double tax;
            String remark;
    
            public ItemBean(String itemCode, String itemname, double price, String taxable,double tax , String remark ) {
                this.itemCode = itemCode;
                this.itemname = itemname;
                this.price = price;
                this.taxable = taxable;
                this.tax  = tax;
                this.remark = remark;
            }
    
    
            public String getItemCode() {
                return itemCode;
            }
            public void setItemCode(String itemCode) {
                this.itemCode = itemCode;
            }
            public String getItemname() {
                return itemname;
            }
            public void setItemname(String itemname) {
                this.itemname = itemname;
            }
            public double getPrice() {
                return price;
            }
            public void setPrice(double price) {
                this.price = price;
            }
            public String getTaxable() {
                return taxable;
            }
            public void setTaxable(String taxable) {
                this.taxable = taxable;
            }
            public double getTax() {
                return tax;
            }
            public void setTax(double tax) {
                this.tax = tax;
            }
    
            public String getRemark() {
                return remark;
            }
    
            public void setRemark(String remark) {
                this.remark = remark;
            }
    
    }
    

    --------------- 输出 --------

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 2015-08-17
      • 2011-01-12
      • 1970-01-01
      相关资源
      最近更新 更多