springmvc前台向后台传值几种方式从简单到
发布网友
发布时间:2022-04-23 00:46
我来回答
共1个回答
热心网友
时间:2022-04-23 02:15
通过HttpServletRequest
public String toIndex(HttpServletRequest request){
//获得前台name为username的元素的值
System.out.println(request.getParameter("username"));return "index";
}
通过参数名获得
@RequestMapping(value="/index")public String toIndex(String username){ //这里名字要与前端元素名字一致才能获得System.out.println(username);return "index";}
通过@RequestParam注解获得
@RequestMapping(value="/index")public String toIndex(@RequestParam(value="username",required=false)String username111){ //username要与前端一致,在此处可以理解为参数username111的别名System.out.println(username111);return "index";}
SpringMvc还能通过将vo作为参数获得vo的各个属性@RequestMapping(value="/index")public String toIndex(User user){System.out.println(user.getUserName());System.out.println(user.getPassword());return "index";