为什么struts在验证失败后重置我的表单? [英] why does struts reset my form after failed validation?

查看:30
本文介绍了为什么struts在验证失败后重置我的表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Struts 1.1 中使用验证框架.验证失败时,整个表单将重置.

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. 当收到新请求时,如果表单对象在当前范围(请求或会话)中不存在,则会创建该表单对象.
  2. 调用重置()
  3. 表单值由 bean 属性填充.
  4. 如果启用则验证开始
  5. 如果验证失败,则返回 ActionErrors 并将请求定向到由我的 struts-config.xml 中的 action 标记的 input 属性给出的 URI.

这就是我的问题所在.如果验证失败,并且我将输入参数设置为同一页面,则会再次调用 reset(),但不会使用表单最初加载时的 bean 值.所以用户必须重新输入所有内容.

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>

用于显示 bean 的表单以:

The form used to display the bean starts with:

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

推荐答案

@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);
}

这篇关于为什么struts在验证失败后重置我的表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