#springMVC返回前台数据的方式
2016-04-25 14:07
435 查看
本博客为转载博客,原网址为:http://www.tuicool.com/articles/mYJRVb
对应的前台jsp页面:
2、直接使用map和页面地址的方式:
3、使用ModelAndView方式,对应的页面地址也放在ModelAndView对象中(一般使用该方式):
4、使用直接将数据流写到前台而不进行页面跳转的方式,该方式一般用于ajax请求:
4.1、使用HttpServletResponse中的printWriter方法进行输出的方式:
4.2、直接使用printWriter方式进行数据流输出:
在使用PrintWriter类进行数据流输出时需要将对应的对象转换为json串对应转换方式如下:
后台向页面传输数据的方式
1、使用ModelAndView和map/** * 方法的返回值采用ModelAndView, new ModelAndView("index", map);, * 相当于把结果数据放到request里面 * @return * @throws Exception */ @RequestMapping("/toPerson4.do") public ModelAndView toPerson4() throws Exception{ Person person = new Person(); person.setName("jerome"); person.setAge(22); person.setAddress("nanan"); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = format.parse("2012-12-21"); person.setBirthday(date); Map<String,Object> map = new HashMap<String, Object>(); map.put("p", person); return new ModelAndView("jsp/index",map); }
对应的前台jsp页面:
<body> <h5>${p.name }</h5> <h5>${p.age }</h5> <h5>${p.address }</h5> <h5><fmt:formatDate value="${p.birthday }" pattern="yyyy-MM-dd"/></h5> </body>
2、直接使用map和页面地址的方式:
/** * 直接在方法的参数列表中来定义Map,这个Map即使ModelAndView里面的Map * 由视图解析器统一处理,统一走ModelAndView的接口 * 也不建议使用 * @param map * @return * @throws Exception */ @RequestMapping("/toPerson5.do") public String toPerson5(Map<String,Object> map) throws Exception{ Person person = new Person(); person.setName("jerome"); person.setAge(22); person.setAddress("nanan"); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = format.parse("2012-12-21"); person.setBirthday(date); map.put("p", person); return "jsp/index"; }
3、使用ModelAndView方式,对应的页面地址也放在ModelAndView对象中(一般使用该方式):
/** *在参数列表中直接定义Model,model.addAttribute("p", person); *把参数值放到request类里面去,建议使用 * @param map * @return * @throws Exception */ @RequestMapping("/toPerson6.do") public String toPerson6(Model model) throws Exception { Person person = new Person(); person.setName("jerome"); person.setAge(22); person.setAddress("nanan"); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = format.parse("2012-12-21"); person.setBirthday(date); //把参数值放到request类里面去 model.addAttribute("p", person); return "jsp/index"; }
4、使用直接将数据流写到前台而不进行页面跳转的方式,该方式一般用于ajax请求:
4.1、使用HttpServletResponse中的printWriter方法进行输出的方式:
/** * ajax的请求返回值类型应该是void,参数列表里直接定义HttpServletResponse, * 获得ProntWriter的类,最后可把结果写到页面 * 不建议使用 * @param name * @param response */ @RequestMapping("/ajax.do") public void ajax(String name, HttpServletResponse response) { String result = "hello " + name; try { response.getWriter().write(result); } catch (IOException e) { e.printStackTrace(); } }
4.2、直接使用printWriter方式进行数据流输出:
/** * 直接在参数的列表上定义PrintWriter,out.wrote(result); * 把结果写到页面,建议使用 * @param name * @param out */ @RequestMapping("/ajax1.do") public void ajax1(String name, PrintWriter out) { String result="hello1 "+name; out.write(result); }
在使用PrintWriter类进行数据流输出时需要将对应的对象转换为json串对应转换方式如下:
import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.util.CycleDetectionStrategy; public class JSONUtils { /** * 将对象转换为json字符串 * @param obj * @return */ public static String object2String(Object obj){ JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); JSONObject json =JSONObject.fromObject(obj, jsonConfig); //JSONObject json = JSONObject.fromObject(obj); return json.toString(); } /** * 将数组转换为json字符串 * @param obj * @return */ public static String array2String(Object obj){ JSONArray jsonArray = JSONArray.fromObject(obj); return jsonArray.toString(); } }
相关文章推荐
- Spring整合Dwr,Cannot locate BeanDefinitionParser for element [annotation-scan]
- spring3 MVC手工搭建Spring3项目demo
- spring mvc 开发需要的jar包的用途详解
- 基于注解的 Spring MVC 简单入门-网摘
- [MVC] 深入浅出Spring MVC
- spring mvc的web.xml的配置
- SpringMVC工作原理
- Spring MVC 为例,从前台—>后台—>前台的编解码过程及出现乱码的案例
- 在spring mvc与mybatis中加入logback
- springmvc+mybatis
- 当项目使用SpringMvc时拦截器的注意事项
- (struts2) spring mvc + mybatis + maven整合
- 文章标题
- Spring+Spring MVC 整合JasperReports
- 使用springmvc框架之第1篇:spring中bean的系统自动注册
- SpringMVC Json
- ajax跨域:JQuery + Spring MVC3.0
- 项目经验之springmvc单元测试
- spring mvc详解1
- Spring MVC和Struts2的区别