Springmvc accepts request parameters

Springmvc accepts request parameters

Ready to work

  • Submit a new form
    • Request address:http://localhost:8080/ProjectName/user/login.do


<form action="<%=request.getContextPath()%>/user/login.do" method="post">
    username:<input type="text" name="username"> <br>
    password:<input type="text" name="password"> <br>
    age:<input type="text" name="age">
    <input type="submit" value="提交">
</form>

[Not recommended] HttpServletRequest

  • Direct use HttpServeletRequestas a method parameter springwill automatically inject it
  • Requires manual conversion of parameter types
@RequestMapping(value="/login.do")
    public String login(HttpServletRequest request,HttpServletResponse response){
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        int age=Integer.parseInt(request.getParameter("age"));  //转换类型
        System.out.println(username+"--->"+password);
        return "success";
    }

Declare the corresponding parameters directly in the method (the attribute of name must be the same as the method parameter)

  • nameThe properties in the form should be the same as the parameters in the method
  • When it comes to type conversion, you can directly declare parameters of different types in the method
  • Pros: Convenience, and can make Springmvc handle data types automatically
@RequestMapping(value = "/login.do")
    public String login(String username,String password,Integer age) {
        System.out.println(username + "--->" + password+"----->"+age);
        return "success";
    }

【Recommendation】@RequestParam

  • Use this annotation to get request parameters, the parameters in the method can be nameinconsistent with the attributes in the form
  • The value obtained using @RequestParamthis must be included in the request parameters, otherwise an error will be reported unless requiredthe property is set tofalse
/**
     * 使用@RequestParam获取请求参数
     * @RequestParam()中的value属性为form表单中对应的name属性
     * 自动转换数据类型,只需要定义方法参数为所需的数据类型即可,spring会为我们自动转换
     */
    @RequestMapping(value = "/login.do")
    public String login(@RequestParam(value = "username") String name,
            @RequestParam("password") String pwd,@RequestParam("age") Integer age) {
        System.out.println(name + "--->" + pwd+"----->"+age);
        return "success";
    }

Attributes

  • valueSpecify the corresponding attribute name in the request, this is like the name attribute defined in the form
  • required
    Specify whether this parameter must be included in the request address, the default is true, that is, if this parameter is not included, an error will be reported.
  • defaultValueSpecify the default value. If it is set required=falseand this value is not set, then the default is null, but this property can also be used to set the default value of the parameter. Of course, for a parameter with an int type, if its value is not specified, an error will be reported, because intthere is no type null, only Integerthe type has it, so it needs to be set to Integer
/*
     * 这里的获取请求参数中的username,age的值
     * 其中username的这个注解默认的是required=true,因此这个是不可以没有的
     * age的这个注解设置了required=false,表示可以没有这个参数,但是如果没有这个参数,那么默认的是null,但是int类型的没有null,因此也会是报错的,有两种解决方式:
     *      1) 通过defalutValue设置其默认的值
     *      2) 如果我们就想要设其为null,可以使用封装类Integer类型即可
     */
    @RequestMapping(value = "/testRequestParams")
    public String testRequestParams(@RequestParam(value="username") String username,@RequestParam(value="age",required=false) int age) {
        System.out.println("username,age  "+ username+"  ,  "+age);
        return SUCCESS;
    }

[Recommended] Use a custom data type (JavaBean)

  • If there are many request parameters passed in, then we use the above method to obtain request parameters, and we need to declare a lot of method parameters. But we can encapsulate the passed request parameters into one JavaBean, then we can directly pass in a JavaBean object to receive all the request parameters.
  • Declare a User class, which must add a setmethod , and the variable name in it must be nameconsistent with the attribute in the form
public class User {
    private String username;
    private String password;
    private int age;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password + "]";
    }

}
  • So our method at this time is UserControlleras loginfollows:
@RequestMapping(value = "/login.do")
    public String login(User user) {
        System.out.println(user);
        return "success";
    }

Summarize

  • All the above methods do not conflict and can be mixed
/**
     * 混合使用案例
    */
    @RequestMapping(value = "/login.do")
    public String login(User user,@RequestParam("gender")String gender) {
        System.out.println(user);
        return "success";
    }
  • It is not recommended to use the HttpServletRequestmethod to obtain, the other two methods are very useful methods, you can choose according to the actual situation

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326391147&siteId=291194637