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

建站之星破解版手机每日新闻播报

建站之星破解版手机,每日新闻播报,自己做视频网站能赚钱吗,德阳机械加工网目录 章节简介 一 请求处理(初级) eg:请求头 二 请求处理(进阶) eg:请求体 三 获取请求头 四 获取Cookie 五 级联封装 六 使用RequestBoby封装JSON对象 七 文件的上传 八 获取整个请求 HttpEntity 九 原生请求 Spring…

目录

章节简介

一 请求处理(初级)

eg:请求头

二 请求处理(进阶)

eg:请求体

三 获取请求头

四 获取Cookie

五 级联封装

六 使用@RequestBoby封装JSON对象

七 文件的上传

八 获取整个请求 HttpEntity

九 原生请求


Spring MVC 是 Spring 框架中的一个核心模块,专门用于构建 灵活、松耦合的 Web 应用程序。它基于经典的 MVC(Model-View-Controller)设计模式,但通过 Spring 的依赖注入(DI)和面向切面(AOP)等特性,简化了开发流程,提高了代码的可维护性。

与传统 Servlet/JSP 对比

特性Spring MVC传统 Servlet/JSP
耦合性低(基于接口和注解)高(直接依赖 Servlet API)
配置注解驱动,简化 XML/Java 配置需手动配置 web.xml
测试易于单元测试(Mock 框架支持)测试复杂(依赖容器)
扩展性模块化设计,易于扩展扩展需手动管理
功能集成无缝集成 Spring 生态(如安全、事务)需自行整合其他库

复习巩固前言JSON

图示:

章节简介

一 请求处理(初级)

eg:请求头

1 处理请求参数

前端代码:

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><title>信息提交表单</title>
</head>
<body>
<h3>信息采集</h3>
<!-- 指定提交地址为 /handle02  表单数据会附加在 URL 参数中-->
<form action="/handle02" method="get"><fieldset><legend>主要信息</legend><label for="account1">账户:</label><input type="text" id="account1" name="account" value="" maxlength="10"><br><label>密码:<input type="password" id="password1" name="pw" maxlength="9"></label><br><label>性别:<input type="radio" name="gender" id="gender1" value="male" checked> 男<input type="radio" name="gender" value="female"> 女</label><br><label>爱好:<input type="text" id="hobby" name="hobby"><input type="checkbox" name="hobby" value="smoke"> 抽烟<input type="checkbox" name="hobby" value="drink"> 喝酒<input type="checkbox" name="hobby" value="perm"> 烫头</label><br></fieldset>其他补充信息:<br><textarea name="other" cols="25" rows="2"></textarea><br>籍贯:<select name="place"><option value="鲁">山东省</option><option value="皖">安徽省</option><option value="粤">广东省</option><option value="京" selected>北京市</option><option value="苏">江苏省</option></select><br><input type="hidden" name="hid" value="hahahahahahaha"><input type="submit" value="确认"><input type="reset" value="重置">
</form>
</body>
</html>

请求方法类:

