springmvc的前后端传值

2021-06-21 05:03

阅读:456

标签:tree   arrays   test   name   type   val   use   str   ping   

一.后端传值给前端

1.ModelAndView

@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
	String viewName=SUCCESS;
	ModelAndView model = new ModelAndView(viewName);
	model.addObject("time", new Date());
	return model;
		
}

  

2.map

@RequestMapping("/testMap")
public String testMap(Map map){		
	map.put("names", Arrays.asList("Tom","maray","hery"));
	return SUCCESS;
}

  

3.ModelMap

@RequestMapping("/testModelMap")
public String testModleMap(ModelMap map){
	map.addAttribute("modelMapDate", "modelMapDate");
	return SUCCESS;
		
}

  

4.model

@RequestMapping("/testModel")
public String testModel(Model model){
	model.addAttribute("modelDate", "modelDate");
	return SUCCESS;		
}

5.使用servlet Api

二.前端向后台传值

1.pojo的值

后端java
@RequestMapping("/testPOJO")
public String testPOJO(User user){		
	System.out.println(user);
	return SUCCESS;
		
}
前端jsp
username:
pasword:
age:
email:
city:
stree:

 2.利用@RequestParam注解参数

@RequestMapping("/testResquestParam")
public String testResquestParam(@RequestParam(value="username") String username,
	@RequestParam(value="age",required=false,defaultValue="14") int age,
	@RequestParam String email,
     double price){
  System.out.println("testResquestParam:"+username+" "+age+" "+email+" "+price);
  return SUCCESS;
}

 3.利用

@ModelAttribute,为对象传值
@RequestMapping("/testModelAttribut")
public String testModelAttribut(@ModelAttribute User user,Map map){
	System.out.println("model"+user);
	System.out.println("map"+map.get("user"));
	return SUCCESS;	
}

  

 

springmvc的前后端传值

标签:tree   arrays   test   name   type   val   use   str   ping   

原文地址:https://www.cnblogs.com/QYou/p/9685864.html


评论


亲,登录后才可以留言!