【问题标题】:Short-circuit in Struts2 validationStruts2 验证中的短路
【发布时间】:2014-01-10 17:21:26
【问题描述】:

假设动作类中有一个BigDecimal 类型的字段,如下所示。

@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value = "struts-default")
public final class TestAction extends ActionSupport
{
    private BigDecimal price;

    //Setter and getter.        

    @Validations(
    requiredFields = {
        @RequiredFieldValidator(fieldName = "price", type = ValidatorType.FIELD, message = "Price is mandatory.")},
    fieldExpressions = {
        @FieldExpressionValidator(fieldName = "price", expression = "price>0", shortCircuit = true, message = "Price cannot be less than or equal to zero.")})
    @Action(value = "Add",
    results = {
        @Result(name = ActionSupport.SUCCESS, type = "redirectAction", params = {"namespace", "/admin_side", "actionName", "Test"}),
        @Result(name = ActionSupport.INPUT, location = "Test.jsp")},
    interceptorRefs = {
        @InterceptorRef(value = "defaultStack", params = {"params.acceptParamNames", "price", "validation.validateAnnotatedMethodOnly", "true"})
    })
    public String insert() {
        return ActionSupport.SUCCESS;
    }

    //This method is worth nothing. It is used just to return an initial view on page load.
    @Action(value = "Test",
    results = {
        @Result(name = ActionSupport.SUCCESS, location = "Test.jsp"),
        @Result(name = ActionSupport.INPUT, location = "Test.jsp")},
    interceptorRefs = {
        @InterceptorRef(value = "defaultStack", params = {"params.acceptParamNames", "", "params.excludeMethods", "load", "validation.validateAnnotatedMethodOnly", "true"})})
    public String load() throws Exception {
        return ActionSupport.SUCCESS;
    }
}

下面是表格。

<s:form namespace="/admin_side" action="Test" id="dataForm" name="dataForm">
    <s:fielderror fieldName="price"/>
    <s:textfield id="price" name="price"/>

    <s:submit value="Submit" action="Add"/>
</s:form>

我想实现,

  1. 如果该字段留空,那么唯一的消息,价格是必填项。 应通过@RequiredFieldValidator 显示
  2. 如果输入像“abc”这样的非数字值,它应该只显示来自属性文件的转换错误消息。
  3. 如果尝试使用负值,那么唯一的消息,价格不能小于或等于零。应该通过@FieldExpressionValidator 出现。

一次应该出现一个转换错误或一个验证错误。

这可能吗?到目前为止,我还没有正确理解 shourtCircuit 属性的功能。

【问题讨论】:

  • 您是只显示错误还是要阻止验证?
  • 是的,我说的是预防。一次只能发生一个。其余的必须忽略。如果转换失败,那么检查任何验证都没有意义。如果转换成功,则应按定义的顺序执行验证,应验证第一个空字段,如果成功则应检查值的范围/长度等。如果一个验证失败,那么其余的必须被忽略。这个可以吗?

标签: java jsp validation struts2 struts2-convention-plugin


【解决方案1】:

乍一看没见过这么多。但是看看Type Conversion Error Handling 我会说有一种方法可以处理转换错误。通过将转换验证器添加到短路验证器的配置中。 Short-circuit 表示如果此类验证器有错误,则跳过其他验证器。

 conversionErrorFields = @ConversionErrorFieldValidator(fieldName = "price", message = "Price has invalid value", shortCircuit = true) 

将此代码放在@Validations 注释下。

【讨论】:

  • 我目前在不同的位置,无法尝试。我稍后会尝试(两天或更多天后)。谢谢。
  • 题外话:我想知道你怎么能一天有 41 票(至少到现在为止)?每日投票限制为 40(最大值)。
  • 我不知道有一天我认为这取决于代表,但随着代表长大,限制没有改变,我认为它有一些额外的投票超过限制。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多