【Spring全家桶系列超全整理之SpringBoot框架】4 SpringBoot如何进行Web开发?

本文开始介绍SpringBoot相关内容。和【跨考菌】一起加油吧~

在这里插入图片描述

如果你有收获,记得帮博主一键三连哦😊


之前我们web开发,会在类路径下存在一个webapp目录,存放静态资源(例如jquery的jar包就一般放在这个目录下面)。但是使用springboot之后这个文件夹消失了,怎么引用呢?

其实在WebMvcAutoConfiguration类中就定义了针对所有资源的映射规则,根据规则配置即可。

1 SpringBoot对静态资源的映射规则

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties implements ResourceLoaderAware {
//可以设置和静态资源有关的参数,缓存时间等

WebMvcAuotConfiguration:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
	if (!this.resourceProperties.isAddMappings()) {
		logger.debug("Default resource handling disabled");
		return;
	} 
	Integer cachePeriod = this.resourceProperties.getCachePeriod();
	if (!registry.hasMappingForPattern("/webjars/**")) {
		customizeResourceHandlerRegistration(
		registry.addResourceHandler("/webjars/**")
		.addResourceLocations(
		"classpath:/META‐INF/resources/webjars/")
		.setCachePeriod(cachePeriod));
	} 
	String staticPathPattern = this.mvcProperties.getStaticPathPattern();
	//静态资源文件夹映射
	if (!registry.hasMappingForPattern(staticPathPattern)) {
		customizeResourceHandlerRegistration(
		registry.addResourceHandler(staticPathPattern)
		.addResourceLocations(
		this.resourceProperties.getStaticLocations())
		.setCachePeriod(cachePeriod));
		}
	} 
	//配置欢迎页映射
	@Bean
	public WelcomePageHandlerMapping welcomePageHandlerMapping(
	ResourceProperties resourceProperties) {
		return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
		this.mvcProperties.getStaticPathPattern());
	}
	//配置喜欢的图标
	@Configuration
	@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
	public static class FaviconConfiguration {
		private final ResourceProperties resourceProperties;
		public FaviconConfiguration(ResourceProperties resourceProperties) {
		this.resourceProperties = resourceProperties;
	} 
	@Bean
	public SimpleUrlHandlerMapping faviconHandlerMapping() {
		SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
		mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
		//所有 **/favicon.ico
		mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
		faviconRequestHandler()));
		return mapping;
	} 
	@Bean
	public ResourceHttpRequestHandler faviconRequestHandler() {
		ResourceHttpRequestHandler requestHandler = new
		ResourceHttpRequestHandler();
		requestHandler
		.setLocations(this.resourceProperties.getFaviconLocations());
		return requestHandler;
	}
}

1)、所有 /webjars/** ,都去classpath:/META-INF/resources/webjars/找资源;
webjars:以jar包的方式引入静态资源;http://www.webjars.org/

在这里插入图片描述

用法:只需要引入webjar的jar包即可:

<!‐‐引入jquery‐webjar‐‐>在访问的时候只需要写webjars下面资源的名称即可
<dependency>
	<groupId>org.webjars</groupId>
	<artifactId>jquery</artifactId>
	<version>3.3.1</version>
</dependency>

访问:localhost:8080/webjars/jquery/3.3.1/jquery.js

2)、"/**" 访问当前项目的任何资源,都去(静态资源的文件夹)找映射

"classpath:/META‐INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径

在这里插入图片描述
3)、欢迎页; 静态资源文件夹下的所有index.html页面;被"/**"映射;
localhost:8080/ 找index页面
4)、所有的 **/favicon.ico 都是在静态资源文件下找;

2 模板引擎

在这里插入图片描述
SpringBoot推荐的Thymeleaf;
语法更简单,功能更强大;

2.1 引入thymeleaf;

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring‐boot‐starter‐thymeleaf</artifactId>
	2.1.6
</dependency>
切换thymeleaf版本
<properties>
	<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
	<!‐‐ 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 ‐‐>
	<!‐‐ thymeleaf2 layout1‐‐>
	<thymeleaf‐layout‐dialect.version>2.2.2</thymeleaf‐layout‐dialect.version>
</properties>

2.2 Thymeleaf使用

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
    private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML5";

只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;

使用:

1、导入thymeleaf的名称空间

<html lang="en" xmlns:th="http://www.thymeleaf.org">

2、使用thymeleaf语法;

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF‐8">
<title>Title</title>
</head>
<body>
	<h1>成功!</h1>
	<!‐‐th:text 将div里面的文本内容设置为 ‐‐>
	<div th:text="${hello}">这是显示欢迎信息</div>
</body>
</html>

塞入数据:

//查出用户数据,在页面展示
    @RequestMapping("/success")
    public String success(Map<String,Object> map){
        map.put("hello","<h1>你好</h1>");
        return "success";
    }

2.3 语法规则

1)、th:text;改变当前元素里面的文本内容;

th:任意html属性;来替换原生属性的值

<!--th:text 将div里面的文本内容设置为 -->
<div id="div01" class="myDiv" th:id="${hello}" th:class="${hello}" th:text="${hello}">这是显示欢迎信息</div>

上述的id和class都会被替换掉。

在这里插入图片描述

2)遍历

<!-- th:each每次遍历都会生成当前这个标签: 3个h4 -->
<h4 th:text="${user}"  th:each="user:${users}"></h4>

在这里插入图片描述

<h4>
    <span th:each="user:${users}"> [[${user}]] </span>
</h4>

在这里插入图片描述

3)关于超链接引用
在这里插入图片描述
asserts/css/bootstrap.min.css是引用静态资源的方式。
那么如何通过模板引擎来引用呢。

