位置: IT常识 - 正文

最全面的SpringBoot教程(三)——SpringBoot Web开发(最全面的心脏检查怎么做)

编辑:rootadmin
最全面的SpringBoot教程(三)——SpringBoot Web开发 前言

推荐整理分享最全面的SpringBoot教程(三)——SpringBoot Web开发(最全面的心脏检查怎么做),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:最全面的英语,最全面的食疗书籍推荐,最全面的维生素哪个好,最全面的心脏检查怎么做,最全面的身体检查项目要多少钱的呢,最全面的心脏检查怎么做,最全面的英语,最全面的蛋糕教科书,内容如对您有帮助,希望把文章链接给更多的朋友!

本文为SpringBoot Web开发相关内容介绍,下边将对静态资源管理(包括:静态资源访问,静态资源前缀,webjar,首页支持),请求参数处理(包括:Rest风格,参数注释),数据响应,模板引擎(包括:Thymeleaf模板引擎,基本语法,thymeleaf使用),登录功能 + 拦截器,异常处理等进行详尽介绍~

📌博主主页:小新要变强 的主页 👉Java全栈学习路线可参考:【Java全栈学习路线】最全的Java学习路线及知识清单,Java自学方向指引,内含最全Java全栈学习技术清单~ 👉算法刷题路线可参考:算法刷题路线总结与相关资料分享,内含最详尽的算法刷题路线指南及相关资料分享~ 👉Java微服务开源项目可参考:企业级Java微服务开源项目(开源框架,用于学习、毕设、公司项目、私活等,减少开发工作,让您只关注业务!)

目录SpringBoot Web开发前言目录一、静态资源管理1️⃣静态资源访问2️⃣静态资源前缀3️⃣webjar4️⃣首页支持二、请求参数处理1️⃣Rest风格2️⃣参数注释三、数据响应四、模板引擎1️⃣Thymeleaf模板引擎2️⃣基本语法3️⃣thymeleaf使用五、登录功能 + 拦截器六、异常处理后记

一、静态资源管理1️⃣静态资源访问

默认情况下,Spring Boot 从类路径中的/static (或/public 或/resources 或/META-INF/resources)目录或 ServletContext的根目录提供静态内容。

访问: 当前项目根路径/ + 静态资源名

