注意:下面的参数签名以及返回值不仅仅可以在@Controller类中的处理请求方法中使用,还可以用于@ExceptionHandler(异常处理的方法中),比如@Controller中的异常处理,或者全局异常处理@ControllerAdvice中
@RequestMapping(value = {"/method.do"}, method = {RequestMethod.GET, RequestMethod.POST,以及其他})
public ModelAndView controllerMethod(各种参数...) {
final ModelAndView view = new ModelAndView(视图名称);
渲染view .......
return view;
}
@ExceptionHandler(value = {Exception.class})
public void exceptionHandler(HttpServletRequest request, Writer writer, Exception ex) throws IOException {
writer.write(ex.getMessage());
//writer.flush();
//writer.close();
}
public @ResponseBody ModelAndView controllerMethod(各种参数...) {}
@ResponseBody ModelAndView可以让页面渲染、返回,Ajax就可以实现刷页面的功能,不需要拼接HTML;
@ResponseBody Map
@RequestMapping(value = {"/login.do"}, method = {RequestMethod.POST})
@ResponseBody public Map controllerMethod(各种参数...) {
Map json = new HashMap<>();
json.put("error", "数据不正确!!");
json.put("success", "成功了!!!");
return json;
}
@RequestMapping(value = {"/method.do"}, method = {RequestMethod.GET, RequestMethod.POST})
public void controllerMethod(
WebRequest webRequest,
NativeWebRequest nativeWebRequest,
HttpServletRequest request,//常见
HttpServletResponse response,//常见
HttpSession session,//常见
PushBuilder pushBuilder,
Principal principal,
HttpMethod method,
Locale locale,//Java8的time API
TimeZone timeZone,//Java8的time API
ZoneId zoneId,//Java8的time API
InputStream is,
Reader reader,
OutputStream os,
Writer writer,
HttpEntity httpEntity,
Errors errors,
BindingResult result,
SessionStatus sessionStatus,
@PathVariable String path,//常见
@MatrixVariable String matri,
@RequestParam String username,
@RequestHeader String header,
@CookieValue String cookie,
@RequestBody String body,//常见
@RequestPart String part,
@SessionAttribute String valueInSession,
@RequestAttribute String valueInRequest) {
}
这里需要插播一段引用, http://tomcat.apache.org/whichversion.html
Tomcat版本 - Servlet版本 - JDK版本