因为所有 /webjars/** ,都去classpath:/META-INF/resources/webjars/找资源;所以只需要以/webjars开头,后面跟上具体的路径即可。
在这里插入图片描述

3 SpringMVC自动配置

Spring Boot自动配置好了SpringMVC
SpringBootSpringMVC的默认配置:(WebMvcAutoConfiguration),其中完成了如下工作:

  • 视图解析器
    • 自动配置了ViewResolver(视图解析器:根据方法的返回值得到视图对象(View),视图对象决定如何渲染(转发?重定向?))
    • ContentNegotiatingViewResolver:组合所有的视图解析器的;
    • 如何定制:我们可以自己给容器中添加一个视图解析器;自动的将其组合进来;
  • 静态资源文件夹路径,webjars
  • Static index.html support. 静态首页访问
  • Custom Favicon support (see below). favicon.ico
  • 自动注册了 of Converter , GenericConverter , Formatter beans
    • Converter:转换器; public String hello(User user):类型转换使用Converter
    • Formatter 格式化器; 2017.12.17===Date;
@Bean
@ConditionalOnProperty(prefix = "spring.mvc", name = "date‐format")//在文件中配置日期格式化的规则
public Formatter<Date> dateFormatter() {
	return new DateFormatter(this.mvcProperties.getDateFormat());//日期格式化组件
}

自己添加的格式化器转换器,我们只需要放在容器中即可

  • Support for HttpMessageConverters (see below).
    • HttpMessageConverter:SpringMVC用来转换Http请求和响应的;User—Json;
    • HttpMessageConverters 是从容器中确定;获取所有的HttpMessageConverter;

自己给容器中添加HttpMessageConverter,只需要将自己的组件注册容器中
(@Bean,@Component)

  • Automatic registration of MessageCodesResolver (see below).定义错误代码生成规则
  • Automatic use of a ConfigurableWebBindingInitializer bean (see below)
    我们可以配置一个ConfigurableWebBindingInitializer来替换默认的;(添加到容器)
初始化WebDataBinder;
请求数据=====JavaBean;

扩展SpringMVC

<mvc:view‐controller path="/hello" view‐name="success"/>
<mvc:interceptors>
	<mvc:interceptor>
		<mvc:mapping path="/hello"/>
		<bean></bean>
	</mvc:interceptor>
</mvc:interceptors>

编写一个配置类(@Configuration),是WebMvcConfigurerAdapter类型;不能标注@EnableWebMvc;

既保留了所有的自动配置,也能用我们扩展的配置;

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		// super.addViewControllers(registry);
		//浏览器发送 /atguigu 请求来到 success
		registry.addViewController("/atguigu").setViewName("success");
	}
}

原理:

1)、WebMvcAutoConfiguration是SpringMVC的自动配置类
2)、在做其他自动配置时会导入;@Import(EnableWebMvcConfiguration.class)

@Configuration
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {
private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
//从容器中获取所有的WebMvcConfigurer
@Autowired(required = false)
public void setConfigurers(List<WebMvcConfigurer> configurers) {
	if (!CollectionUtils.isEmpty(configurers)) {
		this.configurers.addWebMvcConfigurers(configurers);
		//一个参考实现;将所有的WebMvcConfigurer相关配置都来一起调用;
		@Override
		// public void addViewControllers(ViewControllerRegistry registry) {
		// for (WebMvcConfigurer delegate : this.delegates) {
		// delegate.addViewControllers(registry);
		// }
		}
	}
}

3)、容器中所有的WebMvcConfigurer都会一起起作用;
4)、我们的配置类也会被调用;
效果:SpringMVC的自动配置和我们的扩展配置都会起作用;

全面接管SpringMVC

SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己配置;所有的SpringMVC的自动配置都失效了

我们需要在配置类中添加@EnableWebMvc即可;

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
@EnableWebMvc
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		// super.addViewControllers(registry);
		//浏览器发送 /atguigu 请求来到 success
		registry.addViewController("/atguigu").setViewName("success");
	}
}

原理:
为什么@EnableWebMvc自动配置就失效了;
1)@EnableWebMvc的核心

@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {

2)、

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

3)、

@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,
WebMvcConfigurerAdapter.class })
//容器中没有这个组件的时候,这个自动配置类才生效
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

4)、@EnableWebMvcWebMvcConfigurationSupport组件导入进来;
5)、导入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;

修改SpringBoot的基本配置

模式:
1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean@Component)如果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默
认的组合起来;
2)、在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置
3)、在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置

4 案例

在这里插入图片描述
首先,public目录下的index.html属于静态资源,不需要做任何处理,因为静态资源直接去public、static、resources等目录下去找即可

但是,templates目录下的资源需要进行模板引擎的解析。

方式一:
自动映射到模板目录。

    @RequestMapping({"/","/index.html"})
    public String index(){
        return "index";
    }

方式二:
全局配置。

 //所有的WebMvcConfigurerAdapter组件都会一起起作用
    @Bean //将组件注册在容器
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("index");
                registry.addViewController("/index.html").setViewName("index");
                registry.addViewController("/main.html").setViewName("index");
            }
        return adapter;
    }

5 RestfulCRUD

5.1 默认访问首页

现在首页index.html存放在模板目录templates,要怎么访问(默认情况下访问的是静态目录下的index.html)。

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
//@EnableWebMvc   不要接管SpringMVC
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    //所有的WebMvcConfigurerAdapter组件都会一起起作用
    @Bean //将组件注册在容器
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("index");
                registry.addViewController("/index.html").setViewName("index");
                registry.addViewController("/main.html").setViewName("index");
            }
        };
        return adapter;
    }
}

5.2 国际化

1)、编写国际化配置文件;
在这里插入图片描述

2)、使用ResourceBundleMessageSource管理国际化资源文件

SpringBoot自动配置好了管理国际化资源文件的组件;

在这里插入图片描述
可以把配置文件直接放到message.properities下。也可以通过spring.messages.basename=i18n.login配置国际化的目录位置。

3)、在页面使用fmt:message取出国际化内容
在这里插入图片描述
效果:根据浏览器语言设置的信息切换了国际化;

原理:

国际化Locale(区域信息对象);LocaleResolver(获取区域信息对象);

 @Bean
 @ConditionalOnMissingBean
 @ConditionalOnProperty(
      prefix = "spring.mvc",
      name = {"locale"}
  )
  public LocaleResolver localeResolver() {
      if (this.mvcProperties.getLocaleResolver() == org.springframework.boot.autoconfigure.web.WebMvcProperties.LocaleResolver.FIXED) {
          return new FixedLocaleResolver(this.mvcProperties.getLocale());
      } else {
          AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
          localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
          return localeResolver;
      }
  }

默认的就是根据请求头带来的区域信息获取Locale进行国际化。
@ConditionalOnMissingBean表示你没配置LocaleResolver的时候使用默认的AcceptHeaderLocaleResolver处理国际化信息,你配置了自己的LocaleResolver则使用你自己的。

4)、点击链接切换国际化

/**
 * 可以在连接上携带区域信息
 */
