如何在JSP中为servlet获取选择值? JAVAEE

时间:2018-02-24 18:42:14

标签: java jsp servlets java-ee

这里的第一张图片经过一个月的阅读后来帮助我,有点兴奋! :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文件中获取的列表

非常感谢您的帮助:)。

1 个答案:

答案 0 :(得分:1)

作为<select>唯一标记name等于listePlats,它将以此名称提交。

request.getParameter("listePlats")将返回所选option的值。

顺便说一下,您似乎将所有options标记为selected。我怀疑这是有用的:对于非多<select>,只应标记一个(或没有)。