避免返回“输入";自动在 Struts 中 [英] Avoid Return "input" Automatically in Struts

查看:19
本文介绍了避免返回“输入";自动在 Struts 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struts.xml中的action配置如下是没有问题的:

There is no problem when the action configuration in struts.xml is like this:

<action name="customer-form">
    <result name="success" type="tiles">/customer.tiles</result>
</action>

当我访问动作配置(struts.xml)上的动作类时,问题就来了.我在显示表单之前访问该类,因为我希望表单中的下拉选项在我在操作类中实例化它时显示适当的值.但事实证明,它会返回输入",我必须处理它.

The problem comes when I access the action class on action configuration (struts.xml). I access the class before displaying the form because I want the dropdown option in the form to display the appropriate value as I instantiate it in action class. But it turns out, it will return "input" and I must handle it.

动作类中的方法:

public String addCusto(){
    custoType = new ArrayList<String>();
    custoType.add("ABC");
    custoType.add("EFG");
    System.out.println(custoType.size());
    return SUCCESS;
}

struts.xml:

<action name="customer-form" class="com.satunol.struts.template.action.CustomerAction" method="addCusto">
    <result name="success" type="tiles">/customer.tiles</result>
    <result name="input" type="tiles">/customer.tiles</result>
</action>

jsp中的表单

<s:form action="savecusto" method="post" validate="true">
<s:select
   label="Customer Type"
   list="custoType"
   emptyOption="true"
   headerKey="-1"
   headerValue="None"/>
<s:textfield name="custo.name" key="custo.name" size="20" />
<s:textfield name="custo.email" key="email" size="20" />
<s:textfield name="custo.address" key="address" size="20" />
<s:textfield name="custo.phone" key="phone" size="20" />
<s:submit method="addCustomer" key="label.add.customer" align="center" />

结果?addCusto 方法未执行,我的表单虽然尚未提交,但已直接/自动验证.

The result? The addCusto method is not executed, and my form is directly/automatically validated although not yet submitted.

我该如何解决这个问题?

How can I solve this?

推荐答案

如果你的 Action 可以返回 input 结果,那么你必须在 struts.xml 配置中处理它.

If your Action can return an input result, then you must handle it in the struts.xml configuration.

input 结果由验证拦截器在发生某些验证错误时返回,或者当您尝试设置 Action 属性的错误 type 时,例如当您尝试将 int 设置为 Date 字段.

input result is returned by the Validation Interceptor when some validation error occurs, or when you are trying to set the wrong type of an Action's properties, for example when you try to set a int to a Date field.

当一个拦截器返回一个结果而不是继续到下一个拦截器(或者如果它是最后一个拦截器),则调用的 Action 方法将不会被执行,因为它不会到达.

When an Interceptor is returning a result instead of proceeding to the next Interceptor (or the Action if it is the last), the Action method called won't be executed, because it won't be reached.

仔细检查您的代码和您的请求,看看它在哪里失败并返回 input 结果.

Check out carefully your code and your request to see where it is failing and returning the input result.

PS:

如果有

我在显示表单之前访问该类,因为我希望表单中的下拉选项在我在操作类中实例化它时显示适当的值.

I access the class before displaying the form because I want the dropdown option in the form to display the appropriate value as I instantiate it in action class.

您的意思是您需要在执行任何方法之前(或返回 input 结果时)预填充字段,您应该为此使用 prepare() 方法,由在验证拦截器之前运行的准备拦截器运行.这样,即使验证失败,您的 prepare() 代码也会被执行.

you mean that you need to prepopulate fields before the execution of any method (or when input result is returned), you should use prepare() method for that, run by the Prepare Interceptor that runs before the Validation Interceptor. That way, your prepare() code will be executed even when validation will fail.

有关更多信息,请阅读 验证失败时我们如何重新填充控件

For more info, read How do we repopulate controls when validation fails

这篇关于避免返回“输入";自动在 Struts 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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