SpringBoot 8/2

CRUD

  1. 发送put请求修改数据有三个步骤:

    1. SpringMVC中配置HiddenHttpMethodFilter
    2. 页面上创建一个post请求(form标签只能写get和post)
    3. 创建一个input项目,name="_method",值就是我们所指定方式
    4. <input type="hidden" name="_method" value="put" th:if="${emp!=null}" / >
    5. <input type="hidden" name="id" th:if="${emp!=null}" th:value="${emp.id}">

错误处理机制

  1. 错误提示有状态码,类型,时间....

  2. 默认效果:返回一个错误的页面。

  3. 如果是其他客户端,默认响应一个json数据,时间戳、状态码、错误提示、错误信息、访问路径。

  4. 如何定制错误信息:

    1. 一个是定义错误页面
    2. 一个是定义json数据
  5. external libiaries->spring-boot-auto-configure-web-ErrorMvcAutoConfigure容器中四个组件非常重要,分别是:

    1. DefaultErrorAttributes
    2. BasicErrorController:默认/error请求
    @Controller
    @RequestMapping({"${server.error.path:${error.path:/error}}"})
    public class BasicErrorController extends AbstractErrorController {
    @RequestMapping(
    produces = {"text/html"}
    )//产生html页面
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
    HttpStatus status = this.getStatus(request);
    Map<String, Object> model = Collections.unmodifiableMap(this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.TEXT_HTML)));
    response.setStatus(status.value());
    ModelAndView modelAndView = this.resolveErrorView(request, response, status, model);
    return modelAndView != null ? modelAndView : new ModelAndView("error", model);
    } @RequestMapping
    @ResponseBody//返回json数据
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
    Map<String, Object> body = this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.ALL));
    HttpStatus status = this.getStatus(request);
    return new ResponseEntity(body, status);
    }

    但是它是如何区分是网页浏览,还是其它客户端浏览呢?

可以看到浏览器在访问时的请求头accept属性为text/html,

  1. ErrorPageCustomizer
@Value("${error.path:/error}")
private String path = "/error"; //系统出现错误以后,到error进行请求,
  1. DefaultErrorViewResolver

一旦发生4..或者5..错误,ErrorPageCustomizer定制错误页面。

配置嵌入式Servlet容器

SpringBoot使用的是默认的Servlet容器(Tomcat)

问题:

  1. 如何定制和修改Servlet容器的相关配置

    1. 在application.properties当中修改server相关的配置

    2. server.port=8081
      server.context-path=/crud //通用的server设置
      server.xxx //tomcat的设置
      server.tomcat.uri-encoding=UTF-8
    3. 编写一个EmbeddedServletContainerCustomizer:嵌入式的servlet容器的定制器,来修改servlet容器的配置。

    4. (以上两种方式实际上在底层是一样的,都是EmbeddedServletContainerCustomizer)

  2. 能不能支持其他的Servlet容器

注册Servlet、Filter、Listener

Servlet:ServletRegistrationBean

Filter:FilterRegistrationBean

Listener:ServletListenerRegistrationBean

由于SpringBoot默认是以jar包方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。

注册三大组件用如下的方式:

 //注册三大组件
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
return servletRegistrationBean;
}

SpringBoot(1)的更多相关文章

  1. 解决 Springboot Unable to build Hibernate SessionFactory @Column命名不起作用

    问题: Springboot启动报错: Caused by: org.springframework.beans.factory.BeanCreationException: Error creati ...

  2. 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo

    Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...

  3. Springboot搭建web项目

    最近因为项目需要接触了springboot,然后被其快速零配置的特点惊呆了.关于springboot相关的介绍我就不赘述了,大家自行百度google. 一.pom配置 首先,建立一个maven项目,修 ...

  4. Java——搭建自己的RESTful API服务器(SpringBoot、Groovy)

    这又是一篇JavaWeb相关的博客,内容涉及: SpringBoot:微框架,提供快速构建服务的功能 SpringMVC:Struts的替代者 MyBatis:数据库操作库 Groovy:能与Java ...

  5. 解决 SpringBoot 没有主清单属性

    问题:SpringBoot打包成jar后运行提示没有主清单属性 解决:补全maven中的bulid信息 <plugin> <groupId>org.springframewor ...

  6. SpringBoot中yaml配置对象

    转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...

  7. springboot 学习资源推荐

    springboot 是什么?对于构建生产就绪的Spring应用程序有一个看法. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.(这是springboot的官方介绍) 我们为什么要学 ...

  8. Springboot框架

    本片文章主要分享一下,Springboot框架为什么那么受欢迎以及如何搭建一个Springboot框架. 我们先了解一下Springboot是个什么东西,它是干什么用的.我是刚开始接触,查了很多资料, ...

  9. 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧

    做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...

  10. 5分钟创建一个SpringBoot + Themeleaf的HelloWord应用

    第一步:用IDE创建一个普通maven工程,我用的eclipse. 第二步:修改pom.xml,加入支持SpringBoot和Themeleaf的依赖,文件内容如下: <?xml version ...

随机推荐

  1. ping fping

    通过ping来监测对端网络状态 ping fpinf在windows和linux上的参数是不同的,返回的结果也是不同的 在网络连通性监测方面用的比较多,在py go中调用命令,对返回的结果使用正则来在 ...

  2. QQ for Mac聊天纪录怎么查找??

    在你Mac上打开你的QQ,选择任意聊天窗口,打字的上面有6个图表快捷键,第6个就是查看聊天记录的功能键

  3. Exception in thread "main" java.lang.IllegalStateException: Failed to read 问题解决

    开发中偶尔遇到这样的问题:Exception in thread "main" java.lang.IllegalStateException: Failed to read .. ...

  4. 可持久化01trie树——模板

    给你一个数,在一段区间内找到另一个数,使得他们的异或最大: trie树上存储每个数的二进制位,查询时贪心查询能让当前高位取得1的位置: 实际上是一个求前缀和的思想.每个数都开一个trie树浪费空间,当 ...

  5. 「HNOI2016」序列

    传送门 Description 有 \(q\) 个询问,每个询问给定两个数\(l\) 和\(r\),求 \(a[l:r]\) 的不同子序列的最小值之和 Solution  校内模拟赛用了这道题,但是莫 ...

  6. pycharm无法识别自己的文件夹的程序

    网上找教程折腾了半天也没解决,然后我换了一下文件夹名称…… 文件夹名称不能用数字开头,否则识别不出来.

  7. hdoj - 1506 直方图中最大的矩形

    Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a commo ...

  8. 【算法编程 C++ python】单链表反序输出

    题目描述 输入一个链表,从尾到头打印链表每个节点的值.   以下方法仅仅实现了功能,未必最佳.在牛客网测试, C++:3ms 480k Python:23ms 5732k /** * struct L ...

  9. Spark安装(standalone)

    文档:http://spark.apache.org/docs/latest/spark-standalone.html 安装scalahttps://www.scala-lang.org/downl ...

  10. Tomcat的并发能力

    关注   一.一些限制 Windows 每个进程中的线程数不允许超过 2000 Linux 每个进程中的线程数不允许超过 1000 在 Java 中每开启一个线程需要耗用 1MB 的 JVM 内存空间 ...