#springMVC返回前台数据的方式

本博客为转载博客,原网址为:http://www.tuicool.com/articles/mYJRVb

后台向页面传输数据的方式

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();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值