一路繁花似锦绣前程
失败的越多,成功才越有价值

导航

 

一、自动参数匹配

<form action="/login">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="提交">
</form>
@RequestMapping("/login")
public Object login(String username,String password){
    return "成功";
}

  表单中控件的name的值,和controller层方法的参数名一致,则匹配;

<form action="hello" method="post">
    <input type="text" name="username">
    <input type="text" name="password"><br/>
    <input type="text" name="address.province">
    <input type="text" name="address.city"><br/>
    <input type="text" name="list[0].province">
    <input type="text" name="list[0].city"><br/>
    <input type="text" name="map['first'].province">
    <input type="text" name="map['first'].city"><br/>
    <input type="submit" value="提交">
</form>
@RequestMapping("/hello")
public String hello(Person person) {
    System.out.println(person);
    /*
    Person(username=孟美岐, password=123456,
            address=Address(province=江苏省, city=无锡市),
            list=[Address(province=河北省, city=沧州市)],
            map={first=Address(province=新疆, city=乌鲁木齐市)})
    */
    return "success";
}

  参数绑定对象和集合类型,参数名匹配属性名;

 

二、url地址中get形式的参数匹配

url:.../list?pageNum=1&pageSize=10

@RequestMapping("/list")
public Object login(Integer pageNum,Integer pageSize){
    return "成功";
}

 

三、必须使用包装类类型的参数

  因为当参数不存在时,springmvc会将参数的值转换成null,而使用基本类型会出现转换异常。

 

四、注解形式的参数匹配

@RequestMapping("/login")
public Object login(@RequestParam("usernmae") String name,String password){
    return "成功";
}

 

五、可以用defaultValue属性设置上参数的默认值

@RequestMapping("/list")
public Object login(@RequestParam(value = "pageNum",defaultValue = "1") Integer currentPage,
                    @RequestParam(value="pageSize",defaultValue = "10") Integer pageSize){
    return "成功";
}

 

六、设置参数是否可选

  可以使用required属性设置参数是否为可选参数

@RequestMapping("/list")
public Object login(@RequestParam(value = "pageNum",defaultValue = "1",required = true) Integer currentPage,
                    @RequestParam(value="pageSize",defaultValue = "10",required = true) Integer pageSize){
    return "成功";
}

 

七、路径参数

@RequestMapping("/list/{id}")
public Object login(@PathVariable("id") Integer id){
    return "成功";
}

 

八、json数据类型的提交

$.ajax({
    url:'/savePerson',
    type:'post',
    contentType:'application/json;charset=utf-8',
    data:JSON.stringify({})
})
@RequestMapping("/savePerson")
public Object login(@RequestBody Person person){
    return "成功";
}
@RequestMapping("/savePerson")
public Object login(@RequestBody Map<String, Object> map){
    return "成功";
}

 

九、返回json类型的数据

@RequestMapping("/savePerson")
@ResponseBody
public Object login(@RequestBody Person person){
    return "成功";
}

 

十、获取请求头

@RequestMapping("/hello")
public String hello(@RequestHeader("Accept") String headerAccept) {
    System.out.println(headerAccept);
    return "success";
}

 

十一、获取cookie

@RequestMapping("/hello")
public String hello(@CookieValue("JSESSIONID") String cookieJsessionid) {
    System.out.println(cookieJsessionid);
    return "success";
}

 

十二、ModelAttribute注解

  1、@ModelAttribute注解的方法会在@RequestMapping注解的方法之前执行;作用:可以为请求参数赋默认值

  2、两种方式

    1、有返回值

@RequestMapping("/model")
public String model(Address address) {
    System.out.println(address);
    return "success";
}

@ModelAttribute
public Address modelAttribute1(Address address) {
    address.setProvince("河北省");
    address.setCity("沧州市");
    return address;
}

    2、无返回值

@RequestMapping("/model")
public String model(@ModelAttribute("address") Address address) {
    System.out.println(address);
    return "success";
}

@ModelAttribute
public void modelAttribute1(Address address, Map<String, Object> map) {
    address.setProvince("河北省");
    address.setCity("沧州市");
    map.put("address", address);
}

 

十三、SessionAttributes注解

@Controller
@SessionAttributes({"msg"})// model.addAttribute既在request域存一份,session域也会存一份
public class HelloController {
    @RequestMapping("/hello")
    public String hello(Model model) {
        // 底层会存储到request域对象中
        model.addAttribute("msg", "孟美岐");
        return "success";
    }

    @RequestMapping("/getSessionVal")
    public String getSessionVal(ModelMap modelMap) {
        // 从session域获取数据
        String msg = (String) modelMap.get("msg");
        System.out.println(msg);
        return "success";
    }

    @RequestMapping("/delSessionVal")
    public String delSessionVal(SessionStatus sessionStatus) {
        // 清空session域的数据
        sessionStatus.setComplete();
        return "success";
    }
}

 

posted on 2020-04-05 15:53  一路繁花似锦绣前程  阅读(243)  评论(0编辑  收藏  举报