Search code examples
formsstruts-1

Sending HTML form parameters to Struts action class


I am implementing a search functionality in a website I am building, which involves searching by the md5 hash of the name of the file submitted and searching by the notes associated with each submitted file. So, I should detect as to which button is pressed "Search by MD5" or "Search by Notes". This is the code I have:

JSP code for the form:

<form id="search" name="search" action = "search.do"
        method="POST" enctype="multipart/form-data">
    <table align = "center">
        <tr>
            <th colspan="4" bgcolor="#004276"><font color="white">
                    Search for Sample 
            </th>
        </tr>
         <tr>
            <td><input name="md5" type="text" value="${form.md5}"/></td>
            <td><input name="md5search" type="submit" value="Search by MD5"/>
        </tr>
        <tr>
            <td><input name="notes" type="text" value="${form.notes}"/></td>
            <td><input name="notessearch" type="submit" value="Search by Notes"/>
        </tr>
    </table>
</form>

search.do is mapped to SearchResultsAction.java. Code in Java action class (SearchResultsAction) which handles the request is:

        if(request.getParameter("md5search").toString().equals("Search by MD5")){
            searchSubmissionsList = submissionsDAO.searchSubmissionsByMD5(form.getMD5());
        }

        if(request.getParameter("notessearch").toString().equals("Search by Notes")){
            searchSubmissionsList = submissionsDAO.searchSubmissionByNotes(form.getNotes());
        }

But the problem I am facing here is that, request.getParameter("md5search") and request.getParameter("notessearch") return null for some reason. I have been working on this for a while and have not been able to figure it out. The weird thing is that it once worked for me sometime back when I was working on another project. Am I missing something here?


Solution

  • It's null because you used multipart/form-data form encoding instead of (default) application/x-www-form-urlencoded. Basically, you have to (let Struts) extract the text fields from the multipart form data body the same way as you (or Struts) extracted the uploaded file. Or, as there is actually no <input type="file"> field in your form at all, just remove the enctype attribute altogether.

    See also