首页 技术 正文
技术 2022年11月8日
0 收藏 736 点赞 1,247 浏览 3246 个字
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进去。
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295