根据上个章节建立的树结构工程的基础上进行讲解
首先在diary工程的pom内引入spring-boot-starter-parent做为父工程用来管理工程内的jar包版本
导入jar包后记得用maven刷新diary工程
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
然后引入spring-boot-starter-web模块
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
</dependencies>
如果想要把项目打包成一个可执行的jar包,需要添加maven的一下组件并设置一下UTF-8编码:
<build>
<plugins>
<!-- 编译插件(设置源代码的JDK版本,目标代码JDK版本,编译字符集) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
diary.web工程内加入,thymeleaf模板引擎这样才可以访问html文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
新建一个application.yaml文件在diary.web.admin下的src/main/resources文件夹内
内容如下,值得注意的是yaml对缩进要求非常严格如果格式不正确的话节点将失效
# application.yaml
# Server settings (ServerProperties)
server:
servlet:
contextPath: /admin
port: 8080
address: 127.0.0.1
sessionTimeout: 30
# Tomcat specifics
tomcat:
accessLogEnabled: false
protocolHeader: x-forwarded-proto
remoteIpHeader: x-forwarded-for
basedir:
backgroundProcessorDelay: 30 # secs
spring:
thymeleaf:
cache: false
prefix: /WEB-INF/views/
suffix: .html
然后新建一个
类型名为src/main/webapp的文件夹用来存放html等文件,结构如下
然后新建一个org.run.web包用来存放启动器
package org.run.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* <p>Title : WebApplication</p>
* <p>Description : 启动类</p>
* <p>DevelopTools : Eclipse_x64_v4.7.1</p>
* <p>DevelopSystem : Windows 10</p>
* <p>Company : org.wcy</p>
* @author : WangChenYang
* @date : 2018年11月14日 下午9:10:14
* @version : 0.0.1
*/
@SpringBootApplication
//@ComponentScan用于配置扫描org.wcy.web之外的包下面的类
@ComponentScan(basePackages={"org.wcy"})
public class WebApplication {
public static void main(String[] args) {
SpringApplication sa=new SpringApplication(WebApplication.class);
// 禁用devTools热部署
//System.setProperty("spring.devtools.restart.enabled", "false");
// 禁用命令行更改application.properties属性
sa.setAddCommandLineProperties(false);
sa.run(args);
}
}
org.wcy.config包内的MyWebMvcConfig.java用来配置静态文件访问路径
package org.wcy.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* <p>Title : MyWebMvcConfig</p>
* <p>Description : 自定义静态资源映射路径和静态资源存放路径</p>
* <p>DevelopTools : Eclipse_x64_v4.7.1</p>
* <p>DevelopSystem : Windows 10</p>
* <p>Company : org.wcy</p>
* @author : WangChenYang
* @date : 2018年11月14日 下午9:07:33
* @version : 0.0.1
*/
@Configuration
public class MyWebMvcConfig extends WebMvcConfigurationSupport {
/**
* 在Spring添加拦截器之前先创建拦截器对象,这样就能在Spring映射这个拦截器前,把拦截器中的依赖注入的对象给初始化完成了。
* </br>避免拦截器中注入的对象为null问题。
* @return
*/
/*@Bean
public SecurityInterceptor getSecurityInterceptor(){
return new SecurityInterceptor();
}*/
/**
* 添加自定义静态资源映射路径和静态资源存放路径(图片)
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
System.out.println("配置静态资源");
registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/WEB-INF/resources/");
super.addResourceHandlers(registry);
}
/**
* 添加拦截器
* @param registry
*/
/*@Override
public void addInterceptors(InterceptorRegistry registry) {
// 多个拦截器组成一个拦截器链
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用户排除拦截
registry.addInterceptor(getSecurityInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}*/
}
测试controller访问login.html
package org.wcy.controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
/**
* @描述:返回登陆界面
* @创建人:WangChenYang
* @创建时间:2018年11月16日 上午11:24:41
* @return
*/
@RequestMapping("/login.html")
public String login() {
return "login/login";
}
}
链接:http://127.0.0.1:8080/admin/login.html
以上就是springboot的基本配置,接下来我将会继续学习springboot并更新博客,这是我的QQ1900353090希望可以和大家一起学习进步^_^