最近玩一下SpringMVC,代码如下:
@Controller
@RequestMapping("/test.do")
public class TestController {
@RequestMapping(method = RequestMethod.GET)
public ModelAndView sync(Model m,
@RequestParam("fid") String fid,
@RequestParam("sid") String sid) throws Exception {
if(fid==null || sid==null) {
m.addAttribute("file", new FileMetaDAO());
return new ModelAndView(ViewProvider.UPLOAD);
} else {
m.addAttribute("sync", new SyncDAO());
return new ModelAndView(ViewProvider.HR_SYNC);
}
}
}
访问http://localhost:8080/hellorest/test.do 时发生如下问题: 查了一下Annotation Type RequestParam的javadoc,加上可选参数required解决战斗。
@Controller
@RequestMapping("/test.do")
public class TestController {
@RequestMapping(method = RequestMethod.GET)
public ModelAndView sync(Model m,
@RequestParam(value="fid", required = false) String fid,
@RequestParam(value="sid", required = false) String sid)
throws Exception {
if(fid==null || sid==null) {
m.addAttribute("file", new FileMetaDAO());
return new ModelAndView(ViewProvider.UPLOAD);
} else {
m.addAttribute("sync", new SyncDAO());
return new ModelAndView(ViewProvider.HR_SYNC);
}
}
}