java - 如何将 JSP 页面内的表单提交给 servlet?

标签 java forms jsp jakarta-ee servlets

我对 JSP 页面开发还很陌生,我对如何解决此任务有以下疑问。

我有一个 JSP 页面,其中包含 JQuery Accordion 菜单,类似的内容:

enter image description here

这是呈现此 Accordion 菜单的代码:

    <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>

正如您在上一张图片中看到的,在 Accordion 标题内放置的表格的最后一列中,有 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>

现在我要做的就是向处理该 View 的 servlet 提交一个值(此时甚至是一个固定值,对于 esample:第一个按钮为 1,第二个按钮为 2,第三个按钮为 3)单击按钮时。

我认为我必须使用邮寄提交的表单。但我究竟应该做什么呢?网上搜索发现这篇文章:http://www.tutorialspoint.com/jsp/jsp_form_processing.htm

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

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

Tnx

最佳答案

您可以使用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>

关于java - 如何将 JSP 页面内的表单提交给 servlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27226713/

相关文章:

与 Paypal 的 Spring 请求映射

java - PrintWriter 和 byte[] 问题

java - 我可以将对象的实例存储在 javaFX 节点中吗?

javascript - 使用jquery问题验证和提交表单

javascript - 尝试对 onSubmit 中的参数使用序列化时失败 ='return function(this)'

java - 转发或重定向后表单参数为空

Java lineTo() 非常慢

java - 返回索引 vector

javascript - HTML 表单到 XMLHttprequest

java - 从 forEach 循环内的代码访问 JSTL 标签