原理: 静态映射/**。

请求进来,先去找Controller看能不能处理。不能处理的所有请求又都交给静态资源处理器。静态资源也找不到则响应404页面

我们添加图片到resource下的static里:

请求进来,先去找Controller看能不能处理。不能处理的所有请求又都交给静态资源处理器。静态资源也找不到则响应404页面

2️⃣静态资源前缀

可以添加访问静态资源前缘:

spring: mvc: static-path-pattern: /res/**

现在访问就是: 当前项目根路径 + /res + 静态资源名

3️⃣webjar

webjar官网:https://www.webjars.org/

webJars是可以让大家以jar包的形式来使用前端的各种框架、组件。例如,引用jquery。

<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.6.1</version></dependency>

访问路径:当前项目根路径/ + webjars/**

4️⃣首页支持静态资源路径下 index.html可以配置静态资源路径不能与静态资源前缀共用spring: resources: static-locations: [classpath:/haha/]二、请求参数处理1️⃣Rest风格

RESTFUL是一种网络应用程序的设计风格和开发方式。

对比:

功能传统请求Rest风格获取用户/user/getUser (GET请求)/user (GET请求)保存用户/user/saveUse (POST请求)/user (POST请求)修改用户/user/editUser (POST请求)/user (PUT请求)删除用户/user/deleteUser(POST请求)/user (DELETE请求)

springboot用法:表单method=post,隐藏域 _method=put。

(1)开启页面表单的Rest功能

spring: mvc: hiddenmethod: filter: # 开启页面表单的Rest功能 enabled: true

(2)添加页面请求

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><script src="/webjars/jquery/3.6.1/jquery.js"></script><body> <h1>首页</h1> <button id="getUser">获取用户</button> <button id="saveUser">保存用户</button> <button id="editUser">修改用户</button> <button id="deleteUser">删除用户</button> <p id="msg"></p><script> $("#getUser").on("click",()=>{ $.get("/user",(res)=>{ $("#msg").text(res); }) }); $("#saveUser").on("click",()=>{ sendAjax(null); }); $("#editUser").on("click",()=>{ sendAjax('PUT'); }); $("#deleteUser").on("click",()=>{ sendAjax("DELETE"); }); function sendAjax(type){ let data = {'_method':type} $.post("/user",data,(res)=>{ $("#msg").text(res); }) }</script></body></html>

(3)添加后端接口

// 组合注解,@Controller + RequestBody@RestController@RequestMapping("/user")public class UserController { // 普通写法 // @RequestMapping(value = "/user",method = RequestMethod.GET) // 精简写法 @GetMapping public String getUser(){ return "get user"; } @PostMapping public String saveUser(){ return "post user"; } @PutMapping public String editUser(){ return "put user"; } @DeleteMapping public String deleteUser(){ return "delete user"; }}

为什么明明请求方式是POST,会跑到别的接口。

核心Filter;HiddenHttpMethodFilter。由过滤器来判断改变。

最全面的SpringBoot教程(三)——SpringBoot Web开发(最全面的心脏检查怎么做)

如果请求方式是直接发送Put、delete等方式请求,无需Filter。

扩展:_method的值可以自定义,只需要重新实现过滤器方法即可。

//自定义filter@Beanpublic HiddenHttpMethodFilter hiddenHttpMethodFilter(){ HiddenHttpMethodFilter methodFilter = new HiddenHttpMethodFilter(); methodFilter.setMethodParam("_m"); return methodFilter;}2️⃣参数注释@PathVariable:从请求路径上获取参数@RequestHeader:从请求头上获取参数@RequestParam:从请求参数上获取参数@CookieValue:从请求Cookie中获取参数@RequestBody:从请求body上获取参数@MatrixVariable:从请求路径上;分割获取变量

SpringBoot默认禁用语法: 请求路径/test;user=jack;age=16,interests=sleep,dream后端接收,@MatrixVariable("user") String name,@MatrixVariable("age") Integer age,@MatrixVariable("interests") List<String> interests@GetMapping("/{id}")public String getParam(@PathVariable("id") Integer id, @RequestHeader("Host") String host, @RequestParam("name") Integer name, @CookieValue("_username") String usernmae){}@PostMappingpublic void postMethod(@RequestBody String content){}// 可以传多个值,用对象来接收,存在相同属性时,会自动封装到里面。@PostMappingpublic void postMethod(@RequestBody Student student){}三、数据响应

数据响应,一般分两个类型:

响应页面响应数据

响应数据的格式可以是json,xml,io流等。

SpringMVC支持返回值:

ModelAndViewModelViewResponseEntity ResponseBodyEmitterStreamingResponseBodyHttpEntityHttpHeadersCallableDeferredResultListenableFutureCompletionStageWebAsyncTask有 @ModelAttribute 且为对象类型的@ResponseBody 注解四、模板引擎

SpringBoot默认不支持 JSP,需要引入第三方模板引擎技术实现页面渲染。

1️⃣Thymeleaf模板引擎

官网:https://www.thymeleaf.org/

前端显示页面,是html页面。我们以前开发,做的是jsp页面,jsp可以动态渲染一些数据在页面上,可以写Java代码。JSP+Servlet+JavaBean,是我们很早之前就不用了,企业也用得少。

现在SpringBoot推荐Thymeleaf模板引擎。

2️⃣基本语法表达式名字语法用途变量取值${…}获取请求域、session域、对象等值选择变量*{…}获取上下文对象值消息#{…}获取国际化等值链接@{…}生成链接片段表达式~{…}jsp:include 作用,引入公共页面片段<!-- 常用标签,一般都是 th:XXX --><!-- 需要设置头部(非标准HTML5 规范),也可以不设置 --><html xmlns:th="http://www.thymeleaf.org"><!-- 不设置头部的写法(符合HTML5规范) --><p data-th-text="${msg}">msg</p><!--设置文本--><p th:text="${msg}">提醒消息</p><!--设置文本--><a th:href="@{href}">超链接</a><!-- 设置属性值 --><input type="text" th:id="${student.id}" /><!-- 获取session --><p th:id="${#session.user}" />3️⃣thymeleaf使用

(1)添加thymeleaf依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

(2)创建文件,springboot帮我们配置好了,我们直接开发页面即可

// 接口@Controllerpublic class IndexController { @GetMapping("/thymeleaf") public String index(Model model) { model.addAttribute("msg","hello thymeleaf"); model.addAttribute("link","www.baidu.com"); // 返回视图层 return "thymeleaf"; }}

在templates下新建thymeleaf.html:

<!doctype html><html><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title></head><body> <h1 data-th-text="${msg}">提醒消息</h1> <h2> <a data-th-href="${link}">超连接</a> </h2></body></html>

(3)效果

五、登录功能 + 拦截器

例子:访问项目,需要登录,如果没有登录就不能访问

🍀(1)添加登录页面:login.html

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>LOGIN</title> <style> * { margin: 0; padding: 0; } html { height: 100%; } body { height: 100%; } .container { height: 100%; background-image: linear-gradient(to right, #fbc2eb, #a6c1ee); } .login-wrapper { background-color: #fff; width: 358px; height: 588px; border-radius: 15px; padding: 0 50px; position: relative; left: 50%; top: 50%; transform: translate(-50%, -50%); } .header { font-size: 38px; font-weight: bold; text-align: center; line-height: 200px; } .input-item { display: block; width: 100%; margin-bottom: 20px; border: 0; padding: 10px; border-bottom: 1px solid rgb(128, 125, 125); font-size: 15px; outline: none; } .input-item:placeholder { text-transform: uppercase; } .btn { border: 0; font-size: 20px; text-align: center; padding: 10px; width: 100%; margin-top: 40px; background-image: linear-gradient(to right, #a6c1ee, #fbc2eb); color: #fff; } .msg { color:red; text-align: center; line-height: 88px; } a { text-decoration-line: none; color: #abc1ee; } </style></head><body><div class="container"> <div class="login-wrapper"> <div class="header">Login</div> <div class="form-wrapper"> <form data-th-action="@{/login}" method="post"> <input type="text" name="username" placeholder="username" class="input-item" / > <input type="password" name="password" placeholder="password" class="input-item" /> <input type="submit" class="btn" value="Login" /> </form> </div> <div class="msg" data-th-text="${msg}"> Don't have account? <a href="#">Sign up</a> </div> </div></div></body></html>

🍀(2)添加登录接口

@Controllerpublic class LoginController { @GetMapping("/") public String index() { // 返回视图层 return "/login/login"; } @PostMapping("/login") public String login(String username, String password, HttpSession session, Model model) { if(StringUtils.hasLength(username) && "123456".equals(password)){ //把登陆成功的用户保存起来 session.setAttribute("loginUserName",username); //登录成功重定向到 thymeleaf ; 重定向防止表单重复提交 return "redirect:/thymeleaf"; }else { model.addAttribute("msg","账号密码错误"); //回到登录页面 return "/"; } }}

🍀(3)添加拦截器

继承HandlerInterceptor 接口

@Slf4jpublic class LoginInterceptor implements HandlerInterceptor { /** * 目标方法执行之前 * @param request * @param response * @param handler * @return * @throws Exception */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 请求路径 request.getRequestURI(); //登录检查逻辑 HttpSession session = request.getSession(); Object loginUser = session.getAttribute("loginUserName"); if(loginUser != null){ //放行 return true; } //拦截住。未登录。跳转到登录页 request.setAttribute("msg","请先登录"); // 跳转 request.getRequestDispatcher("/").forward(request,response); return false; }}

