开发者

How to pass List from JSP to ACtion Class

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: &nbsp</td>       <td><input type="text" value="${exam.examName}" name="examType" readonly ="true"></td>
                       </tr>
                       <tr>
                           <td>Date: </td>                 <td><in开发者_StackOverflow社区put 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>

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.


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");


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