如何将列表从 JSP 传递到 ACtion 类

发布于 2024-11-15 17:03:17 字数 2878 浏览 1 评论 0原文

再会!

我想将我的代码转换为 STRUTS..并且我尝试不在我的 Action 类中使用 getParameter.. 但如果不使用 getParameter,我就无法将信息从 JSP 传递到 Action 类。

JSP:

 <html:form action="EditExam">
                <input type = "hidden" name = applicantNumber value="${applicantForm.applicantNumber}"  >

                <table>
                    <c:forEach var="exam" items="${examList}">
                        <input type = "hidden" name ="examId" value="${exam.examId}"  >
                       <tr>
                           <td>Exam Type: &nbsp</td>       <td><input type="text" value="${exam.examName}" name="examType" readonly ="true"></td>
                       </tr>
                       <tr>
                           <td>Date: </td>                 <td><input type="text" value="${exam.examDate}" name="examDate" class="date"></td>
                       </tr>
                       <tr>
                           <td>Result: </td>               
                           <td>
                                <select name = examResult> 
                                    <option value="Pass" ${exam.examResult == 'Pass' ? 'selected' : ''}>Pass</option>
                                    <option value="Fail" ${exam.examResult == 'Fail' ? 'selected' : ''}>Fail</option>
                                    <option value="" ${exam.examResult == '' ? 'selected' : ''}></option>
                                </select>   
                           </td>   
                       </tr>
                        <tr><td>&nbsp</td><td> &nbsp</td></tr>
                    </c:forEach>
                </table>

                <input type="submit" class="saveButton" value="SAVE">

            </html:form>

操作类:

public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // TODO Auto-generated method stub

        String forward = "success";

        ApplicantForm applicantForm = (ApplicantForm)form;
        int applicantNumber = applicantForm.getApplicantNumber();   

        String examDate[] = request.getParameterValues("examDate");
        String examResult[] = request.getParameterValues("examResult");
        String examId[] = request.getParameterValues("examId");
            //MORE CODES AFTER...

我的问题是: 如何在不使用 getparameter 的情况下将数据从 JSP 传递到 Action Class。

要考虑:

  1. 我的考试是一个列表...
  2. 编辑按钮位于循环之外...因此应该捕获循环内的所有更改..(我需要传递 ArrayList 吗?我如何在操作表单上捕获它?)

您的回复将不胜感激。谢谢。

Good day!

I want to convert my code into STRUTS.. and i tried not using the getParameter in my Action Class..
But I can't pass the information from JSP to Action Class without using the getParameter.

JSP:

 <html:form action="EditExam">
                <input type = "hidden" name = applicantNumber value="${applicantForm.applicantNumber}"  >

                <table>
                    <c:forEach var="exam" items="${examList}">
                        <input type = "hidden" name ="examId" value="${exam.examId}"  >
                       <tr>
                           <td>Exam Type:  </td>       <td><input type="text" value="${exam.examName}" name="examType" readonly ="true"></td>
                       </tr>
                       <tr>
                           <td>Date: </td>                 <td><input type="text" value="${exam.examDate}" name="examDate" class="date"></td>
                       </tr>
                       <tr>
                           <td>Result: </td>               
                           <td>
                                <select name = examResult> 
                                    <option value="Pass" ${exam.examResult == 'Pass' ? 'selected' : ''}>Pass</option>
                                    <option value="Fail" ${exam.examResult == 'Fail' ? 'selected' : ''}>Fail</option>
                                    <option value="" ${exam.examResult == '' ? 'selected' : ''}></option>
                                </select>   
                           </td>   
                       </tr>
                        <tr><td> </td><td>  </td></tr>
                    </c:forEach>
                </table>

                <input type="submit" class="saveButton" value="SAVE">

            </html:form>

ACTION CLASS:

public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // TODO Auto-generated method stub

        String forward = "success";

        ApplicantForm applicantForm = (ApplicantForm)form;
        int applicantNumber = applicantForm.getApplicantNumber();   

        String examDate[] = request.getParameterValues("examDate");
        String examResult[] = request.getParameterValues("examResult");
        String examId[] = request.getParameterValues("examId");
            //MORE CODES AFTER...

My problem is:
How can I pass the data from JSP to Action Class without using the getparameter.

TO CONSIDER:

  1. My Exam is a list...
  2. The EDIT Button is outside the loop... so all changes inside the loop should be captured.. (Do i need to pass the ArrayList? How can i catch it on the Action FOrm?)

You're reply would be highly appreciated. THANK YOU.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屋檐 2024-11-22 17:03:18

HTML/JSP 不理解列表等 Java 对象。它们只处理纯字符串/数字或字节流。

所以你必须使用

request.getParameter("paramName");

或者如果你需要地图,你可以使用

Map < String, String[] > queryParamsMap = (Map < String, String[] > )request.getParameterMap();

从地图中,你可以直接获取你的特定参数的数组。通过使用例如

String[] paramArray = queryParamsMap.get("myParam");

HTML/JSP doesn't understand java objects such as lists. They only deal with pure strings/numbers or bytestreams.

So you have to use

request.getParameter("paramName");

or if you need a map, you can use

Map < String, String[] > queryParamsMap = (Map < String, String[] > )request.getParameterMap();

From the map, you can directly get an array of your specific parameter. By using for example

String[] paramArray = queryParamsMap.get("myParam");
请你别敷衍 2024-11-22 17:03:18

你不能。可以使用HTTP协议将数据从浏览器(html,jsp的结果)传输到服务器,该协议仅传输文本请求参数。

所以你必须使用 request.getParameter[Values](..) 。如果您需要List,可以使用Arrays.asList(array)

我认为struts应该有某种形式的绑定,所以无论你在哪里指定输入参数,你都可以尝试指定一个List,也许struts会填充它。 (但它仍然会在后台使用 request.getParameterValues(..)

You can't. You can transfer data from the browser (html, the result of jsp) to the server with the HTTP protocol, which transfers only textual request parameters.

So you have to use request.getParameter[Values](..). If you need a List, you can use Arrays.asList(array).

I think struts should have some form of binding, so wherever you specify your input parameters, you can try specifying a List, and perhaps struts will populate it. (but it will still use request.getParameterValues(..) under the hood)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文