public class MyLocaleResolver implements LocaleResolver {
    
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String l = request.getParameter("l");
        Locale locale = Locale.getDefault();
        if(!StringUtils.isEmpty(l)){
            String[] split = l.split("_");
            locale = new Locale(split[0],split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}

将区域文件对象注册到bean中:

@Bean
    public LocaleResolver localeResolver(){

        return new MyLocaleResolver();
    }
<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>

5.3 登录

如何实现开发阶段模板引擎在页面修改之后实时生效。

1)、禁用模板引擎的缓存

# 禁用缓存
spring.thymeleaf.cache=false

2)、页面修改完成以后ctrl+f9:重新编译;

处理登录请求

 //@RequestMapping(value = "/user/login",method = RequestMethod.POST)
    @PostMapping(value = "/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String, Object> map, HttpSession session) {
        if (!StringUtils.isEmpty(username) && "123456".equals(password)) {
            //登陆成功,防止表单重复提交,可以重定向到主页
            session.setAttribute("loginUser", username);
            // return "dashboard";
            return "redirect:/main.html";
        } else {
            //登陆失败

            map.put("msg", "用户名密码错误");
            return "login";
        }

    }
}

细节:

  • @PostMapping等价于@RequestMapping(value = "/user/login",method = RequestMethod.POST)
  • 如果直接return "dashboard";,浏览器的url不会变化(默认时转发),所以这里采用的时重定向的方式。

定义拦截器

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
//@EnableWebMvc   不要接管SpringMVC
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    //所有的WebMvcConfigurerAdapter组件都会一起起作用
    @Bean //将组件注册在容器
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/main.html").setViewName("dashboard");
            }

            //注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //super.addInterceptors(registry);
                //静态资源;  *.css , *.js
                //SpringBoot已经做好了静态资源映射
                registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                        .excludePathPatterns("/index.html","/","/user/login");
            }
        };
        return adapter;
    }
}

5.4 CRUD员工列表

在这里插入图片描述
具体的实验架构:
在这里插入图片描述
thymeleaf公共页面元素抽取

1、抽取公共片段
<div th:fragment="copy">
&copy; 2011 The Good Thymes Virtual Grocery
</div>
2、引入公共片段
<div th:insert="~{footer :: copy}"></div>
~{templatename::selector}:模板名::选择器
~{templatename::fragmentname}:模板名::片段名
3、默认效果:
insert的公共片段在div标签中
如果使用th:insert等属性进行引入,可以不用写~{}:
行内写法可以加上:[[~{}]];[(~{})]

三种引入公共片段的th属性:

th:insert:将公共片段整个插入到声明引入的元素中
th:replace:将声明引入的元素替换为公共片段
th:include:将被引入的片段的内容包含进这个标签中

下面看看这三种的效果分别是什么样子的?

<footer th:fragment="copy">
&copy; 2011 The Good Thymes Virtual Grocery
</footer>
引入方式
<div th:insert="footer :: copy"></div>
<div th:replace="footer :: copy"></div>
<div th:include="footer :: copy"></div>
效果
------------------------
<div>
<footer>
&copy; 2011 The Good Thymes Virtual Grocery
</footer>
</div>
------------------------
<footer>
&copy; 2011 The Good Thymes Virtual Grocery
</footer>
------------------------
<div>
&copy; 2011 The Good Thymes Virtual Grocery
</div>

在这里插入图片描述
在这里插入图片描述
获取所有员工列表

侧边栏的菜单项:

<li class="nav-item">
	<a class="nav-link active" href="#" th:href="@{/emps}" <!-- 请求地址为/emps -->
	th:class="${activeUri=='emps'?'nav-link active':'nav-link'}">
		<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-users">
			   <path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path>
			   <circle cx="9" cy="7" r="4"></circle>
			   <path d="M23 21v-2a4 4 0 0 0-3-3.87"></path>
			   <path d="M16 3.13a4 4 0 0 1 0 7.75"></path>
		</svg>
	员工管理
	</a>
