默认支持的入参
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
*/
}
}
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
*/
}
}
包装 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
*/
}
}
数组参数绑定
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]
*/
}
}
集合参数绑定
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'}]
*/
}
}
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
*/
}
}
评论区