SpringMVC之Controller下的void与String返回值
众所周知,要编写一个接收请求的方法,一般使用String返回值,那么当使用void返回值如何响应呢?
如果需要使用void的话,当响应时,应使用HttpServletRequest转发或者HttpServletResponse进行重定向
要注意以下几点
- 要想使用request,response需要再方法参数填写HttpServletRequest request,HttpServletResponse response
- 使用request时,转发记得携带相对路径,为什么不直接文件名,因为不会经过视图解析器
- 当使用response时,重定向更要携带路径,也不会走视图解析器
- response重定向时,WEB-INF下的文件不可以访问,且加绝对路径
代码实现
返回值为void时
/**
* 返回为Void时
* @param request
* @param response
*/
@RequestMapping("/testVoid")
public void