提交的域名称和处理方法的参数名一致
@Controller
@RequestMapping("user")
public class UserController {
@GetMapping("test1")
public String test1(String name, Model model){
System.out.println(name);
model.addAttribute("name", name);
return "user";
}
}
提交数据 http://localhost:8080/user/test1?name=starry
输出结果 starry
提交的域名称和处理方法的参数名不一致
@GetMapping("test2")
public String test2(@RequestParam("username") String name, Model model){
System.out.println(name);
model.addAttribute("name", name);
return "user";
}
@RequestParam("value")
value:表单提交数据的名称
提交数据 http://localhost:8080/user/test2?username=starry
输出结果 starry
提交的是一个对象
要求提交的表单域和对象的属性名一致 , 参数使用对象即可
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private int age;
}
@RequestMapping("test3")
public String test3(User user, Model model){
System.out.println(user);
model.addAttribute("name", user.getName());
return "user";
}
提交数据 http://localhost:8080/user/test3?name=starry&id=1&age=20
输出结果 starry