这周花了点时间弄了一下spring mvc3,打算整理一个cheatsheet出来,方便后来者快速上手。
服务端数据到客户端
- 返回页面,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";
}
-
返回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
-
直接操作Response自己实现想要的效果。
@RequestMapping(value="/getCustomResponse", method=RequestMethod.GET)
public void getCustomResponse(HttpServletResponse response){
//操作response...
}
注:response为spring根据方法的type类型注入的
客户端数据到服务端
- 通过URL传回参数:
view
<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
package sample;
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);
}
}
-
通过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(){});
class Pojo{
private Long id;
private String name;
private Integer age;
//get,set...
}
@RequestMapping(value="pojo", method=RequestMethod.POST)
publicvoid sentPojo(@RequestBody Pojo pojo){
System.out.println(pojo.getPojoName());
System.out.println(pojo.getPojoValue());
}
注:目前对于一对象,附带几个简单参数的解决办法是将简单参数通过为REST的url路径参数来传送。
- 直接拿到Request来操作:
@RequestMapping(value="/", method=RequestMethod.GET)
public String withRequest(HttpServletRequest request){
//操作request...
return"someview";
}
以上controller内的方法的形参, 除了@RequestBody和@RequestParam不能同时声明外,都可以进行组合来满足各种需求。
小结:spring mvc3充分利用了annotation的好处将参数传递过程中的转换全部变为了透明,这样省去了程序员对参数进行一些无聊的转换,这肯定能提高不少效率。另一方面想说的是spring的@RequestBody还可以做的更好,如果能允许多个对象同时传送,那这个东西就十分好了。
分享到:
相关推荐
十三、如何给spring3 MVC中的Action做JUnit单元测试? 十四、spring mvc 转发与重定向 十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八...
Spring MVC internationalization annotation example 这个是annotation 方式的国际化 访问http://localhost:8080/login.htm
十三、如何给spring3 MVC中的Action做JUnit单元测试? 十四、spring mvc 转发与重定向 十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八...
这里提供了Spring3 MVC中较为常见的使用方法,主要是利用Spring3官方文档建议使用的Annotation进行开发的一般步骤,包含了常见的集中Annotation的使用方法。
一个简单的spring mvc登录案例,采用的是annotation方式。
十三、如何给spring3 MVC中的Action做JUnit单元测试? 十四、spring mvc 转发与重定向 十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean ...
学习JPA——Hibernate_Annotation使用实例学习JPA——Hibernate_Annotation使用实例学习JPA——Hibernate_Annotation使用实例
Spring MVC 整合Hibernate的一个小小的用例,适合初学Spring MVC 的学者。
NULL 博文链接:https://garrincha.iteye.com/blog/2109688
基于注解的Spring MVC,环境搭建,示例代码
博文链接:https://pesome.iteye.com/blog/236273
Table of Contents Introduction Chapter 1: The Spring Framework Chapter 2: Model 2 and the MVC Pattern Chapter 3: Introduction to Spring MVC Chapter 4: Annotation-Based Controllers Chapter 5: Data ...
Spring Annotation
Spring annotation 样例
Spring4 MVC Hibernate4集成,使用Annotation,封装dao层和service层。 环境: spring 4.0.3.RELEASE hibernate 4.3.5.Final mysql 5.1.29
spring mvc annotation interceptor
Ajax With Spring 3. Eclipse 工程,包含Web所需要的 所有jar包。 1> ajax 请求。 2> spring 3注解使用 3> mvc:annotation
Spring mvc Aop+annotation实现系统日志记录功能实现的jar包asm-3.3.jar ,aspectjrt.jar , aspectjweaver.jar , cglib-nodep-2.1_3.jar , spring-aop.jar
With the power of annotation-based configuration, Spring MVC makes web application development easy for developers. This book is a great companion for beginners who want to learn Spring MVC. With ...
使用Spring MVC3基于注解的形式实现的REST小例子 部署运行: 1. 将工程导入eclipse 2. 添加tomcat属性 工程(右键)——> tomcat project——> update context 3. 启动tomcat 4. 在浏览器中输入...