在spring mvc中使用flush先发送部分网页内容,mvcflush,在耗时的请求中,我们最好
分享于 点击 37053 次 点评:277
在spring mvc中使用flush先发送部分网页内容,mvcflush,在耗时的请求中,我们最好
在耗时的请求中,我们最好先发送一部分请求,这样可以缓解用户等待的焦虑。 在jsp中可以使用out.flush()
实现发送部分内容,那么在spring mvc中该如何实现呢?
首先需要在spring mvc的请求处理方法添加response参数:
如下方法定义:
@RequestMapping(value = "/customReport", method = RequestMethod.GET) public ModelAndView showReport( @ModelAttribute(Constants.SESSION_KEY_LOGIN_USER) AppUser loginUser, @RequestParam(value = "categoryId", required = false) Integer categoryId, @RequestParam(value = "startDate", required = true) Date startDate, @RequestParam(value = "endDate", required = true) Date endDate, HttpServletResponse response) throws IOException { ...}
上面的方法的最后一个参数是HttpServletResponse类型的,spring mvc框架在处理请求时会自动传递response值。
在方法体中可以使用如下方法来flush部分请求:
response.setContentType("text/html;charset=utf-8");response.getWriter().write(waitingHtml);response.getWriter().flush();上面的第一行代码设置http头,这是必须的,如果不设置会导致乱码。第二行代码写入要发送的部分内容第三行代码将部分内容发送到客户端
其实质和jsp中使用是一样的。
用户点评