Struts 1 Losing Request Parameters after Failed Form Validation with Multipart/Form-Data Enctype
Asked Answered
M

1

2

I have a simple Struts form. It has a few text fields and a file field. The enctype is multipart/form-data on my form. I validate in the actionform's validate method. If the text fields are empty, I return errors that they are required. Along with the visible fields, I pass a few hidden fields that are needed as request params when the form is processed and returned to the JSP. The JSP needs these request params.

Everything works great when there are no validation errors as the request params get returned by using the ActionRedirect class in the action. But if there are errors returned, I lose the request params. (I am able to access them in the actionform validate method or in the action).

How can I make sure the request params are passed back upon validation error in multipart form? Is there any sort of workaround?

Action-mappings (slightly edited for obfuscation) below:

   <action
    path="/saveQuestion"
    type="blahblahblah.QuestionAction"
    parameter="save"
    name="QuestionForm"
    input="populateQuestion.do"
    scope="request"
    validate="true">
    <set-property property="cancellable" value="true"/>
    <forward name="success" path="viewSurvey.do" redirect="true"/>
  </action>
  <action
    path="populateQuestion"
    type="blahblahblah.QuestionAction"
    parameter="populateRequest"
    name="ItemForm"
    scope="request">
      <forward name="success" path=".editing.Question"/>
  </action>

And my JSP form line:

<html:form styleId="QuestionForm" action="/saveQuestion" enctype="multipart/form-data" method="POST">
Mares answered 9/1, 2013 at 16:7 Comment(8)
can you show us the important parts of your code? (for me that would be the related action-mappings in struts-config file)Erysipelas
Are those hidden fields also properties of the ActionForm?Wycliffite
Th0rndike I can't paste the code here, but I have the proper input, name, and forwarding. My struts-config.xml is fine... everything works except when I have the multipart/form-data and the validation fails. Horstmann - yes.Mares
Are you implementing the reset() method in your form?Droop
I don't believe so Claudio.Mares
@Mares you don't pass params upon submit, thus your params are missed.Woad
Roman - what exactly do you mean? All my form parameters are definitely passed to the action. I'm only having this issue when form validation fails.Mares
I have encountered this problem before. I can't help you without knowing some things about your code. Starting by the action mappings.Erysipelas
E
1

I believe you have two options to solve this problem:

  • Change the scope to session: This way the data will be stored in session and you won't lose any data.
  • Implement the reset method of your validation: This way, when the reset method is called in your validation, you can repopulate the data of the form.

I hope this helps somehow. I might have some other suggestions in my old code files, but i don't have access to them right now. If I have time I'll check them out later.

Erysipelas answered 10/1, 2013 at 9:23 Comment(6)
Are there any side-effects or other ramifications of changing the scope from request to session? And does this reset get called after the validation, but before my form values disappear? How am I actually supposed to repopulate the data of the form - it's either in the request or it's not right? If it's not in my request, I have no idea to discern in the code which IDs and other variables I wanted.Mares
First question: you'll find the form in session and it's expected that the form is in session. If you use the same form multiple times it could cause problems(other than the normal problems that session creates, gotta watch it out). Second question: the reset method gets called before populating the form(before calling the setter methods of your beans)Erysipelas
For the reset solution, I don't quite follow how this can help me. The reset is called in the very beginning before the validate - how am I supposed to pass along some variable I need from the form to the action that I am redirected to after the failed validation when the form's values all turn to null in between these two steps? Are you saying use a bean that is common to the Action afterwards and set its variables from the reset so it is persisted somewhere besides the form (which would require changing my action code)?Mares
That's exactly what i meant. However I realize that this is not a solution in your case, unless you use the same form for both actions.Erysipelas
Wouldn't this require some sort of singleton bean? I have no way of accessing any variables in my post-validation action from the reset.Mares
It turns out using the reset method is not suggested - at least implicitly - by the Java Doc: struts.apache.org/1.x/apidocs/org/apache/struts/action/…, javax.servlet.ServletRequest). It looks like there is no elegant, real solution to this problem except hacky workarounds in my user interface (e.g. splitting up the forms).Mares

© 2022 - 2024 — McMap. All rights reserved.