📜  struts 2双重验证示例

📅  最后修改于: 2021-01-11 06:28:45             🧑  作者: Mango

Struts 2双重验证示例

双重验证器检查给定的浮点数是否在指定范围内。可用于产品价格等

双重验证器的参数

为双重验证器定义了5个参数。

Parameter Description
fieldName specifies the field name that is to be validated. It is required in Plain-Validator only.
minInclusive specifies the minimum inclusive value. It is ignored bydefault.
maxInclusive specifies the maximum inclusive value. It is ignored bydefault.
minExclusive specifies the minimum exclusive value. It is ignored bydefault.
maxExclusive specifies the maximum exclusive value. It is ignored bydefault.

双重验证器的示例


 
          
              price
              100.0
              10000.0
              Price must be between ${minInclusive} and ${maxInclusive}
          
          


    
          
              
                 100.0
                 10000.0
                 Price must be between ${minInclusive} and ${maxInclusive}
          
          


双重验证器的完整示例

1)创建index.jsp作为输入

该jsp页面使用struts UI标记创建表单。它从用户那里接收名称,密码和电子邮件ID。

<%@ taglib uri="/struts-tags" prefix="s" %>














2)创建动作类

该操作类继承了ActionSupport类并覆盖了execute方法。

package com.javatpoint;

import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport{
private int id;
private double price;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}

public String execute(){
    return "success";
}

}

3)创建验证文件

在这里,我们使用捆绑的验证器来执行验证。



  

          
          
          
          
            100.0
          9999.9
          
          Price must be between ${minInclusive} to ${maxExclusive}
          
          
          

4)创建struts.xml

该xml文件通过名称输入和拦截器jsonValidatorWorkflowStack定义了额外的结果。






index.jsp
welcome.jsp



    
  
 

5)创建视图组件

它是显示用户信息的简单jsp文件。

<%@ taglib uri="/struts-tags" prefix="s" %>

Product Id:
Product price: