一.后端传值给前端
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; }
原文地址:https://www.cnblogs.com/QYou/p/9685864.html
时间: 2024-10-13 03:17:27