如何将JSP页面中的此表单提交给servlet?

时间:2014-12-01 10:49:17

标签: java forms jsp java-ee servlets

我在 JSP 页面开发方面相当新,我对如何解决此任务抱有以下疑问。

我有一个 JSP页面,其中包含 JQuery手风琴菜单,类似于:

enter image description here

这是呈现此手风琴菜单的代码:

    <div id="accordion">

        <%
            for (SalDettaglio salDettaglio : (SalDettaglio[]) request.getSession(false).getAttribute("salDettaglio")) {
        %>
                <h2>
                    <!-- <span>SALDETTAGLIO: </span> -->  <%-- out.println(salDettaglio.getCodice()); --%>
                    <!--
                    <table class="salDettaglio" border="1" width="100%">

                        <tr class="salDettaglioRow">
                            <td width = "8.3%"><%=salDettaglio.getCodice()%></td>
                            <td width = "8.3%"><%=salDettaglio.getStato()%></td>
                            <td width = "8.3%"><%=salDettaglio.getDataCreazione()%></td>
                            <td width = "8.3%"><%=salDettaglio.getDataRegistrazione()%></td>
                            <td width = "8.3%"><%=salDettaglio.getAutoreCreazione()%></td>
                            <td width = "8.3%"><%=salDettaglio.getAutoreConvalida()%></td>
                            <td width = "8.3%"><%=salDettaglio.getAutoreAcquisizione()%></td>
                            <td width = "8.3%"><%=salDettaglio.getTotImponibile().toString()%></td>
                            <td width = "8.3%"><%=salDettaglio.getFornitore()%></td>
                            <td width = "8.3%"><%=salDettaglio.getRmConRiserva()%></td>
                            <td width = "8.3%"><%=salDettaglio.getErrore()%></td>
                            <td width = "8.3%">
                                <button class="acceptButton">ACCEPT ICON BUTTON</button>
                                <button class="cancelButton">CANCEL ICON BUTTON</button>
                                <button class="sapButton">SAP ICON BUTTON</button>
                            </td>
                        </tr>
                    </table>
                    -->
                    <table class="standard-table-cls" border="1" width="100%">
                        <thead>
                            <tr class="salDettaglioRow">
                                <th width = "8.33%"><%=salDettaglio.getCodice()%></th>
                                <th width = "8.33%"><%=salDettaglio.getStato()%></th>
                                <th width = "8.33%"><%=salDettaglio.getDataCreazione()%></th>
                                <th width = "8.33%"><%=salDettaglio.getDataRegistrazione()%></th>
                                <th width = "8.33%"><%=salDettaglio.getAutoreCreazione()%></th>
                                <th width = "8.33%"><%=salDettaglio.getAutoreConvalida()%></th>
                                <th width = "8.33%"><%=salDettaglio.getAutoreAcquisizione()%></th>
                                <th width = "8.33%"><%=salDettaglio.getTotImponibile().toString()%></th>
                                <th width = "8.33%"><%=salDettaglio.getFornitore()%></th>
                                <th width = "8.33%"><%=salDettaglio.getRmConRiserva()%></th>
                                <th width = "8.33%"><%=salDettaglio.getErrore()%></th>
                                <th width = "8.33%">
                                    <button class="acceptButton">ACCEPT ICON BUTTON</button>
                                    <button class="cancelButton">CANCEL ICON BUTTON</button>
                                    <button class="sapButton">SAP ICON BUTTON</button>
                                </th>
                            </tr>
                        </thead>
                    </table>
                </h2>

                <div>
                    <table border="1" align="right" class="standard-table-cls">
                        <caption><div align="center"><b>RM</b></div></caption>

                        <thead>
                            <tr>
                                <th width="14.2%">Codice RM</th>
                                <th width="14.2%">Autore Firma</th>
                                <th width="14.2%">Data Firma</th>
                                <th width="14.2%">Acq Riserva</th>
                                <th width="14.2%">Consegna Finale</th>
                                <th width="14.2%">Descrizione RM</th>
                                <th width="14.2%">Imponibile</th>
                            </tr>
                        </thead>

                        <%
                        for (RM currentRM : salDettaglio.getRM()) {
                            String test = currentRM.getAcqRiserva();
                        %>

                                <tr id="rmRow">
                                    <td><%=currentRM.getCodiceRm()%></td>
                                    <td><%=currentRM.getAutoreFirma()%></td>
                                    <td><%=currentRM.getDataFirma()%></td>
                                    <td><%=currentRM.getAcqRiserva()%></td>
                                    <td><%=currentRM.getConsegnaFinale()%></td>
                                    <td><%=currentRM.getDescrizioneRM()%></td>
                                    <td><%=currentRM.getImponibile().toString()%></td>
                                </tr>

                    <%}%>
                    </table>
                </div>
        <%
            }
        %>

   </div>

正如您在手风琴标题中放置的表格的最后一栏中的上一张图片中所见,有3个图标使用 JQuery按钮实现,这些:

<th width = "8.33%">
    <button class="acceptButton">ACCEPT ICON BUTTON</button>
    <button class="cancelButton">CANCEL ICON BUTTON</button>
    <button class="sapButton">SAP ICON BUTTON</button>
</th>

现在我要做的是向处理此视图的servlet提交一个值(此时甚至是一个固定值,例如:第一个按钮为1,第二个按钮为2,第三个按钮为3)单击按钮时。

我认为我必须使用在帖子中提交它的表单。但究竟我该怎么做?在线搜索我创建了这篇文章:http://www.tutorialspoint.com/jsp/jsp_form_processing.htm

但是在之前的教程中展示了如何将表单从HTML页面提交到JSP ,但这不是我想要做的。

如何将表单提交到JSP到servlet?

TNX

1 个答案:

答案 0 :(得分:1)

您可以使用Jquery $.post ajax请求来实现此目的:

<table>
   <td><input type="text" value="test" id="name" />
   <td><button onclick="post(<%=entity.id%>)">submit</button><td>
</table>

<script>
  function post(id){
     var name=$("#name").val();

     $.post(
        "/myservlet", { customerid: id, customername: name }
     ).done(function(data){
        alert(data);
       //This will send get request to your servlet. can use this if your previous
       //post request done any update in DB
       window.location.href='/myservlet?customerid='+id;
     });
  }
</script>