</li>

控制层查询所有的员工列表:

//查询所有员工返回列表页面
@GetMapping("/emps")
public String list(Model model) {
    Collection<Employee> employees = employeeDao.getAll();

    //放在请求域中
    model.addAttribute("emps", employees);
    // thymeleaf默认就会拼串
    // classpath:/templates/xxxx.html
    return "emp/list"; // 模板引擎自动转到templates目录下寻找/emp/list.html
}

list.html:

<table class="table table-striped table-sm">
	<thead>
		<tr>
			<th>#</th>
			<th>lastName</th>
			<th>email</th>
			<th>gender</th>
			<th>department</th>
			<th>birth</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody>
		<tr th:each="emp:${emps}">
			<td th:text="${emp.id}"></td>
			<td>[[${emp.lastName}]]</td>
			<td th:text="${emp.email}"></td>
			<td th:text="${emp.gender}==0?'':''"></td>
			<td th:text="${emp.department.departmentName}"></td>
			<td th:text="${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}"></td>
		</tr>
	</tbody>
</table>

thymeleaf对日期的格式化:${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}

5.5 CRUD员工添加

list.html:

<h2><a class="btn btn-sm btn-success" href="emp" th:href="@{/emp}">员工添加</a></h2>

来到添加员工页面:

将部门信息带到添加员工的界面。

//来到员工添加页面
    @GetMapping("/emp")
    public String toAddPage(Model model) {
        //来到添加页面,查出所有的部门,在页面显示
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("depts", departments);
        return "emp/add";
    }

add.html(新增/修改公用页面):

<!--需要区分是员工修改还是添加;-->
<form th:action="@{/emp}" method="post">
	<!--发送put请求修改员工数据-->
	<!--
	1、SpringMVC中配置HiddenHttpMethodFilter;(SpringBoot自动配置好的)
	2、页面创建一个post表单
	3、创建一个input项,name="_method";值就是我们指定的请求方式
	-->
	<input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>
	<input type="hidden" name="id" th:if="${emp!=null}" th:value="${emp.id}">
	<div class="form-group">
		<label>LastName</label>
		<input name="lastName" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${emp.lastName}">
	</div>
	<div class="form-group">
		<label>Email</label>
		<input name="email" type="email" class="form-control" placeholder="zhangsan@atguigu.com" th:value="${emp!=null}?${emp.email}">
	</div>
	<div class="form-group">
		<label>Gender</label><br/>
		<div class="form-check form-check-inline">
			<input class="form-check-input" type="radio" name="gender" value="1" th:checked="${emp!=null}?${emp.gender==1}">
			<label class="form-check-label"></label>
		</div>
		<div class="form-check form-check-inline">
			<input class="form-check-input" type="radio" name="gender" value="0" th:checked="${emp!=null}?${emp.gender==0}">
			<label class="form-check-label"></label>
		</div>
	</div>
	<div class="form-group">
		<label>department</label>
		<!--提交的是部门的id-->
		<select class="form-control" name="department.id">
			<option th:selected="${emp!=null}?${dept.id == emp.department.id}" th:value="${dept.id}" th:each="dept:${depts}" th:text="${dept.departmentName}">1</option>
		</select>
	</div>
	<div class="form-group">
		<label>Birth</label>
		<input name="birth" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}">
	</div>
	<button type="submit" class="btn btn-primary" th:text="${emp!=null}?'修改':'添加'">添加</button>
</form>

员工添加:

//员工添加
    //SpringMVC自动将请求参数和入参对象的属性进行一一绑定;要求请求参数的名字和javaBean入参的对象里面的属性名是一样的
    @PostMapping("/emp")
    public String addEmp(Employee employee) {
        //来到员工列表页面

        System.out.println("保存的员工信息:" + employee);
        //保存员工
        employeeDao.save(employee);
        // redirect: 表示重定向到一个地址  /代表当前项目路径
        // forward: 表示转发到一个地址
        return "redirect:/emps";
    }