package org.example.spring04mvc.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class RequestTestController {@RequestMapping("/handle01")public String handleRequest(@RequestParam String account,@RequestParam String pw,@RequestParam String gender,@RequestParam List<String> hobby,@RequestParam(required = false) String other,@RequestParam String place,@RequestParam String hid) {System.out.println("Account: " + account);System.out.println("Password: " + pw);System.out.println("Gender: " + gender);System.out.println("Hobbies: " + hobby);System.out.println("Place: " + place); // 自动处理URL编码(如"京")if (other != null) {System.out.println("Other: " + other);}System.out.println("隐藏信息: " + hid);return "表单上传成功!";}}

Sprig项目启动页面展示

个人控制台与浏览器展示:

二 请求处理(进阶)

eg:请求体

前端代码:

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><title>信息提交表单</title>
</head>
<body>
<h3>信息采集</h3>
<!-- 指定提交地址为 /handle02 表单数据会通过 HTTP 请求体(Body) 发送到服务器。 -->
<form action="/handle02" method="post"><fieldset><legend>主要信息</legend><label for="account1">账户:</label><input type="text" id="account1" name="account" value="" maxlength="10"><br><label>密码:<input type="password" id="password1" name="pw" maxlength="9"></label><br><label>性别:<input type="radio" name="gender" id="gender1" value="male" checked> 男<input type="radio" name="gender" value="female"> 女</label><br><label>爱好:<input type="text" id="hobby" name="hobby"><input type="checkbox" name="hobby" value="smoke"> 抽烟<input type="checkbox" name="hobby" value="drink"> 喝酒<input type="checkbox" name="hobby" value="perm"> 烫头</label><br></fieldset>其他补充信息:<br><textarea name="other" cols="25" rows="2"></textarea><br>籍贯:<select name="place"><option value="鲁">山东省</option><option value="皖">安徽省</option><option value="粤">广东省</option><option value="京" selected>北京市</option><option value="苏">江苏省</option></select><br><input type="hidden" name="hid" value="hahahahahahaha"><input type="submit" value="确认"><input type="reset" value="重置">
</form>
</body>
</html>

封装:(可能由于版本问题@Data使用出错)

package org.example.spring04mvc.bean;import lombok.Data;@Data
public class emailUsers {// 账号指定默认值private String account="zhangsan";private String pw;private String gender;private String hid;private String place;private String other;private String[] hobby;}

指定:

package org.example.spring04mvc.controller;import org.example.spring04mvc.bean.emailUsers;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
public class RequestTestController {@RequestMapping("/handle01")public String handleRequest(@RequestParam String account,@RequestParam String pw,@RequestParam String gender,@RequestParam List<String> hobby,@RequestParam(required = false) String other,@RequestParam String place,@RequestParam String hid) {System.out.println("Account: " + account);System.out.println("Password: " + pw);System.out.println("Gender: " + gender);System.out.println("Hobbies: " + hobby);System.out.println("Place: " + place); // 自动处理URL编码(如"京")if (other != null) {System.out.println("Other: " + other);}System.out.println("隐藏信息: " + hid);return "表单上传成功!";}@RequestMapping("/handle02")public String handleRequest02(emailUsers emailUser){System.out.println(emailUser);return "表单上传成功!";}}

运行结果:

三 获取请求头

    @RequestMapping("/handle03")public String handleRequest03(@RequestHeader(value = "Host",defaultValue = "localhost") String host) {System.out.println(host);return "Ok" + host;}

四 获取Cookie

    @RequestMapping("/handle04")public String handleRequest04(@CookieValue(value = "JSESSIONID",defaultValue = "") String sessionId) {System.out.println(sessionId);return "Ok" + sessionId;}

五 级联封装

六 使用@RequestBoby封装JSON对象

代码实现:

    //测试接收JSON数据,获取请求体json的数据,自动转为对象@RequestMapping("/handle06")public String handleRequest06(@RequestBody emailUsers emailUser) {System.out.println(emailUser);return "OK";}
    //将JSON对象以字符串接收@RequestMapping("/handle07")public String handleRequest07(@RequestBody String json) {System.out.println(json);return "OK";}

七 文件的上传

    //接收文件上传表单@RequestMapping("/handle08")public String handleRequest08(emailUsers emailUser,@RequestParam("file") MultipartFile f1,@RequestParam("file2") MultipartFile[] f2) throws IOException {// 1获取原始文件名String originalFilename = f1.getOriginalFilename();// 2获取文件大小long size = f1.getSize();// 3获取文件流InputStream inputStream = f1.getInputStream();// 4文件保存f1.transferTo(new File("D:\\" + originalFilename));System.out.println(emailUser);return "OK";}

更改部分参数

八 获取整个请求 HttpEntity

HttpEntity 是 Spring Framework 中用于表示 HTTP 请求或响应实体 的核心类,它封装了 HTTP 消息的 头部(Headers) 和 正文(Body),常用于客户端和服务端之间的数据交互。以下是其核心特性和用法:


1. 核心功能

  • 封装数据:包含 HTTP 消息的 body(如 JSON、XML、表单数据)和 headers(如 Content-TypeAuthorization)。

  • 支持泛型HttpEntity<T> 允许指定正文内容的类型(如 Stringbyte[]Map 或自定义对象)。

  • 不可变性:创建后不可修改,若需变更需通过 HttpEntity 的构建方法生成新实例。

    // HttpEntity 封装请求头请求体,把整个请求拿过来封装@RequestMapping("/handle09")public String handleRequest09(HttpEntity<String> httpEntity){//拿到请求头HttpHeaders headers = httpEntity.getHeaders();//拿到请求体String body = httpEntity.getBody();return "OK";}

九 原生请求

    //使用原生API@RequestMapping("/handle10")public String handleRequest10(HttpServletRequest request) throws IOException {//拿到请求头Enumeration<String> headerNames = request.getHeaderNames();//拿到请求体String body = request.getReader().readLine();return "OK";}

http://www.dtcms.com/wzjs/196092.html

相关文章:

  • 涟源市建设局网站秦皇岛网站seo
  • wordpress制作自定义页面的方法seo关键词优化平台
  • 政府门户网站建设技术方案网络平台有哪些?
  • 网站信息建设网络广告推广服务
  • 网络编程是做什么的seo专员岗位职责
  • 保定建网站公司手机上如何制作自己的网站
  • 语言互动网站建设淘宝数据分析工具
  • 许昌做网站哪家好2024年最新时事新闻
  • wordpress上一篇成都官网seo费用
  • 公司网站做排名用什么搜索好点win10最强性能优化设置
  • 辽宁住房城乡建设部官方网站哈尔滨网站建设
  • 北京十大最靠谱it培训机构seo搜索引擎优化到底是什么
  • 想制作自己的网站对网站和网页的认识
  • 江门网站关键词推广排名nba
  • 网站海外推广哪家好web成品网站源码免费
  • 一个企业的网站建设网站宣传推广方案
  • wordpress 七牛oss北京seo学校
  • 厦门高端网站建设公司网店营销策划方案范文
  • java兼职网站开发谷歌搜索引擎入口2023
  • 临沂怎么做网站sem是什么意思呢
  • 做网站用啥软件好网络推广公司加盟
  • 教育公司网站建设方案苏州百度推广公司地址
  • 长春网站制作优势吉网传媒google 优化推广
  • thinkphp5 做网站网络推广的公司更可靠
  • 前端基础知识南安seo
  • 网站建设公司怎么做好企业网站优化价格
  • 广东省住房城乡建设厅门户网站在百度怎么创建自己的网站
  • 帮非法集资公司做网站违法吗广州私人做网站
  • 网站建设靠谱公司鹤壁网站推广公司
  • 网站备案跟域名有什么关系好搜搜索引擎