package com.jys.myBatisSpring.controller;
import com.jys.myBatisSpring.entity.User;
import com.jys.myBatisSpring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by anyx on 14-1-21.
补充知识:
Spring 3.X系列增加了新注解@ResponseBody,@RequestBody
作为@RequestBody和@ResponseBody, Spring使用HttpMessageConverter在request流和response流之间转换.
@RequestBody RequestMappingHandlerAdapter适配器使用合适的HttpMessageConverter转换器,将HTTP请求正文转换为对应的方法参数。
@ResponseBody RequestMappingHandlerAdapter适配器使用合适的HttpMessageConverter转换器,将要返回的内容或对象进行转换。
使用自动转换器HttpMessageConverter,需要开启<mvc:annotation-driven />。
AnnotationMethodHandlerAdapter将会初始化如下的转换器:
在@RequestMapping或@ExceptionHandler方法里,HttpMessageConverter转换器支持@RequestBody标注的方法参数 and @ResponseBody标注的方法返回值的转换 .
完整的HttpMessageConverters转换器列表如下,由mvc:annotation-driven开启:
ByteArrayHttpMessageConverter converts byte arrays.
StringHttpMessageConverter converts strings.
ResourceHttpMessageConverter converts to/from org.springframework.core.io.Resource for all media types.
SourceHttpMessageConverter converts to/from a javax.xml.transform.Source.
FormHttpMessageConverter converts form data to/from a MultiValueMap<String, String>.
Jaxb2RootElementHttpMessageConverter converts Java objects to/from XML — added if JAXB2 is present on the classpath.
MappingJackson2HttpMessageConverter (or MappingJacksonHttpMessageConverter) converts to/from JSON — added if Jackson 2 (or Jackson) is present on the classpath.
AtomFeedHttpMessageConverter converts Atom feeds — added if Rome is present on the classpath.
RssChannelHttpMessageConverter converts RSS feeds — added if Rome is present on the classpath.
*/
@Controller
public class UserController {
@Autowired /*自动装配*/
public UserService userService;
@RequestMapping(value="/user/list.action",method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> list() {
List<User> listUser = this.userService.selectUser(null);
Map<String, Object> map = new HashMap<String, Object>();
if (listUser.size() >= 0) {
map.put("hasError", "false");
map.put("results", listUser.size());
map.put("rows", listUser);
}else{
map.put("hasError", "true");
map.put("results", "0");
map.put("rows", null);
map.put("error", "控制器取数据出错");
}
/*@ResponseBody将内容或对象作为HTTP响应正文返回,它会调用适合HttpMessageConverter的Adapter转换对象,写入输出流*/
/*HttpMessageConverter的Adapter对Map转换时,默认调用MappingJacksonHttpMessageConverter进行转换*/
/*从名称可以看出SpringMVC中,Map默认由Jackson进行转换(需要引入额外的Jackson包)*/
return map;
}
/*@PathVariable方式接收客户端传参*/
/*@PathVariable绑定方法参数到客户端传递过来的URL对应参数上*/
/*适合客户端传递的参数较少,又不怕把传入参数暴露给url的情况*/
@RequestMapping(value="/user/ins/{id1}/{name1}/pathVariable.action",method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> insertUserPathVariable(@PathVariable String id1, @PathVariable String name1) {
/*可以看出,如果User对象有很多属性时,这种方法不适用*/
User user = new User();
user.setId1(Integer.parseInt(id1));
user.setName1(name1);
this.userService.insertUser(user);
List<User> listUser = this.userService.selectUser(null);
Map<String, Object> map = new HashMap<String, Object>();
if (listUser.size() >= 0) {
map.put("hasError", "false");
map.put("results", listUser.size());
map.put("rows", listUser);
}else{
map.put("hasError", "true");
map.put("results", "0");
map.put("rows", null);
map.put("error", "控制器取数据出错");
}
return map;
}
/*@RequestParam方式接收客户端传参,客户端必须采用GET方式提交*/
/*@RequestParam绑定方法参数到客户端传递过来的对应参数上*/
/*适合客户端传递的参数较少的情况*/
@RequestMapping(value="/user/ins/requestParam.action",method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> insertUserRequestParam(@RequestParam String id1, @RequestParam String name1) {
/*同样,如果User对象有很多属性时,这种方法也不适用*/
User user = new User();
user.setId1(Integer.parseInt(id1));
user.setName1(name1);
this.userService.insertUser(user);
List<User> listUser = this.userService.selectUser(null);
Map<String, Object> map = new HashMap<String, Object>();
if (listUser.size() >= 0) {
map.put("hasError", "false");
map.put("results", listUser.size());
map.put("rows", listUser);
}else{
map.put("hasError", "true");
map.put("results", "0");
map.put("rows", null);
map.put("error", "控制器取数据出错");
}
return map;
}
/*[推荐]*/
/*@RequestBody方式接收客户端传参*/
/*@RequestBody把客户端传递过来的JSON格式参数采用合适的转换器装配到User对象中*/
/*适合客户端传递的参数较多*/
@RequestMapping(value="/user/ins/requestBody.action",method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> insertUserRequestBody(@RequestBody User user) {
/*@ResponseBody适合User对象有很多属性的情况*/
this.userService.insertUser(user);
List<User> listUser = this.userService.selectUser(null);
Map<String, Object> map = new HashMap<String, Object>();
if (listUser.size() >= 0) {
map.put("hasError", "false");
map.put("results", listUser.size());
map.put("rows", listUser);
}else{
map.put("hasError", "true");
map.put("results", "0");
map.put("rows", null);
map.put("error", "控制器取数据出错");
}
return map;
}
/*通过id查询,不适合使用RequestBody方式接收客户端传参,采用@RequestParam更适合*/
@RequestMapping(value = "/user/findById.action",method = RequestMethod.GET)
@ResponseBody
public Map<String,Object> findUser(@RequestParam String id1) {
Map<String, Object> map = new HashMap<String, Object>();
if( id1 !="" || id1 !="0") {
User user=new User();
user.setId1(Integer.parseInt(id1));
List<User> listUser = this.userService.selectUser(user);
if (listUser.size() >= 0) {
map.put("hasError", "false");
map.put("results", listUser.size());
map.put("rows", listUser);
}else{
map.put("hasError", "true");
map.put("results", "0");
map.put("rows", null);
map.put("error", "控制器取数据出错");
}
}else{
map.put("hasError", "true");
map.put("results", "0");
map.put("rows", null);
map.put("error", "控制器取数据出错");
- 1
- 2
- 3
- 4
- 5
前往页