Web 开发简介
- 使用 SpringBoot 开发项目基本分为三个步骤;
- 创建 Spring Boot 应用,选中需要的模块,如web项目则选中web模块;
- Spring Boot 已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来
- 自己编写业务代码;
- 自动配置原理要搞清楚,一个场景中 Spring Boot 自动配置了什么?能不能修改?能修改哪些配置?能不能扩展?等等
xxxxAutoConfiguration:帮我们给容器中自动配置组件;
xxxxProperties: 配置类来封装配置文件的内容;
新建 web 项目
默认pom. xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>www.wmx.lct</groupId>
<artifactId>dog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dog</name>
<description>Demo project for Spring Boot</description>
<!-- Spring Boot 项目都要继承的 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<!-- 编码-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web 模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!-- Spring Boot 测试-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 打包的插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
UserController 控制器
package com.lct.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* Created by Administrator on 2018/7/15 0015.
*
* @RestController :表示本类所有的方法都是直接做内容返回到浏览器
* @Controller :用户控制层
*/
@RestController
@Controller
public class UserController {
@RequestMapping(value = "hello")
public String hello() {
return new Date() + " : 欢迎您!";
}
}
浏览器访问测试
静态资源映射规则
- Spring boot 有自己约定的静态资源映射规则,可以在“org.springframework.boot.autoconfigure.web”包下面的“ResourceProperties”类中找到
@ConfigurationProperties(
prefix = "spring.resources",
ignoreUnknownFields = false
)
public class ResourceProperties {
//可以设置和静态资源有关的参数,如缓存时间等
静态资源映射
- ResourceProperties类中约定如下,即默认应用中的静态资源都是从类路径下的以下目录中进行寻找
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
/**
* Locations of static resources. Defaults to classpath:[/META-INF/resources/,
* /resources/, /static/, /public/].
*/
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
- "/**" 访问当前项目的任何资源,都去(静态资源的文件夹)找映射
"classpath:/META‐INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
- "/**" 访问当前项目的任何资源,都去(静态资源的文件夹)找映射
配置应用首页
- 欢迎页即应用首页,默认映射静态资源文件夹下的所有index.html页面作为首页。
- “localhost:8080/应用上下文路径 ”则默认找index页面
WebJars 简述
- Web 前端使用了越来越多的JS或CSS,如jQuery、Bootstrap等。一般情况下,将这些Web资源拷贝到Java Web项目的webapp相应目录下进行管理。
- WebJars 是将web前端资源(js,css等)打成jar包文件,然后借助Maven工具,以jar包形式对web前端资源进行统一依赖管理,保证这些Web资源版本唯一性。
- WebJars 的jar包部署在Maven中央仓库上。WebJars官网:http://www.webjars.org/
- 使用起来更加简单,就像使用后台Java依赖一样,就它们同样在pom.xml中导入依赖即可
导入webJars
- 注意:应用中导入的所有jar包,如下面的“jquery-3.2.0.jar(library home)”就相当于类路径
- “META-INF/resources”本身就是Spring Boot 约定的类路径下的静态资源默认访问目录,所以访问的时候不能再路径中加上“META-INF/resources”,而是直接从“webjars”层级开始
使用 webjars
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>首页</title>
<!-- 导入的所有webjars下的META-INF/resources与自己应用中的静态资源目录是一样的
导入之后,这里写的时候就会有提示了-->
<script type="text/javascript" src="webjars/jquery/3.2.0/dist/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
alert($("h3").text());
});
</script>
</head>
<body>
<h3>中国-长城</h3>
</body>
</html>