I do a search by criteria with an DTO entity for filter in the front-end of my application:
我按照DTO实体的标准搜索我的应用程序前端的过滤器:
public class MyFilter implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
@Enumerated(EnumType.STRING)
private AccessType accessType;
private List<MyType> userType;
private List<OfficeLocation> officeLocation;
private List<Language> languages;
private String country;
}
and getters and setters.
和吸气剂和二传手。
In my controller:
在我的控制器中:
@RequestMapping
public ModelAndView list(@ModelAttribute("filter") MyFilter myFilter, BindingResult result) {
final ModelAndView mav = new ModelAndView("list");
// validate
MyFilterValidator.validate(myFilter, result);
mav.addObject("filter", myFilter);
if (result.hasErrors()) {
return mav;
}
// ...
return mav;
}
I want to validate the search form filter by a validator class:
我想通过验证器类验证搜索表单过滤器:
public class MyFilterValidator implements org.springframework.validation.Validator {
公共类MyFilterValidator实现org.springframework.validation.Validator {
@Override
public void validate(Object object, Errors errors) {
final MyFilter myFilter = (MyFilter) object;
if (myFilter == null) {
errors.reject("error.one.field.required");
} else {
if (StringUtils.isEmpty(myFilter.getName()) && myFilter.getAccessType() == null
&& myFilter.getUserType() == null && myFilter.getLanguages() == null
&& StringUtils.isEmpty(myFilter.getCountry())
&& myFilter.getOfficeLocation() == null) {
errors.reject("error.one.field.required");
}
}
}
@Override
public boolean supports(Class inClass) {
return MyFilter.class.equals(inClass);
}
}
I need to validate if one field is filled, minimum one field of my Filter class is filled. How can I do that in a simple way? I need to check each attribute : StringUtils.isEmpty or .size()<=0, ... ? Is it possible to iterate over each property and check if one of them is not null? To know if one field is fill?
我需要验证是否填充了一个字段,填充了我的Filter类的最小一个字段。我怎么能以简单的方式做到这一点?我需要检查每个属性:StringUtils.isEmpty或.size()<= 0,...?是否可以迭代每个属性并检查其中一个属性是否为空?要知道一个字段是否填满?
1 个解决方案
#1
0
If you need this test very often, then it would be worth to implement a small function that inspect some annotated fields of the DAO by reflection.
如果您经常需要此测试,那么实现一个小函数是值得的,该函数通过反射检查DAO的一些带注释的字段。
public DAO {
public String nevermind;
@AtLeastOneOfThem
public String a;
@AtLeastOneOfThem
public String b;
}
/**
* Return True if at least on of the fields annotated by @AtLeastOneOfThem
* is not Empty.
* THIS IS PSEUDO CODE!
*/
public static boolean atLeastOneOfThemIsNotEmpty(Object o) {
for(Field field : getFieldsAnnotatedWith(AtLeastOneOfThem.class, o) {
if (field.get() != null && !field.get().empty()) {
return true;
}
}
return false;
}
If this is too much work, then it would be the fastet way to implment the check in the tradtional handwritten way.
如果这是太多的工作,那么这将是以传统的手写方式实施支票的紧固方式。
#1
0
If you need this test very often, then it would be worth to implement a small function that inspect some annotated fields of the DAO by reflection.
如果您经常需要此测试,那么实现一个小函数是值得的,该函数通过反射检查DAO的一些带注释的字段。
public DAO {
public String nevermind;
@AtLeastOneOfThem
public String a;
@AtLeastOneOfThem
public String b;
}
/**
* Return True if at least on of the fields annotated by @AtLeastOneOfThem
* is not Empty.
* THIS IS PSEUDO CODE!
*/
public static boolean atLeastOneOfThemIsNotEmpty(Object o) {
for(Field field : getFieldsAnnotatedWith(AtLeastOneOfThem.class, o) {
if (field.get() != null && !field.get().empty()) {
return true;
}
}
return false;
}
If this is too much work, then it would be the fastet way to implment the check in the tradtional handwritten way.
如果这是太多的工作,那么这将是以传统的手写方式实施支票的紧固方式。