视图层由服务端渲染
视图层由用户端渲染
package xin.links.framework.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* ClassName: SpringFramework
* Package: xin.links.framework.springmvc.contriller
* Description:
*
* @author Jane
* @version 1.0
* @since 2024/9/3 10:12
*/
//@RequestMapping的路径通配符
// ?:匹配一个字符
// *:匹配任意字符
// **:匹配任意字符,包括路径中的“/”
// 精确匹配
//
// 各级优先级
// 精确匹配 > ? > * > **
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")// @RequestMapping的value属性指定请求路径
public String hello() {
return "hello页面";
}
}
@Controller
和@ResponseBody
注解的组合,意味着这个类的所有方法返回值都会直接用作响应体。
@RequestMapping取出参数值
@RequestMapping
)@RequestMapping
注解支持丰富的映射规则,可以对路径、请求方法、参数、请求头、数据类型等进行细粒度控制。
@Controller
public class BasicMappingController {
// 简单路径映射
@RequestMapping("/hello")
public String helloWorld() {
return "hello";
}
// 使用单个通配符
@RequestMapping("/users/*/profile")
public String userProfile() {
return "userProfile";
}
// 使用多级通配符
@RequestMapping("/documents/**")
public String handleDocuments() {
return "documents";
}
}
*
匹配单层路径,**
匹配多层路径。
@RequestMapping(value = "/submit", method = RequestMethod.POST)
public String handleSubmit() {
return "submitSuccess";
}
支持多种请求方法:GET、POST、PUT、DELETE、HEAD、OPTIONS、PATCH 等。
请求参数限定:
@RequestMapping(value = "/search", params = "type=advanced")
public String handleAdvancedSearch() {
return "advancedSearch";
}
请求头限定:
@RequestMapping(value = "/upload", headers = "Content-Type=multipart/form-data")
public String handleFileUpload() {
return "uploadSuccess";
}
consumes
)和响应数据类型(produces
)请求数据类型:
@RequestMapping(value = "/json", consumes = "application/json")
public String handleJsonRequest(@RequestBody MyObject obj) {
return "jsonProcessed";
}
响应数据类型:
@RequestMapping(value = "/getJson", produces = "application/json")
@ResponseBody
public MyObject getJsonResponse() {
return new MyObject();
}
@RequestParam
和 POJO普通变量收集请求参数:
@RequestMapping("/greet")
public String greetUser(@RequestParam("name") String name) {
return "Hello, " + name;
}
@RequestParam
明确指定参数:@RequestMapping("/show")
public String showUserInfo(@RequestParam(name = "id", required = true) int userId) {
return "User ID: " + userId;
}
public class User {
private String name;
private int age;
// getters and setters
}
@RequestMapping("/register")
public String registerUser(User user) {
return "User registered: " + user.getName();
}
获取请求头:
@RequestMapping("/info")
public String getInfo(@RequestHeader("User-Agent") String userAgent) {
return "User-Agent: " + userAgent;
}
获取 Cookie 值:
@RequestMapping("/cookie")
public String getCookieValue(@CookieValue("JSESSIONID") String sessionId) {
return "Session ID: " + sessionId;
}
JSON 字符串和文件上传处理
接受 JSON 字符串并转换为对象:
@RequestMapping(value = "/addUser", consumes = "application/json")
public String addUser(@RequestBody User user) {
return "User added: " + user.getName();
}
文件上传:
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleFileUpload(@RequestParam("file") MultipartFile file) {
return "File uploaded: " + file.getOriginalFilename();
}
获取完整请求对象(HttpEntity
):
@RequestMapping("/fullRequest")
public String getFullRequest(HttpEntity<String> httpEntity) {
return "Request Body: " + httpEntity.getBody();
}
使用原生 Servlet API 对象:
@RequestMapping("/nativeApi")
public String handleNative(HttpServletRequest request) {
return "Request URL: " + request.getRequestURL();
}
@RequestMapping(value = "/getJsonData", produces = "application/json")
@ResponseBody
public User getJsonData() {
return new User("Alice", 30);
}
@RequestMapping("/download")
public ResponseEntity<Resource> downloadFile() {
// 实现文件下载功能
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=file.txt")
.body(resource);
}
通过 Thymeleaf 模板引擎将数据渲染到 HTML 页面中:
<p th:text="'Hello, ' + ${name}">Hello, user!</p>
© 著作权归作者所有
本文由 趣代码Blog 创作,采用 知识共享署名4.0 国际许可协议进行许可,本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。