Parameters of processor method in SpringMVC

Table of Contents

1. Directly use the parameters of the method to receive them one by one

2. Use objects to receive multiple parameters

3. The request parameters and method name parameters are inconsistent

4. Use the HttpServletRequest object to obtain parameters

5. Directly use the URL address to pass parameters

6. Get date type parameters

7. Get array type parameters

8. Get parameters of collection type


1.Directly use the method parameters to receive one by one

@Controller
@RequestMapping("param")
public class ParamController {
    @RequestMapping("test01")
    public ModelAndView test01(Integer teamId,String teamName,String
        teamLocation){
     System.out.println("test01-----------------");
     System.out.println(teamId);
     System.out.println(teamName);
     System.out.println(teamLocation);
     return new ModelAndView("ok");
    }
}
<form action="/param/test01" method="post">
Team id:<input type="text" name="teamId"/><br/>
Team name: <input type="text" name="teamName"/><br/>
Team location:<input type="text" name="teamLocation"/><br/>
<button type="submit">Submit</button>
</form>

Directly use the parameters of the method to receive them one by one
:
The parameter name of the method must be consistent with the parameter name carried in the user request, otherwise

Can’t get it

Benefit: No type conversion required

2.Use objects to receive multiple parameters

@RequestMapping("test02")
public ModelAndView test02(Team team){
System.out.println("test02-----------------");
System.out.println(team);
return new ModelAndView("ok");
}
public class Team {
    private Integer teamId;
    private String teamName;
    private String location;
    //Omit getters and setters
    public String toString() {
        return "Team{" +
                "teamId=" + teamId +
                ", teamName='" + teamName + ''' +
                ", location='" + location'}';
    }
}
<form action="/param/test02" method="post">
Team id:<input type="text" name="teamId"/><br/>
Team name: <input type="text" name="teamName"/><br/>
Team location: <input type="text" name="location"/><br/>
<button type="submit">Submit</button>
</form>

Using an object to receive multiple parameters: Requirements
The parameter names carried in the user request must be consistent with the attributes in the entity class
, otherwise

Can’t get

3.The parameters of the request parameter and the method name are inconsistent

@RequestMapping("test03")
public ModelAndView test03(@RequestParam(value = "id",required =
false,defaultValue = "1") Integer teamId,
@RequestParam(value = "name") String teamName,
@RequestParam(value = "location") String
teamLocation){
System.out.println("test03--------------");
System.out.println(teamId);
System.out.println(teamName);
System.out.println(teamLocation);
return new ModelAndView("OK");
}
<form action="/param/test03" method="post">
Team ID:<input name="id" /><br/>
Team name: <input name="name" /><br/>
Team location: <input name="location" /><br/>
<button type="submit">Submit</button>
</form>

Request parameters and method name parameters are inconsistent
:
Use @RequestParam for correction

The value attribute represents the parameter name in the request

required
The attribute indicates whether the parameter is required:
true: a value must be assigned, otherwise a 400 error will be reported; false: no value can be assigned,

The result is null

defaultValue=
Custom default value If no value is obtained from the user request, use the default value

4.UseHttpServletRequest object to obtain parameters

@RequestMapping("test04")
public ModelAndView test04(HttpServletRequest request){
System.out.println("test04-----------------");
String teamId = request.getParameter("teamId");
String teamName = request.getParameter("teamName");
String location = request.getParameter("location");
if(teamId!=null)
System.out.println(Integer.valueOf(teamId));
System.out.println(teamName);
System.out.println(location);
return new ModelAndView("ok");
}
<form action="/param/test04" method="post">
        Team ID: <input name="teamId" /><br/>
        Team name: <input name="teamName" /><br/>
        Team location: <input name="location" /><br/>
        <button type="submit">Submit</button>
    </form>

Use the HttpServletRequest object to obtain parameters: the same method used in the original javaWeb project

5.Directly useURLAddress to pass parameters

@RequestMapping("test05/{id}/{name}/{loc}")
public ModelAndView test05(@PathVariable("id") Integer teamId,
@PathVariable("name") String teamName,
@PathVariable("loc") String teamLocation){
System.out.println("test05-----------------");
System.out.println(teamId);
System.out.println(teamName);
System.out.println(teamLocation);
return new ModelAndView("ok");
}
 <form action="/param/test04" method="post">
        Team ID: <input name="teamId" /><br/>
        Team name: <input name="teamName" /><br/>
        Team location: <input name="location" /><br/>
        <button type="submit">Submit</button>
    </form>

Directly use the URL address to pass parameters: Use the @PathVariable annotation

Accept in order, {id} receives teamId, {name} receives teamName, {loc} receives location

For example, enter directly in the address bar: http://localhost:8080/param/test05/1001/lacker/las

6.Get date type parameters

@RequestMapping("test06")
public ModelAndView test06(Team team){
System.out.println("test06-----------------");
System.out.println(team);
return new ModelAndView("ok");
}
public class Team {
    private Integer teamId;
    private String teamName;
    private String location;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createDate;
    //Omit setters and getters
    public String toString() {
        return "Team{" +
                "teamId=" + teamId +
                ", teamName='" + teamName + ''' +
                ", location='" + location + ''' +
                ", createDate=" + createDate +
                '}';
    }
}
<form action="/param/test06" method="post">
        Team ID: <input name="teamId" /><br/>
        Team name: <input name="teamName" /><br/>
        Team location: <input name="location" /><br/>
        Date:<input name="createDate" /><br/>
        <button type="submit">Submit</button>
    </form>

Define the format to receive dates by annotating DateTimeFormat

7.Get parameters of array type

@RequestMapping("test07")
public ModelAndView test07(String[] teamName,HttpServletRequest request){
System.out.println("test07-----------------");
//Method 1: Directly use the parameter acceptance of the method
for (String s : teamName) {
System.out.println(s);
}
System.out.println("---------------");
//Method 2: Get parameters through HttpServletRequest object
String[] teamNames = request.getParameterValues("teamName");
for (String name : teamNames) {
System.out.println(name);
}
return new ModelAndView("ok");
}
<form action="/param/test07" method="post">
Team name 1: <input type="text" name="teamName"/><br/>
Team name 2: <input type="text" name="teamName"/><br/>
Team name 3: <input type="text" name="teamName"/><br/>
<button type="submit">Submit</button>
</form>

There are many ways to get the array, decide according to your own needs

8.Get parameters of collection type

@RequestMapping("test08")
public ModelAndView test08(@RequestParam("teamName") List<String> nameList){
System.out.println("test08-----------------");
for (String s : nameList) {
System.out.println(s);
}
return new ModelAndView("ok");
}

@RequestMapping("test09")
public ModelAndView test09(QueryVO vo){
System.out.println("test09-----------------");
for (Team team : vo.getTeamList()) {
System.out.println(team);
}
return new ModelAndView("ok");
}
public class QueryVO {
    private List<Team> teamList;

    public List<Team> getTeamList() {
        return teamList;
    }

    public void setTeamList(List<Team> teamList) {
        this.teamList = teamList;
    }
}
<form action="/param/test08" method="post">
Team name 1: <input type="text" name="teamName"/><br/>
Team name 2: <input type="text" name="teamName"/><br/>
Team name 3: <input type="text" name="teamName"/><br/>
<button type="submit">Submit</button>
</form>

<form action="/param/test09" method="post">
Team id1:<input type="text" name="teamList[0].teamId"/><br/>
Team id2:<input type="text" name="teamList[1].teamId"/><br/>
Team id3:<input type="text" name="teamList[2].teamId"/><br/>
Team name 1: <input type="text" name="teamList[0].teamName"/><br/>
Team name 2: <input type="text" name="teamList[1].teamName"/><br/>
Team name 3: <input type="text" name="teamList[2].teamName"/><br/>
<button type="submit">Submit</button>
</form>

Simple types of parameters can be obtained directly using the annotation @RequestParam, but collections of objects are not supported. The collection of objects must be encapsulated in a class and operated as an attribute.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 138,184 people are learning the system