代码之家  ›  专栏  ›  技术社区  ›  m88

带有jsp:usebean的JavaBeans。它们是如何工作的?我不明白

  •  1
  • m88  · 技术社区  · 14 年前

    我必须使用JavaBean从JSP文件中获得2个数字和一个操作。提交这些数字后,将它们从Java bean中调用servlet并返回它们的结果。问题是JavaBean字段从来没有用文本框中的数字完成。 所以,我有,index.jsp的主体:

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>
        <h1>Calculator</h1>
        <FORM METHOD="POST" action="Controller">
            N1: <input type ="text" name="nr1" value="0">
            op: <input type ="text" name="op" value="+">
            N2: <input type ="text" name="nr2" value="0">
            <INPUT class ="button" TYPE="submit" NAME="actiune" VALUE="Calculate"/>
        </FORM>
        <jsp:useBean id="binOp" class="beans.BinaryOperation" scope="session"/>
        <jsp:setProperty name="binOp" property="*"/>
    </body>
    

    servlet的processrequest方法controller.java,放在包servlet中:

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        BinaryOperation binOp = (BinaryOperation) session.getAttribute("binOp");
    
        try {
            if (!binOp.isComplete()) {
                System.out.println(binOp.getNr1() + binOp.getNr2() + binOp.getOp());
                response.sendRedirect("index.jsp");
            } else {
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet Controller</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Bean in controller " + binOp.getNr1() + "__" + binOp.getOp() + "__" + binOp.getNr2() + "</h1>");
                out.println(binOp.toString());
                out.println("</body>");
                out.println("</html>");
            }
    
    
    
        } finally {
            out.close();
        }
    }
    

    而bean,binaryoperation,放在包bean中:

    package beans;
    
    
    public class BinaryOperation {
    
    private String nr1;
    private String op;
    private String nr2;
    
    public void setNr1(String nr1) {
        this.nr1 = nr1;
    }
    
    public void setOp(String op) {
        this.op = op;
    }
    
    public void setNr2(String nr2) {
        this.nr2 = nr2;
    }
    
    public String getNr1() {
        return nr1;
    }
    
    public String getOp() {
        return op;
    }
    
    public String getNr2() {
        return nr2;
    }
    
    public boolean isComplete() {
        return !(((nr1 == null) || (nr1.length() == 0))
                || ((op == null) || (op.length() == 0))
                || ((nr2 == null) || (nr2.length() == 0)));
    }
    }
    

    在Apache日志中,我有来自if语句的下一个输出(请参见servlet-system.out.println(binop.getnr1()+binop.getnr2()+binop.getop());): 零空

    我的错误在哪里?

    2 回复  |  直到 11 年前
        1
  •  0
  •   Boris Hamanov    14 年前

    Bean只是一个Java类,用于使用GETER和SETTER的HODLIN属性。它没有任何魔法属性,也不填充自己。它只是一种物体,像一个图案。这就是他们的工作方式。您必须用appoplate setsmth方法手动填充所需的属性。

        2
  •  0
  •   m88    14 年前

    我在index.jsp和servlet之间又做了一个jsp文件:

     <jsp:useBean id="binOp" class="beans.BinaryOperation" scope="session"/>
    <jsp:setProperty name="binOp" property="*"/>.
    

    这就是“魔法”。