1 回答
TA贡献1820条经验 获得超2个赞
我发现这个问题的另一个解决方案是我们可以使用一类常量而不是属性文件。
我可以在我工作区的任何地方使用这个常量文件,而属性文件不能在Size.max模型验证的属性中使用
CommonConstants.java
public class CommonConstants
{
public static final int NAME=4;
public static final int ROLE=2;
}
Employee.java
private int id;
@Size(max = CommonConstants.NAME, message = "length exceeds : name")
private String name;
@Size(max = CommonConstants.ROLE, message = "length exceeds : role")
private String role;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
EmployeeController.java
@Controller
public class EmployeeController {
private static final Logger logger = LoggerFactory.getLogger(EmployeeController.class);
private Map<Integer, Employee> emps = null;
public EmployeeController() {
emps = new HashMap<Integer, Employee>();
}
@RequestMapping(value = "/emp/save.do", method = RequestMethod.POST)
public String saveEmployeeAction(@Valid Employee employee, BindingResult bindingResult, Model model)
{
if (bindingResult.hasErrors()) {
logger.info("Returning empSave.jsp page");
return "empSave";
}
logger.info("Returning empSaveSuccess.jsp page");
model.addAttribute("emp", employee);
emps.put(employee.getId(), employee);
return "empSaveSuccess";
}
}
输出
添加回答
举报