接收请求参数及数据回显
@RequestParam() 如果传的参数不一样使用,最好都加上
1,接收前端用户传递的参数,判断参数的名字,假设名字直接在方法上,可以直接使用
2.假设传递的是一个对象User,匹配User对象中的字段名:如果名字一效则OK。否则,匹配不到
代码理解
package com.kuang.controller;
import com.kuang.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/user")
public class UserController {
@GetMapping("/t1")
public String test(@RequestParam("username") String name, Model model){
System.out.println("name:"+name);
model.addAttribute("msg",name);
return "test";
}
@GetMapping("/t2")
public String test2(User user){
System.out.println(user);
return "test";
}
}