【发布时间】: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 结果。如果方法共享几乎所有内容,则单个操作类中的更多操作方法是有意义的,但如果即使是共同的结果也必须不同,那么最好使用不同的操作和一个方法,恕我直言