servlet 将对象传回 JSP 时出错
Error when servlet passes object back to JSP
我正在学习 this JSP Murach book. The example I am working through asks the user to enter text in three fields in index.jsp. The fields send their entries to the servlet and the servlet doPost tests whether any of the three fields were left blank. If this occurs, index.jsp is dispatched again, and any fields which were not blank are filled with their previous entries. I believe I have done this as the book shows as well as what this site shows under 3. Passing Objects 的教程,但我仍然收到以下错误。
javax.el.PropertyNotFoundException: Property [inputA] not readable on
type [package01.User]
也许我的用户 class 设置不正确?我真的不明白 JSP 应该如何理解 "message" 或 "user"。显然,它不理解 inputA、inputB、inputC 的 "user" 属性。
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);
}
用户class
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;}
}
正如 JB Nizet 评论的那样,我的用户 class 需要 public。如果没有 public 访问修饰符,名为 "package01" 的包之外的任何内容都不能访问 class.
我正在学习 this JSP Murach book. The example I am working through asks the user to enter text in three fields in index.jsp. The fields send their entries to the servlet and the servlet doPost tests whether any of the three fields were left blank. If this occurs, index.jsp is dispatched again, and any fields which were not blank are filled with their previous entries. I believe I have done this as the book shows as well as what this site shows under 3. Passing Objects 的教程,但我仍然收到以下错误。
javax.el.PropertyNotFoundException: Property [inputA] not readable on type [package01.User]
也许我的用户 class 设置不正确?我真的不明白 JSP 应该如何理解 "message" 或 "user"。显然,它不理解 inputA、inputB、inputC 的 "user" 属性。
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);
}
用户class
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;}
}
正如 JB Nizet 评论的那样,我的用户 class 需要 public。如果没有 public 访问修饰符,名为 "package01" 的包之外的任何内容都不能访问 class.