一.spring boot的web开发支持
spring boot提供了spring-boot-starter-web为web开发予以支持,spring-boot-starter-web为我们提供了嵌入的tomcat以及spring mvc的依赖。
二.Thymeleaf模板引擎
推荐使用的模板,因为提供了完美的spring mvc集成
1.Thymeleaf基础知识
1.1 引入Thymeleaf
<html xmlns:th="http://www.thymeleaf.org"> <!--此命名空间,将镜头页面转换为动态视图,需要进行动态处理的元素将使用“th:”为前缀-->
<head></head>
<body>
<script th:src="@{jquery-1.10.2.min.js}" ></script> <!--通过“@{}”引用web静态资源-->
</body>
</html>
1.2 访问model中的数据
通过"${}"访问model中的属性
<div>
<span th:text="${person.name}" ></span> <!--访问model中的person的name属性。-->
</div>
1.3 数据判断与model中的数据迭代
<div th:if="${not #lists.isEmpty(personList)}" > <!--表达式判断personList是否为空-->
<ul>
<li th:each="person:${personList}" >
<span th:text="${person.name}" />
<span th:text="${person.age}" />
</li>
</ul>
</div>
1.4 在js中访问model
<script th:inline="javascript">
var single = [[${singlePerson}]]; <!--通过"[[${}]]"格式获得实际的值-->
console.log(single.name+"/"+single.age)
</script>
1.5 其他
2.集成依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
三.Web相关配置
1.spring boot 提供的自动配置 --注:这节的内容有点懵
1.1 自动配置的ViewResolver
- ContentNegotiatingViewReolver
- BeanNameViewResolver
- InternalResourceViewResolver
1.2 自动配置的静态资源
1.3 自动配置的Formatter和Converter
1.4 自动配置的HttpMessageConverters
1.5 静态首页的支持。
eg:把静态index.html文件放在classpath:/resources/index.html,访问引用根目录http:locatlhost:8080时,会映射。
2.接管spring boot的web配置
2.1 在配置类上加注解@EnableWebMvc注解来实现完全自己控制的MVC
2.2 实现接口WebMvcConfigurer,既保留springboot自动配置,又增加自己的额外配置。
3. 注册servlet,Filter,Listener
项目启动监听
@Component
public class MyListener implements ApplicationListener<ContextRefreshedEvent>{
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent){
sout("项目启动。。。。。。。")
}
}
四. tomcat配置
1.配置tomcat
配置servlet容器
server.port=9090 #配置程序端口,默认为8080
server.session-timeout=5000 #用户会话session过期时间,以秒为单位
server.context-path=/demo #配置访问路径,默认为/
配置tomcat
server.tomcat.uri-encoding= UTF-8 #配置tomcat编码,默认为UTF-8
server.tomcat.compression=off #tomcat是否开启压缩,默认为关闭off
2.代码配置tomcat
2.1 通用配置
@Component
public static class CustomServletContainer implements EmbeddedServletContainerCustomizer{
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(8888);//配置端口
container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));//配置错误页面
container.setSessionTimeout(10,TimeUnit.MINUTES);//配置用户会话过期时间
}
}
2.2 特定配置
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setPort(8888);
factory.setSessionTimeout(10, TimeUnit.MINUTES);
factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
return factory;
}
3. 替换tomcat
依赖:
3.1 替换为Jetty
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
3.2 替换为Undertow
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
4.SSL配置
SSL(Secure Sockets Layer,安全套接层)是为网络通信提供安全及数据完整性的一种安全协议,SSL在网络传输层对网络连接进行加密。
基于B/S的Web应用中,是通过HTTPS来实现SSL的。HTTPS是以安全为目标的HTTP通道,简单讲是HTTP的安全版,即在HTTP下加入SSL层,HTTPS的安全基础是SSL。
4.1生成证书
打开控制台输入:
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
生成别名是 keystore.p12的文件,放到项目跟目录下
4.2 spring boot配置SSL
放好证书后,建立一个index.html放到resources/templates文件夹下,一会用于测试。
再配置properties
server.port=8888
server.tomcat.uri-encoding=utf-8
server.servlet.context-path=/demo
server.ssl.key-store=keystore.p12
server.ssl.key-store-password=123456
server.ssl.key-store-type=PKCS12
server.ssl.key-alias=tomcat
spring.thymeleaf.prefix=classpath:/templates/
4.3 http转向https
@Configuration
public class HttpsConfig {
/**
* spring boot 1.0
*/
/* @Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint constraint = new SecurityConstraint();
constraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
constraint.addCollection(collection);
context.addConstraint(constraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}*/
/**
* spring boot 2.0
* @return
*/
@Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint constraint = new SecurityConstraint();
constraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
constraint.addCollection(collection);
context.addConstraint(constraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}
@Bean
public Connector httpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
//Connector监听的http的端口号
connector.setPort(8080);
connector.setSecure(false);
//监听到http的端口号后转向到的https的端口号
connector.setRedirectPort(8888);
return connector;
}
}
控制层
@Controller
@RequestMapping
public class ViewControlller {
@GetMapping("index")
public String index(){
return "index";
}
}
当我们访问http://127.0.0.1:8080/demo/index 时会自动跳转到 https://127.0.0.1:8888/demo/index
5. Favicon配置(网站图标)
spring.mvc.favicon.enabled=false #默认为开启
springboot提供了要给默认的Favicon,若需要设置自己的Favicon,只需要将自己的文件,命名为favicon.ico放置在类路径根目录,类路劲resources/下,类路径static/下或者类路径public/下。
6.WebSocket
WebSocket为浏览器和服务端体哦国内了双工异步通信的功能,即流浪其可以向服务器其发送消息,服务端也可以向浏览器发送消息。
7.基于Bootstrap和AngularJS的现代web应用
现代的B/S系统软件特色:
- 单页面应用:(single-page application,简称SPA)指的是一种类似于原生客户端软件的更流畅的用户体验页面
- 相应时设计:(Responsive web design,简称RWD)指的是不同设备(电脑,平板,手机)访问相同的也买你的时候,得到不同的页面视图,而得到的视图时适用当前屏幕的。
- 数据导向:数据导向是对于页面导向而言的,页面上的数据获得是通过消费后台的REST服务来实现的,而不是通过服务器渲染的动态页面(如JSP)来实现,一般数据交互使用的格式是JSON