您的位置:首页 > 编程语言 > Java开发

SpringMVC返回数据方式

2015-12-27 20:21 441 查看
SpringMVC返回数据方式

一、使用注解@ResponseBody返回数据,

@ResponseBody
public String search(Product pp) throws Exception {
return "hello";
}
注意,springMVC返回String默认指向的是界面,必须加上@ResponseBody才表示返回数据

二、使用HttpServletRequest,返回数据

@RequestMapping(value = "/add")
public String teamDetails(HttpServletRequest request,HttpServletResponse response) {
request.setAttribute("name", name);
return "name.jsp";
}


三、使用HttpServletResponse返回数据

<pre name="code" class="java"> @RequestMapping("/add")
public void write(String str) throws IOException {
response.setCharacterEncoding("UTF-8");//设置返回数据的编码
response.getWriter().write(str);
response.getWriter().flush();
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: