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

#springMVC返回前台数据的方式

2016-04-25 14:07 435 查看
本博客为转载博客,原网址为: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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息