Struts 2 XML form validation breaks when new element that does not need validation is added

372 views Asked by At

I am facing an issue that is really hard to debug. I have a JSP page that has some form elements on it that submit to a Struts2 action. I also have a XML form validation file to perform some validation on the submitted fields. The file has the naming convention 'actionName-validation.xml'

This works fine, but when I add a drop down box, outside of the form, the validation now fails. Instead it redirects to a blank page and my breakpoint in my action class is not hit.

Is there a way to turn on some kind of debugging or logging for the validation? Why would adding a tag outside of a form cause this to happen?

Here is the code on the JSP page:

<s:select id="dataSource" name="selectedDataSource" theme="simple" listValue="top" 
   headerKey="" headerValue="Choose Data" list="dataSources" size="1" />

<div id="forms">
    <s:form method="post" action="MyAction" theme="simple">
      <p>
          <label class="input" for="name"
          <span style="color:red;">*</span>
          <span>Name</span><br>
          <s:textfield theme="simple" name="name" maxlength="11" size="11" />
          <br>
          <s:fielderror theme="plain"><s:param value="'name'" /</s:fielderror></label>

      </p>    
      <s:submit value="Create New" theme="simple" cssStyle="display: block; clear: left;"/>

    </s:form>
</div>

If I remove the <s:select> tag, it works.

Any help would be greatly appreciated it.

EDIT2: I found the problem. I needed a get method for the list that is used to populate the select drop down inside the action that the form submits to.

I had one for the action that initially is called for the page, but when the validation fails and it re-loads that page from the form action class, it tries to re-populate the select drop down and needs a getter there. I feel silly for not finding that sooner. Would be nice if there were some type of logging or messaging of these types of issues.

thanks.

2

There are 2 answers

1
Seephor On BEST ANSWER

I found the problem. I needed a get method for the list that is used to populate the select drop down inside the action that the form submits to.

I had one for the action that initially is called for the page, but when the validation fails and it re-loads that page from the form action class, it tries to re-populate the select drop down and needs a getter there. I feel silly for not finding that sooner. Would be nice if there were some type of logging or messaging of these types of issues.

2
Andrea Ligios On

The error you are encountering is not a validation error (handled by the Validation Interceptor), but an error occurred when setting the parameters (raised by the Parameters Interceptor) and for which the Conversion Error Interceptor added a fieldError, which you are not seeing because

  1. your INPUT result lands on a blank page and
  2. you are using theme="simple" on the textfield, which forces you to add <s:fielderror fieldName="dataSource" /> to show it (or <s:fielderror /> to show them all).

Read how the INPUT result works, set your page as the INPUT page, print the errors, then you will discover the problem, that is most likely related to the fact that you've not specified a listKey attribute in your select, that is sending the description instead of the code of the datasource to selectedDataSource, which is probably an Integer.