【问题标题】:Avoid Return "input" Automatically in Struts避免在 Struts 中自动返回“输入”
【发布时间】:2013-06-19 05:17:41
【问题描述】:

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

我该如何解决这个问题?

【问题讨论】:

  • success 的两个结果是不是错字了?
  • @MahadiSiregar 你能发布你的Jsp代码并解释更多你想要实现的目标吗?
  • 我已经放了。我希望下拉(选择)根据我在操作类中设置的值显示列表选项。

标签: struts2 struts-config struts-validation


【解决方案1】:

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

input 当发生一些验证错误时,或者当您尝试设置错误的操作属性type 时,验证拦截器会返回结果,例如,当您尝试设置 @ 987654324@ 到 Date 字段。

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

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

PS:

如果有

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

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

欲了解更多信息,请阅读How do we repopulate controls when validation fails

【讨论】:

  • 当我将 添加到 struts.xml 中的操作配置时解决。这是否是正确的方法?
  • 是的,但默认情况下它在默认拦截器堆栈中...如果您的堆栈没有运行它,那是因为您创建了一个没有它的自定义拦截器堆栈。只需使用默认值,或者记得在您创建的自定义堆栈中包含 Prepare Interceptor。
  • 是的,我创建了默认的自定义拦截器,但没有将其包含在默认值中。谢谢,它解决了我的问题。
猜你喜欢
  • 2017-03-12
  • 1970-01-01
  • 2019-07-23
  • 2011-06-24
  • 2014-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多