You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Matthew J. Vincent" <vi...@cs.usm.maine.edu> on 2004/01/21 15:44:33 UTC

Struts, Search Page, Help!!!

I need some serious help with Struts.

I have a page that gets invoked by calling search.do, this in turns calls my
SearchAction which gets data from a database to populate a select box in my
JSP search.jsp.

<select name="strainType">
    <option selected value="">ANY
    <c:forEach var="strainType" items="${strainTypes}" varStatus="status">
           <option><c:out value="${strainType}" escapeXml="false"/></option>
    </c:forEach>
</select>

The problem I have is that when the user clicks submit, the correct url gets
invoked (searchResults.do), but when there is an error (either when the form
is not completely filled out or there are no "results" for the search), I
want to put the user back on the search.do page with the values they filled
out already populated and show an error message.  I'm not sure if I have my
struts-config.xml file correct or how to do this in general.  How do I make
sure that the option from the select box is what they previously chose?
Should I not use JSTL for the select box?  Also, what should my execute
method in my StrainSearchResultsAction look like (i.e, do I need to put the
values they submitted in scope again)?  Is this making sense!  Help!




CODE BELOW
=========================================================================

Here is the relative section from my struts-config.xml file:


   <form-beans>
      <form-bean name="strainSearchForm"
                 type="StrainSearchForm"/>
   </form-beans>

   <action-mappings>
      <action path="/search"
              type="SearchAction"
              scope="session">
         <forward name="success" path="/search.jsp"/>
         <forward name="failure" path="/appError.jsp"/>
      </action>

      <action path = "/searchResults"
              type = "SearchResultsAction"
              name = "strainSearchForm"
              validate = "true"
              input = "/search.do">
         <forward name="success" path="/searchResults.jsp"/>
         <forward name="failure" path="/search.do"/>
         <forward name="error" path="/appError.jsp"/>
      </action>
   </action-mappings>


Here is the validate method of my StrainSearchForm:

    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();

        if(((strainName == null) || (strainName.length() == 0)) ||
           ((strainId == null) || (strainId.length() == 0))) {
            errors.add("strainName",
                new ActionError("errors.searchForms.strain.requiredField"));
        }

        return errors;
    }


Thanks!

Matt


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org