【问题标题】:How to configure dynamic "input" results in Struts2如何在 Struts2 中配置动态“输入”结果
【发布时间】:2015-03-30 15:45:23
【问题描述】:

假设我使用 Struts2 验证框架将“每个方法”验证配置到我的操作中(在我的示例中带有注释,但对于 xml 也是相同的)。 假设我的操作中有三个公开的公开方法:

public class MyAction extends ActionSupport {

    @Override
    public String execute() throws Exception {

        //some code here...
    }

    @Validations(
        customValidators = {@CustomValidator(type="myCostomValidator", fieldName="myFieldName", shortCircuit=true, message="")}         
    )
    public String info() {

        //some code here...
    }

    @Validations(
        customValidators = {@CustomValidator(type="myCostomValidator", fieldName="anotherFieldName", shortCircuit=true, message="")},
        visitorFields = {@VisitorFieldValidator(context="update_context", fieldName="anObjectField", message="", shortCircuit=true)}            
    )
    public String update() {

        //some code here...
    }

    //getters, setters and other...
}

现在,这三个方法中的每一个都可以被调用并具有不同的验证。如果验证失败,框架将结果“输入”设置为必须配置到 struts.xml 中:

<action name="myAction_*" method="{1}" class="com.test.gui.action.test.MyAction">
    <result name="success">result1.jsp</result>
    <result name="edit">result2.jsp</result>
    <result name="input">result3.jsp</result>
</action>

如何为每种操作方法提供不同的“输入”结果?例如,如果 info() 方法的验证失败,我想到达一个页面,如果 update() 方法的验证失败,我想到达另一个页面。 谢谢。

【问题讨论】:

  • 结果可以采用 OGNL 表达式,但是...原样这会将 Java 端直接绑定到表示层。我的第一个想法是查看是否可以通过 OGNL 配置默认工作流拦截器以返回除 "input" 之外的其他结果名称,但我不确定是否会正确评估 - 但我仍然会尝试看看会发生什么。
  • 我会把它分成三个动作,最终与普通的东西(准备()或其他东西)共享一个基本动作。然后每个人都可以返回自己的 INPUT 结果。如果方法共享几乎所有内容,则单个操作类中的更多操作方法是有意义的,但如果即使是共同的结果也必须不同,那么最好使用不同的操作和一个方法,恕我直言

标签: struts2 struts-validation


【解决方案1】:

我找到了这个解决方案:使用@InputConfig注解。

使用此注解,您可以为验证错误设置“每个方法”结果名称。因此,在 struts.xml 中,您可以为每个结果名称配置您想要到达的页面。

例子:

@InputConfig(resultName="input_update")
public String update() {
    // perform action
    return SUCCESS;
}

在 struts.xml 中:&lt;result name="input_update"&gt;jsp/input_update.jsp&lt;/result&gt;

或:

@InputConfig(methodName="inputMethod")
public String update() {
    // perform action
    return SUCCESS;
}
public String inputMethod() {
    // perform some data filling
    return INPUT;
}

struts.xml:&lt;result name="input"&gt;jsp/myInput.jsp&lt;/result&gt;

请参阅struts.apache.org/docs/inputconfig-annotation.html 上的文档

也可以通过设置'workflow'拦截器的参数在struts.xml中设置输入结果名称,但是会影响action中的所有方法:

<interceptor-ref name="workflow">
    <param name="inputResultName">custom_input_result</param>
</interceptor-ref>

在此处查看文档struts.apache.org/docs/default-workflow-interceptor.html

【讨论】:

    【解决方案2】:

    您不必修改返回的结果名称。您正在为您的操作使用通配符映射,因此也将它用于 JSP 文件名。

    <action name="myAction_*" method="{1}" class="com.test.gui.action.test.MyAction">
        <result name="success">success.jsp</result>
        <result name="input">result_{1}.jsp</result>
    </action>
    

    【讨论】:

    • 有效!但这有点限制,因为在这种情况下,无法确保操作上的两个不同方法具有相同的“输入”结果页面。例如,假设我在同一个操作上有三个方法:search()、create()、update(),并且在验证错误的情况下,我希望 search() 以 test_search.jsp 结束,并且 create() 和update() 每个都在同一页面结束,例如 test_edit.jsp。如何配置 struts.xml?
    • @Perelum:最简单的方法是创建两个页面并包含您的编辑表单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多