SpringMVC是一种基于Java的MVC(Model-View-Controller)Web开发框架,通过将业务逻辑、数据和界面分离,使得开发人员能够更高效地管理和维护代码,提高应用的可扩展性和可维护性。
SpringMVC核心概念
以下是一些常用的Spring MVC注解:
@Controller
public class UserController {
// controller methods and logic here
}
@Controller
@RequestMapping("/users")
public class UserController {
@RequestMapping("/list")
public String listUsers(Model model) {
// handle logic for user list
return "users/list";
}
}
@Controller
public class UserController {
@GetMapping("/users")
public String listUsers(Model model) {
// handle logic for GET request
return "users/list";
}
}
@Controller
@RequestMapping("/users/{userId}")
public class UserController {
@GetMapping
public String getUser(@PathVariable("userId") Long userId, Model model) {
// handle logic for specific user
return "users/detail";
}
}
@Controller
@RequestMapping("/users")
public class UserController {
@PostMapping("/update")
public String updateUser(@RequestParam("name") String name, @RequestParam("email") String email, Model model) {
// handle logic for updating user information
return "users/update";
}
}
@Controller
@RequestMapping("/users")
public class UserController {
@PostMapping("/create")
public String createUser(@RequestBody User user, Model model) {
// handle logic for creating a new user
return "users/create";
}
}
@Controller
public class UserController {
@GetMapping("/users/{userId}")
@ResponseBody
public User getUser(@PathVariable("userId") Long userId) {
// handle logic for specific user
return user;
}
}
@Controller
public class UserController {
@ModelAttribute("user")
public User createUser(@RequestParam String name, @RequestParam String email) {
// create user object and set properties
User user = new User();
user.setName(name);
user.setEmail(email);
return user;
}
}
@Controller
public class UserController {
@GetMapping("/login")
public String login(Model model) {
// handle login logic and set user data in session
HttpSession session = request.getSession();
session.setAttribute("user", userData);
return "login";
}
}
@Controller
public class UserController {
@GetMapping("/users")
public String getUsers(@RequestHeader("Authorization") String authorizationHeader, Model model) {
// handle logic for getting users based on authorization header
return "users/list";
}
}
@Controller
public class UserController {
@GetMapping("/logout")
public String logout(@CookieValue("USER_TOKEN") String token) {
// handle logic for logout based on user token cookie
return "logout";
}
}
@Controller
public class UserController {
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
}
@Controller
public class UserController {
@ExceptionHandler(UserNotFoundException.class)
public String handleUserNotFoundException(UserNotFoundException e, Model model) {
// handle logic for handling user not found exception and set error message in model
model.addAttribute("errorMessage", "User not found.");
return "error";
}
}
@Controller
public class UserController {
@Autowired
private UserDao userDao;
// or
@Resource(name = "userDao")
private UserDao userDao;
// or
@Qualifier("userDao")
private UserDao userDao;
}