why does struts reset my form after failed validation?

五迷三道 提交于 2019-12-20 05:49:04

问题


I am using the validation framework with Struts 1.1.When validation fails, the entire form is reset.

After much time scouring the net, I have gathered:

  1. When a new request is received, the form object is created if it does not exist in the current scope (request or session).
  2. Reset is called()
  3. Form values are populated from the bean properties.
  4. Validation starts if enabled
  5. If validation fails, ActionErrors are returned and the request is directed to the URI given by the input attribute of the action tag in my struts-config.xml.

That's where I have the problem. If validation fails, and I set the input param to the same page, reset() gets called again but it does not use the bean values from when the form is initially loaded. So the user has to re-enter everything.

My action mapping class for this action looks like this:

<action
  path="/edit/componentRelease"
  type="org.twdata.struts.SpringAction"
  name="edit/componentRelease"
  scope="request"
  input="/WEB-INF/jsp/edit/editComponentRelease.jsp"
  parameter="edit/componentRelease"
  validate="true"
>
  <forward
    name="edit/componentRelease"
    path="/WEB-INF/jsp/edit/editComponentRelease.jsp"
    redirect="false"
  />
</action>

The form used to display the bean starts with:

<html:form method="post" name="componentReleaseEditor" type="com.mx.releasemgr.forms.ComponentReleaseEditorForm" action="/edit/componentRelease">

回答1:


the reset() is used to clear the values previously entered...if u debug it and see then u'll come to know. eg ur entering 1 in the form and say submit and again come on the same form and enter 2 and submit again now what reset will do is it will clear 1 and now 2, and thus u get 2 in ur validate() part.




回答2:


@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {

    //If Clear button click will set date as today and clear all other value
    //If Insert, update with validation as failure than not clear all value on the form, but only clear that wrong when validation (means skipp all value as true on the form and only clear value wrong)
    String actionName = request.getParameter("method");
    if(actionName!=null){
        if (actionName.equals(Enums.ActionName.CLEAR.getActionName())) {
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            this.setPublicationDay(dateFormat.format(date));
        }
        else{
            request.setAttribute("book", this);
        }
    }

    super.reset(mapping, request);
}



回答3:


The solution is to move the display of the form to a different action than that used to forward. In my example, they are both the same.



来源:https://stackoverflow.com/questions/9775511/why-does-struts-reset-my-form-after-failed-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!