码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc的前后端传值

时间:2018-09-21 13:33:56      阅读:145      评论:0      收藏:0      [点我收藏+]

标签: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<String,Object> 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
<form action="springmvc/testPOJO" method="post">
    	username:<input type="text" name="username">
    	<br>
    	pasword:<input type="password" name="password">
    	<br>
    	age:<input type="text" name="age">
    	<br>
    	email:<input type="text" name="email">
    	<br>
    	city:<input type="text" name="address.city">
    	<br>
    	stree:<input type="text" name="address.stree">
    	<br>
    	<input type="submit" value="提交">
    	
    </form> 

 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<String,Object> 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!