在Spring MVC中,Controller
是一个核心组件,负责处理HTTP请求并返回相应的响应。Controller
通常用于接收用户输入、调用业务逻辑、准备模型数据,并决定渲染哪个视图。
Controller的作用
- 处理请求:接收并处理客户端发起的HTTP请求。
- 调用业务逻辑:调用服务层或业务逻辑层处理请求。
- 准备模型数据:将处理结果封装到模型数据中,供视图使用。
- 返回响应:决定返回的视图或直接返回数据(如JSON、XML)。
如何定义一个Controller
在Spring MVC中,定义一个Controller
非常简单,通常使用@Controller
注解来标记一个类为控制器。以下是定义Controller
的详细步骤:
1. 使用 @Controller
注解
@Controller
注解用于标记一个类为Spring MVC的控制器。Spring会自动扫描并注册这些控制器。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class MyController {
@GetMapping("/hello")
public String sayHello() {
return "hello"; // 返回视图名称
}
}
2. 使用 @RequestMapping
及其变体
@RequestMapping
注解用于将HTTP请求映射到控制器的处理方法。Spring还提供了更具体的注解,如@GetMapping
、@PostMapping
、@PutMapping
、@DeleteMapping
等。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserController {
@GetMapping("/profile")
public String userProfile() {
return "profile"; // 返回视图名称
}
@PostMapping("/update")
public String updateUser() {
return "update"; // 返回视图名称
}
}
3. 处理请求参数
控制器方法可以接收请求参数,并将其绑定到方法参数中。常用的注解包括@RequestParam
、@PathVariable
、@ModelAttribute
等。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class UserController {
@GetMapping("/user/{id}")
public String getUserById(@PathVariable Long id, Model model) {
model.addAttribute("userId", id);
return "userDetails";
}
@GetMapping("/search")
public String searchUser(@RequestParam String query, Model model) {
model.addAttribute("searchQuery", query);
return "searchResults";
}
}
4. 返回模型和视图
控制器方法可以通过Model
对象将数据传递给视图,并返回视图名称。
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@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"; // 返回视图名称
}
}
5. 返回JSON或XML数据
如果控制器方法需要返回JSON或XML数据,可以使用@RestController
注解或@ResponseBody
注解。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ApiController {
@GetMapping("/api/data")
@ResponseBody
public String getData() {
return "{\"message\": \"Hello, World!\"}";
}
}
或者使用@RestController
注解,它是@Controller
和@ResponseBody
的组合。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
@GetMapping("/api/data")
public String getData() {
return "{\"message\": \"Hello, World!\"}";
}
}
总结
- Controller的作用:处理HTTP请求、调用业务逻辑、准备模型数据、返回响应。
- 定义Controller:使用
@Controller
注解标记类,使用@RequestMapping
及其变体映射请求,处理请求参数,返回模型和视图或直接返回数据。
在面试中,理解Controller
的作用及其定义方式是关键。
THE END
暂无评论内容