-->

Struts 1 Losing Request Parameters after Failed Fo

2019-05-28 16:01发布

问题:

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">

回答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.