Spring MVC 前后台数据交互

本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除。

原文网址地址:《Spring MVC 前后台数据交互》

1、服务端数据到客户端

(1)返回页面,Controller中方法返回String,String对应的是view的位置,如果需要携带数据通过model(相当于一个Map)传递到view, view中使用jstl的EL表达式来绑定model带来的数据。

1 @RequestMapping(value="/getPojoView", method=RequestMethod.GET)  
2 public String getPojoView(Model model){  
3   Pojo pojo = new Pojo();  
4   pojo.setPojoName("testName");  
5   pojo.setPojoValue("testValue");  
6   model.addAttribute(pojo);  
7   return"sample/pojoView";  
8 }  

(2)返回Json对象,利用@ResponseBody来实现。

1 @RequestMapping(value="/getPojoJson", method=RequestMethod.GET)  
2 public @ResponseBody Pojo getPojoJson(){  
3   Pojo pojo = new Pojo();  
4   pojo.setPojoName("testName");  
5   pojo.setPojoValue("testValue");  
6   return pojo;  
7 }  

注:spring mvc自动将java对象转化成了json对象传回了客户端,返回对象可以是Pojo也可以是List

(3)直接操作Response自己实现想要的效果。

1 @RequestMapping(value="/getCustomResponse", method=RequestMethod.GET)  
2 public void getCustomResponse(HttpServletResponse response){  
3   //操作response...  
4 }  

注:response为spring根据方法的type类型注入的

2、客户端数据到服务端

(1)通过URL传回参数:

 1 <script type="text/javascript"src="jquery-1.4.min.js"></script>  
 2 <h1>button与链接效果一致</h1>  
 3 <a href="simple?name=text&age=28">simple</a><button onclick="simple()">simple</button><br/>  
 4 <script type="text/javascript">  
 5 function simple(){  
 6   $.getJSON("simple",{"name":"nameJsonTest","age":"100"},function(){});  
 7 }  
 8 </script>  
 9 <a href="list?names[]=aaaa&names[]=bbbb">list</a><button onclick="list()">list</button><br/>  
10 <script type="text/javascript">  
11 function list(){  
12   $.getJSON("list",{"names":["name1","name2","name3"]},function(){});  
13 }  
14 </script>  
15 <a href="pojo?pojo[pojoName]=hahaha&pojo[pojoValue]=kkkkkk">pojo</a><button onclick="pojo()">pojo</button><br/>  
16 <script type="text/javascript">  
17 function pojo(){  
18   $.getJSON("pojo",{"pojo":{"pojoName":"testName","pojoValue":"testValue"}},function(){});  
19 }  
20 </script>  
21 <a href="rest/10/2">rest</a><button onclick="rest()">rest</button><br/>  
22 <script type="text/javascript">  
23 function rest(){  
24   var pageSize = 20;  
25   var pageNo = 3;  
26   $.getJSON("rest/"+pageSize+"/"+pageNo,{},function(){});  
27 }  
28 </script>   

Controller:

 1 import org.springframework.stereotype.Controller;  
 2 import org.springframework.web.bind.annotation.PathVariable;  
 3 import org.springframework.web.bind.annotation.RequestMapping;  
 4 import org.springframework.web.bind.annotation.RequestMethod;  
 5 import org.springframework.web.bind.annotation.RequestParam;  
 6   
 7 @Controller  
 8 @RequestMapping(value="/urlparam")  
 9 public class UrlParamController {  
10   @RequestMapping(value="/", method=RequestMethod.GET)  
11   public String index(){  
12     return"urlparam/index";  
13   }  
14    
15   @RequestMapping(value="/simple", method=RequestMethod.GET)  
16   public void simple(@RequestParam String name, @RequestParam Integer age){  
17     System.out.println("name:"+name);  
18     System.out.println("age:"+age);  
19   }  
20   
21   //list内不能放POJO对象  
22   @RequestMapping(value="/list", method=RequestMethod.GET)  
23   public void list(@RequestParam("names[]") String[] names){   
24   //也可以用List<String> names来接收  
25   for(String name : names){  
26     System.out.println("name:"+name);  
27   }  
28 }  
29   
30   //单URL目前还不支持POJO对象,只能支持键值对,希望spring以后有所改善  
31   @RequestMapping(value="/pojo", method=RequestMethod.GET)  
32   public void pojo(@RequestParam("pojo[pojoName]") String name, @RequestParam("pojo[pojoValue]") String value){  
33     System.out.println("name:"+name);  
34     System.out.println("value:"+value);  
35   }  
36   
37   @RequestMapping(value="/rest/{pageSize}/{pageNo}", method=RequestMethod.GET)  
38   public void rest(@PathVariable Integer pageSize, @PathVariable Integer pageNo){  
39     System.out.println("pageSize:"+pageSize);  
40     System.out.println("pageNo:"+pageNo);  
41   }  
42 }   

(2)通过POST表单传回参数:

方式同与url的是一致的,需要将method=RequestMethod.POST,不过有中文的话一般都用post来避免转码。一般ajax的时候用$.post而不能使用jQuery插件json的$.postJSON。下面会讲到。

 通过使用jQuery插件json的$.postJSON传回参数:
$.postJSON返回的是:application/json,
$.post返回的是: application/x-www-form-urlencoded
spring会将postJSON传回的json字符串转换成对象再将对象丢给带有@RequestBody的形参。 由于json字符串直接转换为对象,所以@RequestBody只能接收一个对象还需要属性一一对应,不能多传参数。此方式可以传POJO,也可以传 List<POJO>。

$.postJSON('url', {"name":"testName","age":"28"},function(){});  
1 @RequestMapping(value="pojo", method=RequestMethod.POST)  
2 publicvoid sentPojo(@RequestBody Pojo pojo){  
3   System.out.println(pojo.getPojoName());  
4   System.out.println(pojo.getPojoValue());  
5 }  

注:目前对于一对象,附带几个简单参数的解决办法是将简单参数通过为REST的url路径参数来传送。

(3) 直接拿到Request来操作:

1 @RequestMapping(value="/", method=RequestMethod.GET)  
2   public String withRequest(HttpServletRequest request){  
3      //操作request...  
4    return"someview";  
5 }  
6  

以上controller内的方法的形参, 除了@RequestBody和@RequestParam不能同时声明外,都可以进行组合来满足各种需求。
小结:spring mvc3充分利用了annotation的好处将参数传递过程中的转换全部变为了透明,这样省去了程序员对参数进行一些无聊的转换,这肯定能提高不少效 率。另一方面想说的是spring的@RequestBody还可以做的更好,如果能允许多个对象同时传送,那这个东西就十分好了。

原文地址:https://www.cnblogs.com/acode/p/5393924.html