【问题标题】:JSP how to link between 2 java classes to add items to arraylist and display them in JSPJSP如何在2个java类之间链接以将项目添加到arraylist并在JSP中显示它们
【发布时间】:2016-04-02 02:25:18
【问题描述】:

嘿,我是 JSP 新手,想知道如何解决一个小问题。我不确定如何在 Client.java 和 Item.java 之间建立链接,以便能够将新项目添加到 ArrayList 并在 jsp 页面上显示。

这是我的 Client.java 代码: 包javaCode;

import java.util.ArrayList;
public class Client {
    private String name;
    private String email;
    private int phone;
    private String address;
    private String country;
    private String city;
    public ArrayList<Item> list;
    private String shipping;
    private int fee;
    private double discount;
    private Item item;


    public Client() {
     list = new ArrayList<Item>();
    }

    public void setName(String name) {
     this.name = name;
    }

    public String getName() {
     return name;
    }

    public void setEmail(String email) {
     this.email = email;
    }

    public String getEmail() {
     return email;
    }

    public void setPhone(int phone) {
     this.phone = phone;
    }

    public int getPhone() {
     return phone;
    }

    public void setAddress(String address) {
     this.address = address;
    }

    public String getAddress() {
     return address;
    }

    public void setCountry(String country) {
     this.country = country;
    }

    public String getCountry() {
     return country;
    }

    public void setCity(String city) {
     this.city = city;
    }

    public String getCity() {
     return city;
    }

    public void setShipping(String shipping) {
     this.shipping = shipping;
    }

    public String getShipping() {
     return shipping;
    }

    public void setFee() {
     if(this.shipping == "") {
        this.fee = 0;
     }
     if(this.shipping == "USPS Express Mail") {
        this.fee = 200;
     }
     if(this.shipping == "USPS Priority Mail") {
        this.fee = 300;
     }
     if(this.shipping == "DHL") {
        this.fee = 400;
     }
     if(this.shipping == "FedEx") {
        this.fee = 50;
     }
    }

    public int getFee() {
     return fee;
    } 

    public void setDiscount(double discount) {
     this.discount = discount;
    }

    public double getDiscount() {
     return discount;
    }

    public void setItem(Item item) {
     list.add(this.item);
    }

    public ArrayList<Item> getList() {
     return list;
    }
}

这是我的商品代码: 包javaCode;

 public class Item {
  private String item;
  private int quantity;
  private double price;

  public Item() {
  }

  public void setItem(String item) {
    this.item = item;
  }

  public String getItem() {
    return item;
  }

  public void setQuantity(int quantity) {
    this.quantity = quantity;
    //Client.list.add(this);
  }

  public int getQuantity() {
    return quantity;
  }

  public void setPrice(double price) {
    this.price = price;
  }

  public double getPrice() {
    return price;
  }

  public double getTotal() {
    return (quantity*price);
  }
}

这是 clientdetails.jsp 文件:

<jsp:useBean id="iteminfo" class="javaCode.Item"/> 
<jsp:setProperty property="*" name="iteminfo"/> 
<jsp:useBean id="userinfo" class="javaCode.Client" scope="request"/>
<jsp:setProperty property="*" name="userinfo"/> 
<h2>Personal Information</h2>
<table border="1">
<tr>
    <td>Name</td>
    <td><jsp:getProperty property="name" name="userinfo"/></td>
</tr>
<tr>
    <td>E-mail</td>
    <td><jsp:getProperty property="email" name="userinfo"/></td>
</tr>
<tr>
    <td>Phone</td>
    <td><jsp:getProperty property="phone" name="userinfo"/></td>
</tr>
<tr>
    <td>Address</td>
    <td><jsp:getProperty property="address" name="userinfo"/></td>
</tr>
<tr>
    <td>Country</td>
    <td><jsp:getProperty property="country" name="userinfo"/></td>
</tr>
<tr>
    <td>City</td>
    <td><jsp:getProperty property="city" name="userinfo" /></td>
</tr>
<tr>
    <td>Shipping Method</td>
    <td><jsp:getProperty property="shipping" name="userinfo"/></td>
</tr>
<tr>
    <td>Shipping Amount</td>
    <td><jsp:getProperty property="fee" name="userinfo"/></td>
</tr>
<tr>
    <td>Discount</td>
    <td><jsp:getProperty property="discount" name="userinfo"/></td>
</tr>
<tr>
    <td>List</td>
    <td><jsp:getProperty property="list" name="userinfo"/></td>
</tr>
</table>
<br><br>
<table border="1">
<tr>
    <td>Name</td>
    <td>Item1</td>
</tr>
<tr>
    <td>Quantity</td>
    <td><jsp:getProperty property="quantity" name="iteminfo"/></td>
</tr>
<tr>
    <td>Unit Price</td>
    <td><jsp:getProperty property="price" name="iteminfo"/></td>
</tr>
<tr>
    <td>Total</td>
    <td><jsp:getProperty property="total" name="iteminfo"/></td>
</tr> 
</table>

