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. Impala 介绍(转载)

    一.简介 1.概述 Impala是Cloudera公司推出,提供对HDFS.Hbase数据的高性能.低延迟的交互式SQL查询功能. •基于Hive使用内存计算,兼顾数据仓库.具有实时.批处理.多并发等 ...

  2. Time of Trial

    Time of Trial(思维) 题目大意:一个人想逃,一个人要阻止那个人逃跑 ,阻止者念每一个符咒的时间为b[i],逃跑者念每一个符咒的时间为a[i],逃跑者如果念完了第i个符咒,阻止者还没有念完 ...

  3. Oracle in不超过1000,List<String>参数拆分,代码举例

    public Map<String,Map<String, Object>> getConsInfo(List<String> consIdList) { Map& ...

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

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

  5. Web前端开发规范之脚本文件和动态文本文件命名规则

    脚本文件:一般使用脚本功能的英文小写缩写命名 实际模块:例如广告条的javascript文件名为ad.js,弹出窗口的javascript文件名为pop.js 公用模块:js文件命名:英文命名,后缀j ...

  6. css3实现左侧固宽,右侧随着屏幕,右侧随着屏幕变化而变化

    A, ----float+calc(css3新属性计算属性)方式 <div class="Father"> <div class="LeftChildr ...

  7. sqlite数据库使用具体案例以及mysqlite.db数据库

    本文操作是测试数据库的其中一张表,其中包括清空sqlite数据库MyGroup表中的数据,清空sqlite数据库sqlite_sequence表中的自增变量,sqlite数据库MyGroup直接插入数 ...

  8. Netty 读写检测机制(心跳)

    一.创建服务端 1.MyServer 类 public class MyServer { public static void main(String[] args) throws Exception ...

  9. IntelliJ IDEA Check out from git

    点击check out from vesion control 填写git地址,test,clone

  10. 【转载】 LSTM构建步骤以及static_rnn与dynamic_rnn之间的区别

    原文地址: https://blog.csdn.net/qq_23981335/article/details/89097757 --------------------- 作者:周卫林 来源:CSD ...