当前位置: 首页 > news >正文

What is `@Controller` does?

@ControllerSpringMVC注解,标记一个类作为Web控制器(Controller),负责处理HTTP请求并返回响应结果

SpringMVC中,控制器类的主要职责是:
1、接收来自客户端的HTTP请求
2、调用服务层或其他业务逻辑组件
3、根据操作结果准备视图模型数据(Model),将控制权转移给视图解析器(View Resolver)渲染视图
4、操作结果直接返回HTTP响应(例如JSON、XML等)。

使用样例

简单页面渲染

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;@Controller
public class SimplePageController {@GetMapping("/hello")public String displayHelloPage() {// ...return "hello"; // 假设有一个名为"hello.html"的模板文件在视图解析器配置的路径下}
}

结合ModelAndView对象使用

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;@Controller
public class UserViewController {@GetMapping("/users/{id}")public ModelAndView showUser(@PathVariable Long id, Model model) {// 假设userService.find(id)获取用户信息User user = userService.find(id);// 将用户信息添加到模型中供视图模板使用model.addAttribute("user", user);// 返回包含视图名称和模型数据的ModelAndView对象return new ModelAndView("user-profile", model.asMap());}
}

处理POST请求并返回重定向

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;@Controller
@RequestMapping("/login")
public class LoginController {@GetMappingpublic String showLoginForm() {// 显示登录表单页面return "login";}@PostMappingpublic String processLogin(@RequestParam("username") String username,@RequestParam("password") String password,RedirectAttributes redirectAttrs) {// 假设userService.authenticate(username, password)进行身份验证if (userService.authenticate(username, password)) {// 登录成功,重定向到主页redirectAttrs.addFlashAttribute("successMessage", "Welcome, you have successfully logged in!");return "redirect:/home";} else {// 登录失败,将错误信息添加到模型中以便在重定向后的页面显示redirectAttrs.addFlashAttribute("errorMessage", "Invalid credentials. Please try again.");return "redirect:/login";}}
}

结合@RequestBody处理JSON数据

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserController {@PostMapping("/users")public ResponseEntity<User> createUser(@RequestBody CreateUserRequest request) {User user = userService.createUser(request.getName(), request.getEmail());return ResponseEntity.ok(user);}static class CreateUserRequest {private String name;private String email;// getters and setters}
}
http://www.lryc.cn/news/283536.html

相关文章:

  • 新版AndroidStudio dependencyResolutionManagement出错
  • 第三天业务题
  • nestjs 装饰器
  • 一款开源且不限制大小可以设置过期时间的支持分享的的开源文件共享系统picoshare 部署教程
  • eBPF运行时安全
  • Linux 系统中常见的命令,它们用于执行各种任务,包括文件和目录管理、系统信息查看、用户管理等
  • AutoEventWireup详解
  • SAP ABAP 自定义流水号 编号范围
  • 安卓、ios系统详解
  • 含并行连结的网络(GoogLeNet)
  • 计算机网络(第六版)复习提纲3
  • 怿星科技测试实验室获CNAS实验室认可,汽车以太网检测能力达国际标准
  • GORM 介绍及快速入门
  • Scrcpy:掌握你的Android设备
  • [9, 8, 7, 6][1,2] = ?
  • docker部署Jira+配置MySQL8数据库
  • YOLOv5全网独家首发:DCNv4更快收敛、更高速度、更高性能,效果秒杀DCNv3、DCNv2等 ,助力检测实现暴力涨点
  • HTML中常用标签--详解
  • Vue实现字符串首字母大写、翻转字符串、获取用户选定的文本
  • 基于springboot+vue的旅游网站系统(前后端分离)
  • GB/T28181-2022之图像抓拍规范解读和设计实现
  • 阿赵UE学习笔记——10、Blender材质和绘制网格体
  • 数据结构--串
  • RabbitMQ交换机(3)-Topic
  • 前端密钥怎么存储,以及临时存储一些数据,如何存储才最安全?
  • 第16章_网络编程拓展练习(TCP编程,UDP编程)
  • 深入Docker5:安装nginx部署完整项目
  • HBASE学习四:常用命令汇总梳理(包括数据库、zk、hdfs相关操作与配置)
  • Android平台RTSP|RTMP播放端实时快照保存JPG还是PNG?
  • 【人工智能】之深入了解嵌入模型中的 Token:NLP 中的语义之旅(1)