侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 748 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

SpringMVC(3)之默认支持的入参与参数绑定

zze
zze
2017-12-05 / 0 评论 / 0 点赞 / 638 阅读 / 5256 字

默认支持的入参

package com.zze.springmvc.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class TestController {
    @RequestMapping("test")
    public String test(Model model, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response){
//        ModelAndView modelAndView = new ModelAndView();
//        此时的入参 model 及 modelMap,就对应于 modelAndView 中的 model 部分,而 String 返回值,就相当于 modelAndView 中的 view 部分
//          modelAndView.addObject() 对应 model.addAttribute()、 modelMap.addAttribute()
//          modelAndView.setViewName("list") 对应 return "list"
//        默认还支持直接传入 HttpServletRequest、HttpServletResponse 实例,这样就可以直接访问到原生 Servlet 相关 API
        return null;
    }
}

参数绑定

简单参数绑定

import com.zze.springmvc.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    /**
     * 简单参数绑定
     *  请求参数名必须和请求方法入参名一致
     */
    @RequestMapping("test")
    public void test(Integer id,String name) {
        System.out.println(name);
        System.out.println(id);
        /*
        zhangsan
        123
         */
    }
}

image.png

POJO 参数绑定

import com.zze.springmvc.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    /**
     * pojo 参数绑定
     *  请求参数名必须和 pojo 属性名一致
     */
    @RequestMapping("test")
    public void test(User user) {
        System.out.println(user.getName());
        System.out.println(user.getId());
        /*
        zhangsan
        123
         */
    }
}

image.png

包装 POJO 参数绑定

import com.zze.springmvc.pojo.QueryVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    /**
     * 包装 pojo 参数绑定
     */
    @RequestMapping("test")
    public void test(QueryVo queryVo) {
        System.out.println(queryVo.getUser().getName());
        System.out.println(queryVo.getUser().getId());
        /*
        zhangsan
        123
         */
    }
}

image.png

数组参数绑定

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;

@Controller
public class TestController {
    /**
     * 数组参数绑定
     */
    @RequestMapping("test")
    public void test(Integer[] ids) {
        System.out.println(Arrays.toString(ids));
        /*
        [1, 2]
         */
    }
}

image.png

集合参数绑定

import java.util.List;

public class QueryVo {

    private List<User> users;

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }
}
package com.zze.springmvc.web.controller;

import com.zze.springmvc.pojo.QueryVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;

@Controller
public class TestController {
    /**
     * 集合参数绑定
     */
    @RequestMapping("test")
    public void test(QueryVo queryVo) {
        System.out.println(Arrays.toString(queryVo.getUsers().toArray()));
        /*
        [User{id=1, name='zhangsan'}, User{id=2, name='lisi'}]
         */
    }
}

image.png

Restful 风格参数绑定

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.ResponseBody;

@Controller
public class TestController {

    @RequestMapping("user/{id}")
    @ResponseBody
    public void getUser(@PathVariable Integer id){
        System.out.println(id);
        /*
        请求 localhost:8080/user/1
        控制台输出: 1
         */
    }
}
0

评论区