繁体   English   中英

为什么不从表单值创建请求范围的bean?

[英]Why request-scoped bean is not created from form values?

我已经创建了这样的JSP文件:

<jsp:useBean id="ucz" class="pl.lekcja.beany.beany.Uczen" scope="request">
    <jsp:setProperty name="ucz" property="*"/> 
</jsp:useBean>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Podaj dane ucznia:</h1>

        <form method="POST" action="Ocen">
            <table>
                <tr>
                    <td>Imie:</td>
                    <td><input type="text" name="imie" /></td>
                </tr>
                <tr>
                    <td>Nazwisko:</td>
                    <td><input type="text" name="nazwisko" /></td>
                </tr>
                <tr>
                    <td>Punkty:</td>
                    <td><input type="text" name="punkty" /></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="Oceń" /></td>
                </tr>
            </table>
        </form>
    </body>
</html>

与豆类:

import java.io.Serializable;

public class Uczen implements Serializable {
    private String imie, nazwisko;
    private int punkty;

    public Uczen() {

    }

    public Uczen(String imie, String nazwisko, int punkty) {
        this.imie = imie;
        this.nazwisko = nazwisko;
        this.punkty = punkty;
    }

    public String getImie() {
        return imie;
    }

    public void setImie(String imie) {
        this.imie = imie;
    }

    public String getNazwisko() {
        return nazwisko;
    }

    public void setNazwisko(String nazwisko) {
        this.nazwisko = nazwisko;
    }

    public int getPunkty() {
        return punkty;
    }

    public void setPunkty(int punkty) {
        this.punkty = punkty;
    }
}

和Servlet:

public class Ocen extends HttpServlet {

    private static final int PROG_PUNKTOWY = 50;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Uczen uczen = (Uczen)request.getAttribute("ucz");
        System.out.println(uczen); // <---- here prints null, always, there's no "uczen" object in attributes
        String czyZdal = "nie ";

        if (uczen.getPunkty() >= PROG_PUNKTOWY) {
            czyZdal = " ";
        }

        request.setAttribute("czyZdal", czyZdal);
        request.getRequestDispatcher("/WEB-INF/wynik.jsp").forward(request, response);
    }
}

正如我在Servlet代码中所写的那样,有一点总是打印null,而不是创建的bean类。 没有创建Bean或未将其添加到属性中。

doRequest()和doPost()都调用processRequest()

这段代码有什么问题?

您将请求发布到Ocen servlet。 当执行servlet时,JSP尚未执行,因此jsp:useBean尚未执行,因此Bean不在请求中。

jsp:useBean不应该再使用了。 请求参数应该在控制器servlet中读取,而不是在JSP中读取。 您应该使用Spring MVC或Stripes之类的MVC框架,该框架会自动将请求参数转换为表单bean,并将该表单bean传递给操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM