Spring MVC 前后台数据交互

时间:2023-03-09 05:47:46
Spring MVC 前后台数据交互

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

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

1、服务端数据到客户端

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

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

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

 @RequestMapping(value="/getPojoJson", method=RequestMethod.GET)
public @ResponseBody Pojo getPojoJson(){
Pojo pojo = new Pojo();
pojo.setPojoName("testName");
pojo.setPojoValue("testValue");
return pojo;
}

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

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

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

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

2、客户端数据到服务端

(1)通过URL传回参数:

 <script type="text/javascript"src="jquery-1.4.min.js"></script>
<h1>button与链接效果一致</h1>
<a href="simple?name=text&age=28">simple</a><button onclick="simple()">simple</button><br/>
<script type="text/javascript">
function simple(){
$.getJSON("simple",{"name":"nameJsonTest","age":"100"},function(){});
}
</script>
<a href="list?names[]=aaaa&names[]=bbbb">list</a><button onclick="list()">list</button><br/>
<script type="text/javascript">
function list(){
$.getJSON("list",{"names":["name1","name2","name3"]},function(){});
}
</script>
<a href="pojo?pojo[pojoName]=hahaha&pojo[pojoValue]=kkkkkk">pojo</a><button onclick="pojo()">pojo</button><br/>
<script type="text/javascript">
function pojo(){
$.getJSON("pojo",{"pojo":{"pojoName":"testName","pojoValue":"testValue"}},function(){});
}
</script>
<a href="rest/10/2">rest</a><button onclick="rest()">rest</button><br/>
<script type="text/javascript">
function rest(){
var pageSize = 20;
var pageNo = 3;
$.getJSON("rest/"+pageSize+"/"+pageNo,{},function(){});
}
</script>

Controller:

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; @Controller
@RequestMapping(value="/urlparam")
public class UrlParamController {
@RequestMapping(value="/", method=RequestMethod.GET)
public String index(){
return"urlparam/index";
} @RequestMapping(value="/simple", method=RequestMethod.GET)
public void simple(@RequestParam String name, @RequestParam Integer age){
System.out.println("name:"+name);
System.out.println("age:"+age);
} //list内不能放POJO对象
@RequestMapping(value="/list", method=RequestMethod.GET)
public void list(@RequestParam("names[]") String[] names){
//也可以用List<String> names来接收
for(String name : names){
System.out.println("name:"+name);
}
} //单URL目前还不支持POJO对象,只能支持键值对,希望spring以后有所改善
@RequestMapping(value="/pojo", method=RequestMethod.GET)
public void pojo(@RequestParam("pojo[pojoName]") String name, @RequestParam("pojo[pojoValue]") String value){
System.out.println("name:"+name);
System.out.println("value:"+value);
} @RequestMapping(value="/rest/{pageSize}/{pageNo}", method=RequestMethod.GET)
public void rest(@PathVariable Integer pageSize, @PathVariable Integer pageNo){
System.out.println("pageSize:"+pageSize);
System.out.println("pageNo:"+pageNo);
}
}

(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(){});  
 @RequestMapping(value="pojo", method=RequestMethod.POST)
publicvoid sentPojo(@RequestBody Pojo pojo){
System.out.println(pojo.getPojoName());
System.out.println(pojo.getPojoValue());
}

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

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

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

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