这是form.jsp文件(包含html表单):

 <html>
    <head>
    <title>Order Form</title>
    <link rel="stylesheet" type="text/css" href="formstyle4.css"> 
    <script type="text/javascript"></script>
    </head>
    <body>
    <form name="order" id="orderForm" action="clientdetails.jsp"   method="post">
        <p id="date"><script type="text/javascript"></script></p>
        <h2>Personal Information</h2>
        <label id="fname">* Name</label><input type="text" name="name" id="name" onkeypress="return !isNumber(event)" required>
        <label id="labemail">E-mail</label><input type ="text" name="email" id="email" onblur="validateEmail(this)" required><br/><br/>
        <label for="address" id="labaddr">* Address</label>
        <textarea rows="4" cols="25" id="address" name="address" required></textarea>
        <label id="labcountry">Country</label>
        <select id="country" name="country">
            <option value=""></option>
            <option value="Lebanon">Lebanon</option>
            <option value="UAE">UAE</option>
            <option value="United States">United States</option>
            <option value="France">France</option>
            <option value="Italy">Italy</option>
        </select><br/><br/>
        <label id="labphone">* Phone</label><input type="text" name="phone" id="phone" onkeypress="return isNumber(event)" onblur="validPhone()" required>
        <label id="labcity">City</label><input type="text" name="city" id="city" onkeypress="return !isNumber(event)" required><br><br>
        <br><br>
        <table id="orderTable">
            <th colspan="4">Order Details</th>
            <tr id="first">
                <td>Item</td>
                <td>Quantity</td>
                <td>Unit Price</td>
                <td>Total</td>
            </tr>
            <tr>
                <td id="item">Item1</td>
                <td><input onblur="findTotal(1)" class="item" type="text" name="quantity" id="quantity" onkeypress="return isNumber(event)"/></td>
                <td><input onblur="findTotal(1)" class="item" type="number" min="0" step="0.01" name="price" id="price" onkeypress="return isNumber(event)"/></td>
                <td><input onblur="totals()" class="item" type="text" name="total" id="total" readonly></td>
            </tr>
            <!--
            <tr>
                <td id="item">Item2</td>
                <td><input onblur="findTotal(2)" type="text" name="quantity" id="quantity" onkeypress="return isNumber(event)"/></td>
                <td><input onblur="findTotal(2)" type="text" name="price" id="price" onkeypress="return isNumber(event)"/></td>
                <td><input onblur="totals()" type="text" name="total" id="total" readonly></td>
            </tr>
            <tr>
                <td id="item3">Item3</td>
                <td><input onblur="findTotal(3)" type="text" name="val3" id="qt3" onkeypress="return isNumber(event)"/></td>
                <td><input onblur="findTotal(3)" type="text" name="val3" id="price3" onkeypress="return isNumber(event)"/></td>
                <td><input onblur ="totals()" type="text" name="total" id="total3" readonly></td>
            </tr> -->
            <tr>
                <td id="colshipping">Shipping Method</td>
                <td colspan="2">
                <select id="shipping" name="shipping">
                    <option value="empty"></option>
                    <option value="USPS Express Mail">USPS Express Mail</option>
                    <option value="USPS Priority Mail">USPS Priority Mail</option>
                    <option value="DHL">DHL</option>
                    <option value="FedEx">FedEx</option>
                </select>
                </td>
                <td><input type="text" id="fee" onblur="totals()" readonly></td>
            </tr>
            <tr>
                <td id="coldis">Discount</td>
                <td colspan="2"><input onchange="totals()" type="text" name="discount" id="discount" onkeypress="return isNumber(event)" required> %</td>
                <td><input type="text" onchange="totals()" id="amount" name="amount" readonly></td>
            </tr>
            <tr>
                <td id="coltotal" colspan="3">Total</td>
                <td><input type="text" name="totalPrice" id="totprice" readonly></td>
            </tr>
        </table> 
        <br>
        <input type="submit" id="sub" value="Submit"> 
    </form>
    </body>
</html>

我的问题是,每当我在“form.jsp”中填写信息时,“clientdetails.jsp”就会打开并显示我输入的详细信息,但列表元素除外,它一直显示为空。我不确定如何能够在项目类和客户端类之间进行连接,以便能够在列表中显示 Item1 实例。他们告诉我,也许我应该对setItem(Item item) 方法进行一些修改? 请任何提示或建议可能对我有很大帮助!谢谢!

【问题讨论】:

    标签: java jsp arraylist hyperlink


    【解决方案1】:

    在下面的方法中,

    public void setItem(Item item) {
             list.add(this.item);
            }
    

    你为什么要list.add(this.item);?不应该是list.add(item);吗?

    将其更改为list.add(item);

    【讨论】:

    • 我尝试这样做仍然无法正常工作..所以我将 system.out.println("set item") 语句放在 setItem 方法中没有看到任何打印内容,因此似乎服务器没有进入这种方法,但我真的不知道为什么
    • 在你的form.jsp上,哪一行应该调用setter方法setItem(Item item)
    • 这就是问题所在..form.jsp 上什么都没有..在 form.jsp: Item1
    • 在上面的这段代码中,用户应该填写信息,因此将创建一个项目实例,之后我希望将此实例添加到客户端代码中的数组列表中
    • &lt;INPUT&gt; 字段和 useBean bean 字段之间的自动绑定仅在名称匹配时才会发生。阅读一篇关于 usebean 标签的好文章。另外,我不确定您在 clientdetails.jsp 中使用的 usebean 标签,我认为应该在 form.jsp
    猜你喜欢
    • 1970-01-01
    • 2014-01-31
    • 2015-04-15
    • 2014-04-19
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    相关资源
    最近更新 更多