mr. yoso
mr. yoso

Reputation: 31

request.getParameterValues in struts

I want to convert my previous J2EE recruitment application from servlet to struts and I wanted to know if it is okay to put a request.getParameterValues("name") inside an Action class. Here is a sample code:

public class ConfirmEditApplicantAction extends Action{

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

        String forward = "success";

            **String screenNames[] = request.getParameterValues("screenName");**

            //some codes here....


        return mapping.findForward(forward);
    }
}

that came from this sample form in jsp:

<div id="screenInformation" class="tab_content">
        <h4>Screenings:</h4>

        <form action="EditScreeningServlet" method="post">
            <input type = "hidden" name ="applicantNumber" value="${infoObj.applicantNumber}"  >
            <table>
                <c:forEach var="screen" items="${screenList}">
                    <input type = "hidden" name ="screenId" value="${screen.screenId}"  >
                   <tr>
                       <td>Screen Type: &nbsp</td>       <td><input type="text" value="${screen.screenName}" name="screenName" readonly ="true"></td>
                   </tr>
                   <tr>
                       <td>Date: </td>                   <td><input type="text" value="${screen.screenDate}" name="screenDate" class="date"></td>
                   </tr>
                   <tr> 
                       <td>Result: </td>               
                       <td>
                            <select name = screenResult> 
                                <option value="Pass" ${screen.screenResult == 'Pass' ? 'selected' : ''}>Pass</option>
                                <option value="Fail" ${screen.screenResult == 'Fail' ? 'selected' : ''}>Fail</option>
                                <option value="" ${screen.screenResult == '' ? 'selected' : ''}></option>
                            </select>   
                       </td>
                   </tr>
                    <tr><td>&nbsp</td><td> &nbsp</td></tr>


                </c:forEach>
            </table>

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

Upvotes: 2

Views: 4752

Answers (3)

Jasonw
Jasonw

Reputation: 5064

I think you can do that but to adhere to the MVC design, probably it would be best to spend some time to learn about the ActionForm. That way, you can do validation to your form in a java class that extends ActionForm. In class ConfirmEditApplicantAction, you can code the business logic here which is more systematic.

Upvotes: 2

deltaforce2
deltaforce2

Reputation: 593

I don't see why it shouldn't be OK. Have you actually run that code to see if it works? If it does, then I don't see any problem with it and it should be OK. And if it doesn't, well, then it most likely is not OK since you have no working code.

Upvotes: 1

newbie
newbie

Reputation: 14960

The struts way to deal with request parameters is to put a property of the same name as the parameter in the ActionForm, and let Struts populate it for you. This way, you don't have to deal with request parameters at all, but rather, are dealing with ActionForm properties. So, to answer your question, instead of coding code:<%= request.getParameter("name") %>

I would code a the property "name" in my ActionForm with a getName() and a setName() method. In my jsp, I would then code: code:<bean:write name="MyForm" property="name"/>

OR.. <bean parameter id="username" name="user"/> and <%=username%> If the jsp is called with a name parameter, struts will automatically populate the form bean with the name property.

This is why there's no struts tag that I know of to display a request parameter. There is one in jstl, however:<c:out value="${param.name}" />

Upvotes: 0

Related Questions