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

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

问题描述

struts.xml中的动作配置像这样没有问题:

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

当我访问有关动作配置的动作类(struts.xml)时出现问题.我在显示表单之前访问该类,因为我希望表单中的下拉选项在操作类中实例化它时显示适当的值.但事实证明,它将返回输入",我必须对其进行处理.

操作方法中的方法:

 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方法未执行,尽管尚未提交,但我的表单已被直接/自动验证.

我该如何解决?

解决方案

如果您的操作可以返回input结果,则必须在struts.xml配置中对其进行处理.

当发生某些验证错误时,或者当您尝试为操作属性设置错误的 type 时(例如,当您尝试设置intDate字段.

当Interceptor返回结果而不是返回下一个Interceptor(或Action(如果是最后一个)时)时,将不会执行被调用的Action方法,因为不会被达到.

仔细检查代码和请求,以查看失败的地方并返回input结果.

P.S:

如果使用

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

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

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

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>

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.

method in action class:

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>

form in 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" />

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

How can I solve this?

解决方案

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

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.

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.

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

P.S:

If with

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.

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天全站免登陆