后台接受前台表单传值的方法

1、直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交。

url形式:http://localhost/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111 提交的参数需要和Controller方法中的入参名称一致。

/**

  • 直接把表单的参数写在Controller相应的方法的形参中
  • @param username
  • @param password
    */
@RequestMapping("/addUser1")
public String addUser1(String username,String password) {
    
    
    System.out.println("username is:"+username);
    System.out.println("password is:"+password);
    return "demo/index";
}

2、用注解@RequestParam绑定GET请求参数到方法入参

当请求参数username不存在时会有异常发生,可以通过设置属性required=false解决,例如: @RequestParam(value=“username”, required=false)

/**

  • 用注解@RequestParam绑定请求参数到方法入参
  • @param username
  • @param password
    */
@RequestMapping(value="/addUser6",method=RequestMethod.GET)
public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {
    
    
    System.out.println("username is:"+username);
    System.out.println("password is:"+password);
    return "demo/index";
}

3、创建一个实体类,GET请求、POST请求、接收json数据等等

/**

  • 3、通过一个bean来接收,这个类要写上getter和setter
  • @param user
    */
@RequestMapping("/addUser3")
public String addUser3(@Validated @RequestBody UserModel user) {
    
    
    System.out.println("username is:"+user.getUsername());
    System.out.println("password is:"+user.getPassword());
    return "demo/index";
}

4、通过@PathVariable获取路径中的参数

例如,访问http://localhost/SSMDemo/demo/addUser4/lixiaoxi/111111 路径时,则自动将URL中模板变量{username}和{password}绑定到通过@PathVariable注解的同名参数上,即入参后username=lixiaoxi、password=111111。

/**

  • 通过@PathVariable获取路径中的参数
  • @param username
  • @param password
    */
@RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)
public String addUser4(@PathVariable String username,@PathVariable String password) {
    
    
    System.out.println("username is:"+username);
    System.out.println("password is:"+password);
    return "demo/index";
}

5、通过HttpServletRequest接收,post方式(表单post提交)和get方式都可以,不过如果使用axios,其自动转为json数据

/**

  • 通过HttpServletRequest接收
  • @param request
    */
@RequestMapping("/addUser2")
public String addUser2(HttpServletRequest request) {
    
    
    String username=request.getParameter("username");
    String password=request.getParameter("password");
    System.out.println("username is:"+username);
    System.out.println("password is:"+password);
    return "demo/index";
}

6、使用@ModelAttribute注解获取POST请求的FORM表单数据,如果是axios发送的post则是json形式,用这种方法获取不到

<form action ="<%=request.getContextPath()%>/demo/addUser5" method="post"> 
     用户名: <input type="text" name="username"/><br/>
     密  码: <input type="password" name="password"/><br/>
     <input type="submit" value="提交"/> 
     <input type="reset" value="重置"/> 
</form>

/**

  • 使用@ModelAttribute注解获取POST请求的FORM表单数据
  • @param user
    */
@RequestMapping(value="/addUser5",method=RequestMethod.POST)
public String addUser5(@ModelAttribute("user") UserModel user) {
    
    
    System.out.println("username is:"+user.getUsername());
    System.out.println("password is:"+user.getPassword());
    return "demo/index";
}

猜你喜欢

转载自blog.csdn.net/qq_54537215/article/details/125368154
今日推荐