避免在支柱中自动返回"input"



当struts.xml中的动作配置是这样的时候是没有问题的:

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

当我访问action配置(struts.xml)上的action类时,问题出现了。我在显示表单之前访问类,因为我希望表单中的下拉选项在我在action类中实例化它时显示适当的值。但结果是,它将返回"input",我必须处理它。

方法:

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方法,我的表单直接/自动验证,尽管还没有提交。

我该如何解决这个问题?

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

input结果由验证拦截器返回,当一些验证错误发生,或者当你试图设置错误的类型的一个动作的属性,例如当你试图设置一个intDate字段。

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

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

p。S:

If with

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

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

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

相关内容

最新更新