Spring MVC表单验证 [英] Spring MVC form validation
问题描述
我的问题是我有一个带有html select元素和一些选择选项值&的表单.我想使用:
my problem is that I have a form which has html select element with some choosing option value & I want to validate those value using :
org.hibernate.validator.constraints
or
javax.validation.constraints
注释.这是我的选择元素:
annotations. here is my select element:
<select name="status" id="tbSelect">
<option value="ACTIVE">ACTIVE</option>
<option value="LISTEN">LISTEN</option>
<option value="DOWN">DOWN</option>
</select>
例如,如何使用上面提到的注释验证器来验证select元素中的选项(DOWN,LISTEN,ACTIVE)的值?
how I can for example validate the value of the options(DOWN,LISTEN,ACTIVE) inside the select element by using the annotation validators which I mention above?
我的表单是这样的:
<form:form action="../agents/add" method="POST" commandName="myAgent">
<form:select id="tbSelect" path="state">
<form:option value="ACTIVE" path="state">ACTIVE</form:option>
<form:option value="LISTEN" path="state">LISTEN</form:option>
<form:option value="DOWN" path="state">DOWN</form:option>
</form:select>
我已经这样定义了我的控制器方法:
I have defined my controller method like this:
@RequestMapping(value = "agents/add", method = RequestMethod.POST)
public String addAgentSubmit(@ModelAttribute("myAgent") @Valid final AgentValidator agent, BindingResult result, RedirectAttributes redirect) {
if (result.hasErrors()) {
return "admin/agent/add";
}
...
}
我还定义了一个ModelAttribute:
and I also define a ModelAttribute like this:
@ModelAttribute("myAgent")
public AgentValidator getLoginForm() {
return new AgentValidator();
}
这也是我的AgentValidator类:
Here is my AgentValidator class also:
public class AgentValidator {
@NotEmpty(message = "your state can not be empty !")
private String state;
推荐答案
由于您的state
字段看起来更像枚举,因此我首先建议将state
字段更改为枚举,让Spring MVC将其绑定字段,并仅使用@NotNull
批注:
Since your state
field looks more like an enumeration, first of all I would recommend to change state
field into enum, let Spring MVC to bind that field and use only @NotNull
annotation:
public class AgentValidator {
@NotNull(message = "your state can not be empty !")
private AgenState state;
AgentState
所在的位置:
public enum AgentState {
DOWN,LISTEN,ACTIVE
}
But if for certain reasons you can't change your model, then you may use custom constraints.
特别是您需要创建注释AgentStateConstraint
:
Particular you need to create your annotation AgentStateConstraint
:
@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = AgentStateConstraintValidator.class)
@Documented
public @interface AgentStateConstraint {
String message() default "Some message";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
然后您需要创建验证器AgentStateConstraintValidator
:
Then you need to create validator AgentStateConstraintValidator
:
public class AgentStateConstraintValidator implements ConstraintValidator<AgentStateConstraint, String> {
//Accepted values
private static final Set<String> ACCEPTED_VALUES = new HashSet<String>(
Arrays.asList(
"DOWN",
"LISTEN",
"ACTIVE"
)
);
public void initialize(AgentStateConstraint constraintAnnotation) {
}
public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
return ACCEPTED_VALUES.contains(object);
}
}
这篇关于Spring MVC表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!