package com.hllq.quan.controller;import com.hllq.quan.mapper.WeiboUserMapper;
import com.hllq.quan.model.WeiboUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import com.hllq.quan.mapper.WeiboUserMapper;import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;@RestController
public class WeiboUserController { @Autowired
private WeiboUserMapper weiboUserMapper; @RequestMapping("/selectall")
public List<WeiboUser> selectAll(){
return weiboUserMapper.selectAll();
}// @RequestMapping("/selectbyid/{id}")
// public WeiboUser selectById(@PathVariable("id") int id){
// return weiboUserMapper.selectById(id);
// } @InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}// 获取url参数值,默认方式,需要方法参数名称和url参数保持一致http://localhost:8080/selectbyid?id=2
@RequestMapping(value = "/selectbyid",method = RequestMethod.GET)
public WeiboUser selectById(@RequestParam Integer id){
return weiboUserMapper.selectById(id);
} @RequestMapping(value = "/insert", method = RequestMethod.GET)
public int insertWeiboUser( WeiboUser weiboUser){
weiboUserMapper.insertWeiboUser(weiboUser);
return 1;
} @RequestMapping(value = "/updatenamebyid" ,method = RequestMethod.GET)
public void updateWeiboUserName(WeiboUser weiboUser){
weiboUserMapper.updateWeiboUserName(weiboUser);
} @RequestMapping(value = "/deletebyid",method = RequestMethod.GET)
public void deleteById(@RequestParam Integer id){
weiboUserMapper.deleteById(id);
}
}
@RestController:用于标注控制层组件(如struts中的action),@ResponseBody和@Controller的合集
@RequestMapping:提供路由信息,负责URL到Controller中的具体函数的映射。
value: 指定请求的实际地址
method: 指定请求的method类型, GET、POST、PUT、DELETE等
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回
params: 指定request中必须包含某些参数值是,才让该方法处理
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求
@Autowired:自动导入依赖的bean
@PathVariable指定URL变量名,前提是RequesrMapping需要用{}表面他的变量部分
@RequestMapping("/users/{username}"){username}就是我们定义的变量规则,username是变量的名字
获取的时候: public String userProfile(@PathVariable String username)
或者: public String userProfile(@PathVariable("username") String username)存在多个的时候:
@RequestMapping("/user/{username}/blog/{blogId}")
@ResponseBody
public String getUerBlog(@PathVariable String username , @PathVariable int blogId)也可以使用正则表达式,匹配更加精确的变量,定义语法是{变量名:正则表达式}
@RequestMapping("/user/{username:[a-zA-Z0-9_]+}/blog/{blogId}")
@RequestParam:将请求参数绑定到你控制器的方法参数上
语法:@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””)value:参数名required:是否包含该参数,默认为true,表示该请求路径中必须包含该参数,如果不包含就报错。defaultValue:默认参数值,如果设置了该值,required=true将失效,自动为false,如果没有传该参数,就使用默认值
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
经常会遇到表单中的日期字符串和JavaBean的Date类型的转换,
在需要日期转换的Controller中使用SpringMVC的注解@initbinder和Spring自带的WebDateBinder类来操作。
WebDataBinder是用来绑定请求参数到指定的属性编辑器.由于前台传到controller里的值是String类型的,当往Model里Set这个值的时候,如果set的这个属性是个对象,
Spring就会去找到对应的editor进行转换,然后再SET进去。