JSP 中的 JavaBean

JavaBean in JSP

我是这个代码世界的新手。我正在尝试构建一个简单的 Web 应用程序,它有 CoffeeOrder.html 代表欢迎页面,CoffeeProcess.jsp 处理计算(在这种情况下我没有做任何计算)和 CoffeeBean.java class.

这些是我的代码附件,

CoffeeBean.java

package coffee.bean;

public class CoffeeBean implements java.io.Serializable
{
    private int numSugar;
    private double price;
    private String typeCoffee;

    //default constructor
    public CoffeeBean()
    {}

    //normal constructor
    public CoffeeBean(int numSugar, double price, String typeCoffee)
    {
        this.numSugar = numSugar;
        this.price = price;
        this.typeCoffee = typeCoffee;
    }

    //accessor method
    public int getSugar()
    { return numSugar; }

    public double getPrice()
    { return price; }

    public String getType()
    { return typeCoffee; }

    //mutator method
    public void setSugar(int numSugar)
    { this.numSugar = numSugar; }

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

    public void setCoffee(String typeCoffee)
    { this.typeCoffee = typeCoffee; }

}

CoffeeOrder.html

<!DOCTYPE html>
<html>
    <head>
        <title>Order</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h1>Javabeans in JSP</h1><br>
        <h1>Coffee Order</h1>

        <form action='CoffeeProcess.jsp' method='POST'>
            Type of Coffee  <input type='text' name='typeCoffee'/><br>
            Number of Sugar <input type='text' name='numSugar'/><br>
            Price           <input type='text' name='price'/><br>

            <input type='Submit' value='Submit'/>
        </form>

    </body>

</html>

CoffeeProcess.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Javabeans in JSP</title>
    </head>
    <body>
        <h1>Customer Order</h1>

        <jsp:useBean id = "coffee" scope="request" class= "coffee.bean.CoffeeBean"> 
            <jsp:setProperty name = "coffee" property = "numSugar" value="1"/>
            <jsp:setProperty name = "coffee" property = "price" value="23"/>
            <jsp:setProperty name = "coffee" property = "typeCoffee" value="asf"/>
        </jsp:useBean>

        <p>Type of Coffee:<jsp:getProperty name="coffee" property="typeCoffee"/></p>
        <p>Number of Sugar:<jsp:getProperty name="coffee" property="numSugar"/></p>
        <p>Price:<jsp:getProperty name="coffee" property="price"/></p>

    </body>
</html>

这是我在 CoffeeOrder.html

中单击提交按钮时出现的错误

HTTP Status 500

有人 :c

您遇到此错误是因为 jsp 能够找到 typeCoffee getter setter 方法,因为您为方法指定的名称是 getType()setType()numSugar 相同,也是不匹配的。相反,您的 getter-setter 应该如下所示:

    //getter for numSugar
    public int getNumSugar() {
        return numSugar;
    }
      //setter for numSugar
    public void setNumSugar(int numSugar) {
        this.numSugar = numSugar;
    }


   //getter for typeCoffee
    public String getTypeCoffee() {
        return typeCoffee;
    }
   //setter for typeCoffee
    public void setTypeCoffee(String typeCoffee) {
        this.typeCoffee = typeCoffee;
    }

此外,如果您需要将值从 form 传递到您的 <jsp:setProperty..>,您可以像下面那样做:

  <jsp:useBean id = "coffee" scope="request" class= "coffee.bean.CoffeeBean"> 
      <jsp:setProperty name = "coffee" property = "numSugar" value="${param.numSugar}"/>
      <jsp:setProperty name = "coffee" property = "price" value="${param.price}"/>
      <jsp:setProperty name = "coffee" property = "typeCoffee" value="${param.typeCoffee}"/>
  </jsp:useBean>