问题描述
我有一个名为“Commande”的 Bean,其属性为“client”和 Vector“achats”
这个向量用于放置购买的物品。
所以当我调用“achat.html”并输入我的产品名称时,他会调用“panier.PHP”将它放在这个向量上
当我去“AfficherPanier.jsp”时,他会打印购买的文章
单击“afficherPanier.jsp”时出现错误:
消息在类型为 [enis.Commande] 的 bean 中找不到关于属性 [Achats] 的任何信息
achat.html
<!DOCTYPE html>
<html>
<head>
<Meta charset="UTF-8">
<title>Achat</title>
</head>
<body>
<form action="http://localhost:8080/AchatEnLigneMVC/Panier" method=GET>
Entrer l'article à acheter : <input type="text" name="achat"/><br>
<input type="submit" value="Valider"/>
</form>
</body>
</html>
Commande.java
package enis;
import java.util.Vector;
public class Commande {
String client;
Vector Achats = new Vector();
public String getClient() {
return client;
}
public void setClient(String client) {
this.client = client;
}
public Vector getAchats() {
return Achats;
}
public void setAchats(Vector achats) {
Achats = achats;
}
}
```
Panier.java
package enis;
public class Panier extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Panier() {
super();
// Todo Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request,HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws servletexception,IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(true);
Commande cmd= new Commande();
Vector v = new Vector();
String newitem = request.getParameter("achat");
v.addElement(newitem);
cmd.setAchats(v);
session.setAttribute("commande",cmd);
response.sendRedirect("http://localhost:8080/AchatEnLigneMVC/Achat.html");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request,HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,IOException {
}
}
AfficherPanier.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<Meta charset="UTF-8">
<title>Afficher Panier</title>
</head>
<body>
<jsp:useBean id="commande" class="enis.Commande" scope="session"/>
<jsp:getProperty property="Achats" name="commande"/>
</body>
</html>
解决方法
请使用 Java 命名约定:
Vector achats = new Vector();
在 bean 和 JSP 标记中使用小写 achats
后,它将起作用。
另外,不需要初始化Vector
。