JSP中的JavaBean

问题描述 投票:1回答:1

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

这些都是我的代码的附件。

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>

咖啡过程.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状态500

某个人 :c

jsp netbeans web-applications glassfish javabeans
1个回答
1
投票

你已经得到了这个错误,因为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>
© www.soinside.com 2019 - 2024. All rights reserved.