🍀(4)配置拦截器

@Configurationpublic class AdminWebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()) // 所有请求都被拦截包括静态资源 .addPathPatterns("/**") // 放行的请求 .excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**"); }}

🍀(5)测试

六、异常处理

错误处理:

默认情况下,Spring Boot提供/error处理所有错误的映射对于浏览器客户端,响应一个“ whitelabel”错误视图,以HTML格式呈现相同的数据对于机器客户端,它将生成JSON响应,其中包含错误,HTTP状态和异常消息的详细信息

SpringBoot也为我们提供了自定义错误页的功能。

自定义错误页的话可以在静态路径(如/static/)下的error目录。或放在模板目录(如 /templates/)下的error目录,都会被SpringBootz自动解析。

DefaultErrorAttributes:定义错误页面中可以包含哪些数据。

后记

👉Java全栈学习路线可参考:【Java全栈学习路线】最全的Java学习路线及知识清单,Java自学方向指引,内含最全Java全栈学习技术清单~ 👉算法刷题路线可参考:算法刷题路线总结与相关资料分享,内含最详尽的算法刷题路线指南及相关资料分享~

本文链接地址:https://www.jiuchutong.com/zhishi/300488.html 转载请保留说明!

上一篇:QMS-云质说质量 - 9 我和我的客户投诉(1) - 逢年过节要祈祷(云质信息)

