Servlet将对象传递回JSP时出错

时间:2018-12-21 06:29:29

标签: jsp servlets el

我正在研究this JSP Murach book中的教程。我正在研究的示例要求用户在index.jsp的三个字段中输入文本。这些字段将其条目发送到servlet,并且servlet doPost测试三个字段中的任何一个是否为空。如果发生这种情况,将再次分派index.jsp,并且所有非空白的字段都将使用其先前的条目填充。我相信我已经完成了本书显示的内容以及this site shows under 3. Passing Objects的内容,但是仍然出现以下错误。

  

javax.el.PropertyNotFoundException:无法读取属性[inputA]   输入[package01.User]

也许我的User类设置不正确?我不太了解JSP应该如何理解“消息”或“用户”。显然,它不了解inputA,inputB,inputC的“用户”属性。

enter image description here

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>Index JSP</title>
  </head>
  <body>
  <p>Index JSP body</p>
  <p>${message}</p>
  <form action="servlet01" method="post">
    <input type="hidden" name="nextPage" value="revisitIndex">
    Input A:<input title="A" type="text" name="inputA" value="${user.inputA}"><br>
    Input B:<input title="B" type="text" name="inputB" value="${user.inputB}"><br>
    Input C:<input title="C" type="text" name="inputC" value="${user.inputC}"><br>
    <input type="submit" value="Next Page">
  </form>
  </body>
</html>

Servlet01 doPost

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

        String nextPage = request.getParameter("nextPage");
        if (nextPage == null){
            nextPage = "continueToThanks";
        }

        String url = "/thanks.jsp";
        String message = "";
        if(nextPage.equals("revisitIndex")){
            String inputA = request.getParameter("inputA");
            String inputB = request.getParameter("inputB");
            String inputC = request.getParameter("inputC");
            User user = new User(inputA, inputB, inputC);

            if(inputA == null || inputB == null || inputC == null ||
                    inputA.isEmpty() || inputB.isEmpty() || inputC.isEmpty()){

                url = "/index.jsp";
                message = "Please fill out all three text boxes.";
            }

            request.setAttribute("message", message);
            request.setAttribute("user", user);
        }

        getServletContext()
                .getRequestDispatcher(url)
                .forward(request, response);
    }

用户类别

package package01;

class User {
    private String inputA;
    private String inputB;
    private String inputC;

    User(String a, String b, String c){
        inputA = a;
        inputB = b;
        inputC = c;
    }

    public void setInputA(String a){ inputA = a; }
    public void setInputB(String b){ inputB = b; }
    public void setInputC(String c){ inputC = c; }

    public String getInputA(){ return inputA;}
    public String getInputB(){ return inputB;}
    public String getInputC(){ return inputC;}
}

1 个答案:

答案 0 :(得分:0)

正如JB Nizet所说,我的User类必须是公开的。如果没有public access修饰符,则名为“ package01”的包之外的任何内容都无法访问该类。