说明

  • 添加和修改通用一个页面,如何区分post和put请求呢?<input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>添加一个input框,根据request域是否有emp(修改的时候传入)来决定是否显示该input框。
  • 提交的日期默认只能是2017-12-12。如何修改默认格式?在配置文件中添加配置项:spring.mvc.date-format=yyyy-MM-dd

5.6 CRUD员工修改

list.html:

<a class="btn btn-sm btn-primary" th:href="@{/emp/}+${emp.id}">编辑</a>

查用户详情,回显到修改页面:

//来到修改页面,查出当前员工,在页面回显
    @GetMapping("/emp/{id}")
    public String toEditPage(@PathVariable("id") Integer id, Model model) {
        Employee employee = employeeDao.get(id);
        model.addAttribute("emp", employee);

        //页面要显示所有的部门列表
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("depts", departments);
        //回到修改页面(add是一个修改添加二合一的页面);
        return "emp/add";
    }

修改页面同上4.5.5中的add.html

员工信息修改:

//员工修改;需要提交员工id;
    @PutMapping("/emp")
    public String updateEmployee(Employee employee) {
        System.out.println("修改的员工数据:" + employee);
        employeeDao.save(employee);
        return "redirect:/emps";
    }

5.7 CURD员工删除

list.html:

<!-- html部分 -->
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<div class="table-responsive">
<table class="table table-striped table-sm">
	<thead>
		<tr>
			<th>#</th>
			<th>lastName</th>
			<th>email</th>
			<th>gender</th>
			<th>department</th>
			<th>birth</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody>
		<tr th:each="emp:${emps}">
			<td th:text="${emp.id}"></td>
			<td>[[${emp.lastName}]]</td>
			<td th:text="${emp.email}"></td>
			<td th:text="${emp.gender}==0?'':''"></td>
			<td th:text="${emp.department.departmentName}"></td>
			<td th:text="${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}"></td>
			<td>
				<!-- 设置属性:th:attr="" -->
				<button th:attr="del_uri=@{/emp/}+${emp.id}" class="btn btn-sm btn-danger deleteBtn">删除</button>
			</td>
		</tr>
	</tbody>
</table>
</div>
</main>
<form id="deleteEmpForm"  method="post">
<input type="hidden" name="_method" value="delete"/>
</form>


<!--js部分-->
<script>
$(".deleteBtn").click(function(){
	    //删除当前员工的
	    $("#deleteEmpForm").attr("action",$(this).attr("del_uri")).submit();
	    return false;
          });
</script>

说明

  • th:attr="del_uri=@{/emp/}+${emp.id}"设置自定义属性。
  • $(this).attr("del_uri")获取自定义属性的值。

员工删除:

//员工删除
    @DeleteMapping("/emp/{id}")
    public String deleteEmployee(@PathVariable("id") Integer id) {
        employeeDao.delete(id);
        return "redirect:/emps";
    }

6 错误处理机制

6.1 SpringBoot的默认错误处理机制

默认效果:

1)、浏览器,返回一个默认的错误页面
在这里插入图片描述
浏览器发送请求的请求头:
在这里插入图片描述
2)、如果是其他客户端(如:postman),默认响应一个json数据
在这里插入图片描述
在这里插入图片描述

6.2 如何定制错误响应

定制错误页面

1)、有模板引擎的情况下;error/状态码; 【将错误页面命名为 错误状态码.html 放在模板引擎文件夹里面的error文件夹下】,发生此状态码的错误就会来到对应的页面;

我们可以使用4xx和5xx作为错误页面的文件名来匹配这种类型的所有错误,精确优先(优先寻找精确的状态码.html);

页面能获取的信息;

timestamp:时间戳
status:状态码
error:错误提示
exception:异常对象
message:异常消息
errors:JSR303数据校验的错误都在这里

2)、没有模板引擎(模板引擎找不到这个错误页面),静态资源文件夹下找
3)、以上都没有错误页面,就是默认来到SpringBoot默认的错误提示页面;

定制错误JSON

1)、自定义异常处理&返回定制json数据;