下一篇:自学Web前端开发学习讲解 – 入门篇(新手学web前端开发)

  • 抗税的性质
  • 股权转让主要交什么税
  • 子公司之间股权转让
  • 增值税是如何计提的
  • 资产超过5000万的企业所得税税率
  • 所得税费用贷方表示什么
  • 申报作废了还能修改吗
  • 权益法下被投资企业净资产增加
  • 小规模纳税人如何转一般纳税人
  • 税收的基本特点有
  • 用党委经费买的固定资产如何计提?
  • 暂估出库是什么意思
  • 冲销无形资产如何会计分录?
  • 个人开增值税普票有没有限额
  • 年终奖和当月工资合并申报税
  • 水果 增值税专票
  • 企业所得税一般纳税人是怎么缴纳的
  • 发票清单太多怎么办理
  • 南京房产税2021年
  • 建筑施工企业购进材料会计分录
  • 增值税发票丢失可以用复印件入账么
  • 开发经济适用房是否需要缴纳土增税
  • 土地使用税源编明细表怎么填
  • 员工看病报销要计税吗
  • 公司账上的资金与注册资金不一致可以吗?
  • ah股溢价是什么意思 搬砖
  • 长期待摊费用的内容和特征
  • 个体工商户怎么交社保
  • 鸿蒙系统怎么看运行程序
  • 公司收到利息怎么记账
  • rftray.exe - rftray是什么进程 有什么用
  • 办理发票缴销需要提供的资料
  • 一只棕色
  • 流动资产占总资产比重多少较为合适
  • vericut9.0环境变量
  • pytorch example
  • 制造业企业资金如何循环周转
  • 新手学web前端开发
  • 2021所得税季报怎么填
  • 所得税费用要结转损益吗
  • python如何访问私有方法
  • 捐款怎么组织
  • 企业每年需要做什么
  • 延期付款利息收入要交增值税吗
  • 以设备投资入股的账务处理
  • 以前年度损益调整在利润表中怎么填
  • 公对公转账在手机上怎么操作
  • 营利性幼儿园是什么意思
  • 现金日记账的登记证据有
  • 社保都有哪几项
  • 开具发票涉及到哪些会计科目?
  • 收到返还工会经费账务处理
  • 工厂厨房厨具
  • 开票信息指的是买方还是卖方
  • 分公司有哪些特点
  • 医疗器械销售能一年挣一百万么
  • 工程施工会计科目及账务处理
  • 会计往来账如何记账
  • 仓库周转率是什么意思
  • mySQL中in查询与exists查询的区别小结
  • unix系统中目录采用什么结构
  • tcpsyncookies
  • 虚拟机怎么不能玩游戏
  • win7能不能安装vs2019
  • linux 常用 命令
  • software protection延迟启动
  • linux ftp服务端
  • win7怎么设置网速限制
  • win10蓝屏故障
  • r语言和python画图
  • Unity3D游戏开发标准教程
  • 熟悉的拼音
  • Unity3D游戏开发(第2版)pdf
  • python:\n
  • 税务电子发票怎么开
  • 山东省国税地税体制改革
  • 仓储物流企业的事故指标
  • 高新企业人才落户北京
  • 12366国税网上申报
  • 韩国工资多少人民币
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设