下面是我的控制器:
@RequestMapping("Student/{ID}/{Name}/{Age}")
public void addStudent(@PathVariable String ID,
@PathVariable String Name,
@PathVariable String Age,
HttpServletRequest request,
HttpServletResponse response) throws IOException {
try {
// Handling some type errors
new Integer(Age); // Examine the format of age
StatusBean bean = new StatusBean();
bean.setResonCode("000"); // Success/Fail reasons
bean.setStatus("1"); // 1: Success; 0: Fail
outputJson(bean, response);
}
catch (NumberFormatException e) {
...
}
catch (Exception e) {
...
}
学生ID、姓名、年龄由用户输入,这些变量不能为空或空格。
在正常情况下,控制器可以处理http://localhost:8080/Student/003/Peter/17
. 但是,它无法处理诸如http://localhost:8080/Student///17
或之类的情况http://localhost:8080/Student/003/Peter/
。如果我想在我的控制器中处理这些情况,我该怎么办?
另一个问题是, new Integer(Age) 是检查格式的好方法吗?