如何在springmvc中获取request对象
展开全部
1 注入
@Autowired
private HttpServletRequest request;
2 请求方法中直接获取
@RequestMapping("/xx")
public String listui(HttpServletRequest request){
return "/xx";
}
3、直接写个方法给你吧
/**
* 获取当前Request对象.
*
* @return 当前Request对象 可能为null
* @throws IllegalStateException 当前线程不是web请求抛出此异常.
*/
protected HttpServletRequest currentRequest() throws IllegalStateException {
ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attrs == null) {
throw new IllegalStateException("当前线程中不存在 Request 上下文");
}
return attrs.getRequest();
}
@Autowired
private HttpServletRequest request;
2 请求方法中直接获取
@RequestMapping("/xx")
public String listui(HttpServletRequest request){
return "/xx";
}
3、直接写个方法给你吧
/**
* 获取当前Request对象.
*
* @return 当前Request对象 可能为null
* @throws IllegalStateException 当前线程不是web请求抛出此异常.
*/
protected HttpServletRequest currentRequest() throws IllegalStateException {
ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attrs == null) {
throw new IllegalStateException("当前线程中不存在 Request 上下文");
}
return attrs.getRequest();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询