一个月后阅读帖子帮助我,这里是第一张照片,有点兴奋! :D 我目前正在 scholl 学习 JavaEE,并且在从 JSP 文件中的选择框中获取值时遇到问题。
这就是我想要做的:当我单击“添加到卡”时,从选择框中获取值,将其放入列表中(在 servlet 中),然后将该列表显示回我的 jsp 文件中。我给你我现在的代码(可能是丑陋的,抱歉,第一天......)
Servlet:
public class CommandeServlet extends HttpServlet {
private List listePlatsCommandes = new ArrayList();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doWork(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doWork(request, response);
}
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
CommandeModel beanSessionCommande = (CommandeModel) request.getSession().getAttribute("beanSessionCommande");
if (request.getParameterMap().isEmpty()) {
beanSessionCommande = new CommandeModel();
} else if (request.getParameter("submit") != null) {
listePlatsCommandes.add(request.getParameter("platCommande"));
Commande com = new Commande(request.getParameter("libelle"), listePlatsCommandes);
CommandeManagerSingl.getInstance().ajouter(com);
beanSessionCommande.setCommande(com);
}
System.out.println(listePlatsCommandes);
request.getSession().setAttribute("beanSessionCommande", beanSessionCommande);
request.getRequestDispatcher("/WEB-INF/commande.jsp").forward(request, response);
}
}
型号:
public class CommandeModel {
private List<Plat> plats;
private Commande commande;
public CommandeModel() {
this.plats = new ArrayList<Plat>();
this.commande = new Commande();
}
public List<Plat> getPlats() {
return plats;
}
public void addPlat(Plat plat) {
plats.add(plat);
}
public void setPlats(List<Plat> plats) {
this.plats = plats;
}
public Commande getCommande() {
return commande;
}
public void setCommande(Commande commande) {
this.commande = commande;
}
}
JSP 文件:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Commande</title>
</head>
<body>
<a href="/PlatServlet">Plats</a> - <a href="/CommandeServlet">Commandes</a>
<h1>Gestion des commandes</h1>
<div>
<form action="CommandeServlet">
<select name="listePlats">
<c:forEach var="plat" items="${beanSessionPlat.listePlats}">
<option name="platCommande" value=" ${plat.libelle} " selected="selected"> ${plat.libelle} </option>
</c:forEach>
</select>
<input type="submit" value="Add to cart" name="submit">
</form>
</div>
<div>
<!-- ICI LA LISTE DE LA COMMANDE -->
<h3>Résumé de la commande :</h3> <br><br>
<table border="1">
<c:forEach var="com" items="${beanSessionCommande.plats}">
<tr>
<td> ${com} </td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
忘了说:现在一切都在 session 中,我的选择列表填满了从另一个 JSP 文件获取的列表
非常感谢您的帮助:)
最佳答案
作为唯一<select>
您拥有的标签有name
等于 listePlats
,它将以此名称提交。
request.getParameter("listePlats")
将返回所选option
的值.
顺便说一句,你好像都标记了options
如selected
。我怀疑这是否有用:对于非多 <select>
来说,只有一个(或没有)应该被标记为这样。 .
关于java - 如何将 JSP 中的选择值获取到 servlet? JAVAEE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48966354/