Spring MVC是基于Spring框架的Web MVC框架。
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class GreetingController {
@GetMapping("/greeting")
public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name,
Model model) {
model.addAttribute("name", name);
return "greeting";
}
@GetMapping("/users")
public String listUsers(Model model) {
// 从数据库获取用户列表
List users = userService.findAll();
model.addAttribute("users", users);
return "users";
}
}
提示: 这是一个重要的概念,需要特别注意理解和掌握。
注意: 这是一个常见的错误点,请避免犯同样的错误。