第一种传参方式:Servlet api方式
第一种request域传值方式:Servlet api 方式
@GetMapping("/servletApi")
public String servletApi(HttpServletRequest request) {
// servlet api方式传参
String name = request.getParameter("name");
log.info("param name:{}", name);
// request域传值
request.setAttribute("age", 100);
return "index";
}
第二种传参方式:基本类型
第二种Request域传值方式:ModelAndView的addObject(key,value)
@GetMapping("/basic")
public ModelAndView basicParam(//
@RequestParam(required = false, defaultValue = "10") int age, //
@RequestParam(required = false, defaultValue = "et1812") String name) {
// 打印传入的基本类型参数值
log.info("age:{}, name:{}", age, name);
// 使用ModelAndView传值
ModelAndView mv = new ModelAndView();
mv.addObject("model", "ModelAndView Data!");
mv.setViewName("index");
return mv;
第三种传参方式:使用java bean对象传参
第三种Request传值方式:使用Model的addAttrbute()方法
@GetMapping("/beanParam")
public String beanParam(User user, Model model) {
log.info("param user:{}", user);
// request域传值
User userValue = new User(1, "et1812", "", 18, "2019-04-04");
model.addAttribute("user", userValue);
return "index";
}
第四种Request域传值方式:使用ModelMap
@GetMapping("/modelMap")
public String modelMap(ModelMap modelMap) {
List<User> userList = new ArrayList<>();
modelMap.addAttribute("userList", userList);
User user = new User(10, "etoak", "", 10, "");
User user2 = new User(12, "et1812", "", 18, "");
userList.add(user);
userList.add(user2);
return "index";
}
第四种传参方式:数组方式传参
@PostMapping("/array")
public String array(int[] ids) {
for (int id : ids) {
log.info("id-->{}", id);
}
// 使用页面重定向方式跳转到请求
return "redirect:/user/modelMap";
// return "forward:/user/modelMap";
}
第五种Request域传值方式:Map
@GetMapping("/useMap")
public String useMap(Map<String, Object> map) {
User user = new User(1, "etoak", "", 10, "");
map.put("mapUser", user);
return "index";
}
第五种传参方式:map方式传参
注意:这种方式需要将map定义到一个java bean中
Controller层:
@GetMapping("/mapParam")
public String mapParam(UserVo userVo) {
log.info("userMap:{}", userVo.getUserMap());
return "forward:/user/modelMap";
}
前端jsp页面:
<h4>测试Map传参</h4>
<form action="${pageContext.request.contextPath}/user/mapParam">
用户id:<input type="text" name="userMap['id']" /> <BR/>
用户名称:<input type="text" name="userMap['username']" /> <BR/>
<input type="submit" value="提交" />
</form>
将map定义到一个java bean中
public class UserVo {
// 用于接收前端的userMap参数
private Map<String, Object> userMap;
// 用于接收前端的userList参数
private List<User> userList;
}
第六种传参方式:list方式
* 注意:这种方式需要将list定义到一个java bean中
@PostMapping("/listParam")
public String listParam(UserVo userVo) {
userVo.getUserList().forEach(user -> System.out.println(user));
// post方式提交的,如果转发到GET请求,需要使用重定向
return "redirect:/user/modelMap";
}
含有list的java bean对象
private List<User> userList;
1
第七种传参方式:JSON以及JsonArray传参
@PostMapping("/testJSON")
@ResponseBody
public User testJson(@RequestBody User user) {
log.info("json param : {}", user);
User result = new User();
return result;
}
``
@RestController
@RequestMapping("/rest")
@Slf4j
public class JsonController {
@PostMapping("/test")
public List<User> rest(@RequestBody List<User> user){
for(User u:user){
log.info(u.getUsername());
}
List<User> userList=new ArrayList<>();
User user1=new User(1,"etoak","",10,"");
User user2=new User(2,"etoak2","",18,"");
userList.add(user1);
userList.add(user2);
return userList;
};
}
前端jsp页面:
1
2
json Array传参
测试JSON Array传参
<script src="${pageContext.request.contextPath }/static/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var contextPath = "${pageContext.request.contextPath }";
//
var userJson = {"id": 1, "username": "et1812"};
function testJson(){
$.ajax({
url: contextPath + "/user/testJSON",
type: "POST",
data: JSON.stringify(userJson),
dataType: 'json',
contentType: "application/json;charset=UTF-8",
success: function(data){
console.log("id:" + data.id);
console.log("username:" + data.username);
}
});
}
function testJsonArray(){
var jsonArray = [
{"id": 1, "username": "et1812"},
{"id": 2, "username": "etoak"}
];
$.ajax({
url: contextPath + "/rest/test",
type: "POST",
dataType: "json",
contentType: "application/json;charset=UTF-8",
data: JSON.stringify(jsonArray),
success:function(data) {
$.each(data, function(index, user){
alert("user.id:" + user.id);
});
}
});
}
</script>