【问题标题】:JavaBean in JSPJSP中的JavaBean
【发布时间】:2020-09-26 18:42:12
【问题描述】:

我是这个代码世界的新手。我正在尝试构建一个简单的 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>

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 netbeans web-applications glassfish javabeans


    【解决方案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 传递给您的 &lt;jsp:setProperty..&gt;,您可以执行以下操作:

      <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>
    

    【讨论】:

    • 我没有注意到 xD 非常感谢@Swati
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    相关资源
    最近更新 更多