实体类
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class User { private int id; private String name; private int age; }
Controller
import com.peng.pojo.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("yonghu")//相当于分由 public class UserController { @RequestMapping("/user1") public String UserModel(@RequestParam("name") String name ,Model model){ model.addAttribute("user",name); return "yonghu"; } @RequestMapping("/user") public String UserNew(User user,Model model){ model.addAttribute("user",user); return "yonghu"; } }
@RequestMapping写在类上面相当于分由,写在方法上面相当于请求路径
上面例子请求地址相当于http://localhost:8080/yonghu/user