English 中文(简体)
带有jsp:useBean的Javabeans。它们是如何工作的?我不明白
原标题:Javabeans with jsp:useBean. How do they work? I don t understand

I have to get 2 numbers and an operation from a jsp file, using a java bean. After submitting the numbers, take them to a servlet from that Java Bean and return an result of them. Problem is that the java bean fields are never completed with the numbers written in textboxes. So, I have, index.jsp s body:

<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)));
}
}

In the Apache log I have next output from the if statement(see the servlet - System.out.println(binOp.getNr1() + binOp.getNr2() + binOp.getOp());): nullnullnull

我的错误在哪里?

最佳回答

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

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

这起了“魔法”。

问题回答

bean只是一个Java类,用于混合使用getter和setter的properites。它没有任何魔法属性,也不会自行填充。它只是一种物体,就像一种图案。这就是他们的工作方式。必须使用适当的setSmth方法手动填充所需的属性。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签