【发布时间】:2013-09-16 06:33:19
【问题描述】:
下面是我的控制器:
@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) 是检查格式的好方法吗?
【问题讨论】:
-
请查看以下是否有帮助:stackoverflow.com/questions/2513031/…
标签: java spring spring-mvc spring-3