0

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.

Seephor
  • 1,692
  • 3
  • 28
  • 50

2 Answers2

0

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.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • I had listKey="top" before and it still didn't work. The drop down is populated by a list in the action just fine and it is outside of my form, so I don't quite understand why it would cause an issue since it is not even being submitted. I'll try adding a case for INPUT that redirects to the page. Edit: I checked my struts.xml and I have an input result that is set to the same page - yet I still get a blank page. – Seephor Apr 26 '17 at 17:42
  • I changed the tag from to and it works. So why would not work even with listKey defined? I have an appropriate getter and setter method in the action for it. When it calls the getSelectedDataSource method in my action, it returns null. Why does it still try to do this when the element is outside of the form? – Seephor Apr 26 '17 at 18:16
0

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.

Seephor
  • 1,692
  • 3
  • 28
  • 50
  • 1
    Because you have Action1 that renders JSP1 that submits to Action2 that renders JSP1 again in case of "input". Action1 and Action2 must be two methods of the same action, or share a common parent action, otherwise it's a silly design. BTW, related: https://struts.apache.org/docs/how-do-we-repopulate-controls-when-validation-fails.html – Andrea Ligios Apr 26 '17 at 22:12