@ControllerAdvice
public class MyExceptionHandler {
@ResponseBody
@ExceptionHandler(UserNotExistException.class)
	public Map<String,Object> handleException(Exception e){
		Map<String,Object> map = new HashMap<>();
		map.put("code","user.notexist");
		map.put("message",e.getMessage());
		return map;
	}
} /
/没有自适应效果...

此时发现浏览器和PostMan访问的话都返回的是JSON了,没有了自适应效果。

2)、转发到/error进行自适应响应效果处理

@ExceptionHandler(UserNotExistException.class)
    public String handleException(Exception e, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        //传入我们自己的错误状态码  4xx 5xx
        /**
         * Integer statusCode = (Integer) request
         .getAttribute("javax.servlet.error.status_code");
         */
        request.setAttribute("javax.servlet.error.status_code",500);
        map.put("code","user.notexist");
        map.put("message","用户出错啦");

        request.setAttribute("ext",map);
        //转发到/error
        return "forward:/error";
    }

将我们的定制数据携带出去;

//给容器中加入我们自己定义的ErrorAttributes
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {

    //返回值的map就是页面和json能获取的所有字段
    @Override
    public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
        Map<String, Object> map = super.getErrorAttributes(requestAttributes, includeStackTrace);
        map.put("company","atguigu");

        //我们的异常处理器携带的数据
        Map<String,Object> ext = (Map<String, Object>) requestAttributes.getAttribute("ext", 0);
        map.put("ext",ext);
        return map;
    }
}

最终的效果:响应是自适应的,可以通过定制ErrorAttributes改变需要返回的内容
在这里插入图片描述

7 配置嵌入式Servlet容器

SpringBoot默认使用Tomcat作为嵌入式的Servlet容器;
在这里插入图片描述

7.1 如何定制和修改Servlet容器的相关配置;

1、修改和server有关的配置(ServerProperties【也是EmbeddedServletContainerCustomizer】);

server.port=8081
server.context‐path=/crud
server.tomcat.uri‐encoding=UTF‐8
//通用的Servlet容器设置
server.xxx
//Tomcat的设置
server.tomcat.xxx

2、编写一个EmbeddedServletContainerCustomizer:嵌入式的Servlet容器的定制器;来修改Servlet容器的配置

@Bean //一定要将这个定制器加入到容器中
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
	return new EmbeddedServletContainerCustomizer() {
	//定制嵌入式的Servlet容器相关的规则
		@Override
		public void customize(ConfigurableEmbeddedServletContainer container) {
			container.setPort(8083);
		}
	};
}

7.2 注册Servlet三大组件【Servlet、Filter、Listener】

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

注册三大组件用以下方式

ServletRegistrationBean

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

FilterRegistrationBean

@Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
        return registrationBean;
    }

ServletListenerRegistrationBean

@Bean
    public ServletListenerRegistrationBean myListener(){
        ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return registrationBean;
    }

7.3 替换为其他嵌入式Servlet容器

在这里插入图片描述
默认支持:

Tomcat(默认使用)

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring‐boot‐starter‐web</artifactId>
	引入web模块默认就是使用嵌入式的Tomcat作为Servlet容器;
</dependency>

Jetty:

<!‐‐ 引入web模块 ‐‐>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring‐boot‐starter‐web</artifactId>
<exclusions>
	<exclusion>
		<artifactId>spring‐boot‐starter‐tomcat</artifactId>
		<groupId>org.springframework.boot</groupId>
	</exclusion>
</exclusions>
</dependency>
<!‐‐引入其他的Servlet容器‐‐>
<dependency>
	<artifactId>spring‐boot‐starter‐jetty</artifactId>
	<groupId>org.springframework.boot</groupId>
</dependency>

Undertow:

<!‐‐ 引入web模块 ‐‐>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring‐boot‐starter‐web</artifactId>
<exclusions>
	<exclusion>
		<artifactId>spring‐boot‐starter‐tomcat</artifactId>
		<groupId>org.springframework.boot</groupId>
	</exclusion>
</exclusions>
</dependency>
<!‐‐引入其他的Servlet容器‐‐>
<dependency>
	<artifactId>spring‐boot‐starter‐undertow</artifactId>
	<groupId>org.springframework.boot</groupId>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值