[英]Spring validation set field value
I'm currently trying to familiarize myself with spring validation for web pages. 我目前正在尝试使自己熟悉网页的Spring验证。 Most of the examples I see online show how to send an error message for validation.
我在网上看到的大多数示例都显示了如何发送错误消息进行验证。 I am curious as to how to go about setting a value for a field as a part of validation.
我很好奇如何为验证设置字段的值。
Here's an example to better explain myself 这是一个更好地解释自己的例子
Imagine some class for a web page that takes the details of someones salary 想象一下某个类的网页,其中包含某人的薪水详细信息
Example of getter and setter getter和setter的示例
public Integer getSalary() {
return Salary;
}
public void setSalary(Integer Salary) {
this.Salary = Salary;
}
A persons salary cannot be less than zero so in the event a negative figure is entered the field is to be updated to be a zero. 人员工资不能小于零,因此,如果输入负数,则字段将更新为零。
How can this be done? 如何才能做到这一点?
Would it be done in the validation class? 可以在验证类中完成吗?
Example section in validation class 验证类中的示例部分
if(!isBlank(emp.getSalary)){
int sal = emp.getSalary;
if(sal < 0){
rejectValue("emp.salary", "error.salaryRedection");
// Do something here?
// e.g getSalary(setSalary(0));
}
}
You could pass the salary before setting to a different method:- 您可以在设置其他方法之前通过薪水:-
public void setSalary(Integer Salary)
{
this.Salary = validatedSalary(Salary);
}
and you can make the method return the right value :- 您可以使该方法返回正确的值:
public int validatedSalary(int Salary)
{
if(Salary < 0)
{
return 0;
}
return Salary;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.