struts2 - 避免在 Struts 中自动返回 "input"

标签 struts2 struts-config struts-validation

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

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

我该如何解决这个问题?

最佳答案

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

当发生某些验证错误时,或者当您尝试设置操作属性的错误类型时,例如当您尝试将 int 设置为 Date 字段。

当拦截器返回结果而不是继续执行下一个拦截器(如果是最后一个拦截器,则继续执行操作)时,所调用的操作方法将不会被执行,因为无法到达该方法。

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

附注:

如果有

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() 代码也会执行。

有关更多信息,请阅读 How do we repopulate controls when validation fails

关于struts2 - 避免在 Struts 中自动返回 "input",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17183372/

相关文章:

jsp - 如何在 Struts2 中为标签提供超链接?

java - 如何关闭项目中iBatis和Struts2的log4j调试信息?

Struts2 .action 扩展导致 CSS、JavaScript 和 Struts Dojo 损坏

java - Struts 2 下载 - 如何动态配置文件名?

java - Struts 2 验证字段是否为空

java - 使用 Hibernate、Spring、Struts 2 在 View 模式中打开 session

java - struts2如何在JSP页面中包含css文件?

java - struts中的文件上传

jsp - Struts 无法为禁用字段提供 "refresh"值

java - 为什么 Struts 2 注解没有 LongRangeFieldValidator