配套视频:【编程不良人】2021年SpringBoot最新最全教程_哔哩哔哩_bilibili
1.1 拦截器介绍 filter 过滤器: 过滤器可以拦截javaweb中请求,进行放行、中断等功能
强大之处: 可以拦截一切资源 .jsp、.html、.css、.img .....
1.定义: 拦截器类似于javaweb中filter功能,但只能拦截controller相关的请求
2.作用: 将controller中共有代码放入到拦截器中执行,减少controller中代码冗余
3.拦截器特性:
1).拦截器只能拦截controller相关请求
2).拦截器可以中断请求轨迹
3).请求之前如果该请求配置了拦截器,请求会先经过拦截器,拦截器放行之后执行请求的controller,controller执行完成之后会回到拦截器继续执行拦截器中的代码
4.拦截器开发
a.类 implement HandlerInterceptor接口 实现接口的类有默认实现的preHandler、postHandler、afterCompetition方法
preHandler 预先处理方法: 最先执行方法,返回值布尔类型 true:放行请求、fals:中断(后续不再执行)
返回结果为true时执行controller中内容
postHandler 过程中处理: controller返回之后回到postHandler这个方法执行,执行完成这个方法开始响应浏览器
afterCompletion 最后完成: 当响应结束之后会执行拦截器中这个方法内容
b.配置拦截器
springmvc配置方式:mvc:interceptors springmvc.xml
springboot 提供了springmvc配置类: WebMvcConfigurer
类 implement WebMvcConfigurer接口{
//覆盖配置拦截器方法
1.使用哪个拦截器 2.拦截器拦截请求 3.排除那些请求
}
1.2 开发拦截器
MyInterceptor1
package com.study.interceptors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor1 implements HandlerInterceptor {
//日志对象
private static final Logger logger = LoggerFactory.getLogger(MyInterceptor1.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// logger.debug("handler:{}",handler);
logger.debug("1");
// response.sendRedirect(request.getContextPath()+"/403.jsp");
return true;//放行
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// logger.debug("modelAndView:{}",modelAndView);//view="xxx"; model={xxx}
// logger.debug("model:{}",modelAndView.getModel());
// logger.debug("view:{}",modelAndView.getViewName());
logger.debug("2");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
logger.debug("3");
}
}
MyInterceptor2
package com.study.interceptors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor2 implements HandlerInterceptor {
//日志对象
private static final Logger logger = LoggerFactory.getLogger(MyInterceptor2.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// logger.debug("handler:{}",handler);
logger.debug("4");
return true;//放行
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// logger.debug("modelAndView:{}",modelAndView);//view="xxx"; model={xxx}
// logger.debug("model:{}",modelAndView.getModel());
// logger.debug("view:{}",modelAndView.getViewName());
logger.debug("5");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
logger.debug("6");
}
}
1.3 配置拦截器
package com.study.config;
import com.study.interceptors.MyInterceptor1;
import com.study.interceptors.MyInterceptor2;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor1()) //指定拦截器
.addPathPatterns("
@Controller
@RequestMapping("interceptor")
public class InterceptorController {
private static final Logger logger = LoggerFactory.getLogger(InterceptorController.class);
@RequestMapping("test")
public String test(){
logger.debug("test is ok!");
return "success";//此处返回ModelAndView对象,会自动封装,此时只有view:success.jsp
}
}
1.5 项目目录结构
2. war包部署
配套视频:【编程不良人】2021年SpringBoot最新最全教程_哔哩哔哩_bilibili
2.1 思路
a.执行项目打包的方式为 "war",但默认创建springboot项目打包都是jar
因此需要修改项目的pom.xml:
添加:war
b.去除springboot项目内嵌tomcat依赖
org.apache.tomcat.embed
tomcat-embed-jasper
provided
org.springframework.boot
spring-boot-starter-tomcat
provided
c.在pom.xml的插件(plugins)中配置入口类
org.springframework.boot
spring-boot-maven-plugin
true
-Dfile.encoding=UTF-8
com.baizhi.SpringbootDay6Application
d.启动类添加配置
//1.继承SpringBootServletInitializer 不再使用内嵌容器启动,使用外部tomcat容器启动
//2.覆盖configure方法
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
2.2 打包部署具体配置
pom.xml设置打包方式为war:
com.study spring-boot-day60.0.1-SNAPSHOT spring-boot-day6 war Demo project for Spring Boot 1.8
在pom.xml的插件(plugin)中指定入口类:
org.springframework.boot spring-boot-maven-plugintrue -Dfile.encoding=UTF-8 com.study.SpringBootDay6Application
排除内嵌的tomcat:pom.xml中修改依赖
org.apache.tomcat.embed tomcat-embed-jasperprovided org.springframework.boot spring-boot-starter-tomcatprovided
配置入口类XxxApplication
@SpringBootApplication
//继承SpringBootServletInitializer:不再使用内嵌tomcat容器启动,使用外部tomcat容器启动
public class SpringBootDay6Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringBootDay6Application.class, args);
}
//重写configure方法
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringBootDay6Application.class);
}
}
2.3 打包测试
打包顺序如下:
打包报错:
D:Software_DevelopmentJDKbinjava.exe -Dmaven.multiModuleProjectDirectory=D:Software_DevelopmentIDEA_codeSpringBootspring-boot-day6 "-Dmaven.home=C:Program FilesJetBrainsIntelliJ IDEA 2020.1pluginsmavenlibmaven3" "-Dclassworlds.conf=C:Program FilesJetBrainsIntelliJ IDEA 2020.1pluginsmavenlibmaven3binm2.conf" "-Dmaven.ext.class.path=C:Program FilesJetBrainsIntelliJ IDEA 2020.1pluginsmavenlibmaven-event-listener.jar" "-javaagent:C:Program FilesJetBrainsIntelliJ IDEA 2020.1libidea_rt.jar=56634:C:Program FilesJetBrainsIntelliJ IDEA 2020.1bin" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJetBrainsIntelliJ IDEA 2020.1pluginsmavenlibmaven3bootplexus-classworlds-2.6.0.jar" org.codehaus.classworlds.Launcher -Didea.version2020.1 -DskipTests=true package [INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.study:spring-boot-day6 >--------------------- [INFO] Building spring-boot-day6 0.0.1-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ spring-boot-day6 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Using 'UTF-8' encoding to copy filtered properties files. [INFO] Copying 1 resource [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.681 s [INFO] Finished at: 2022-06-04T17:29:55+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources) on project spring-boot-day6: Input length = 1 -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
通过上面报错信息可以发现可能是版本问题(3.2.0),解决办法参考链接:Failed to execute goal org.apache.maven.plugins:maven-resour_二炮的博客-CSDN博客,修改版本如下:
org.springframework.boot spring-boot-maven-pluginorg.apache.maven.plugins maven-resources-plugin2.4.3
重新打包,编译信息如下:
D:Software_DevelopmentJDKbinjava.exe -Dmaven.multiModuleProjectDirectory=D:Software_DevelopmentIDEA_codeSpringBootspring-boot-day6 "-Dmaven.home=C:Program FilesJetBrainsIntelliJ IDEA 2020.1pluginsmavenlibmaven3" "-Dclassworlds.conf=C:Program FilesJetBrainsIntelliJ IDEA 2020.1pluginsmavenlibmaven3binm2.conf" "-Dmaven.ext.class.path=C:Program FilesJetBrainsIntelliJ IDEA 2020.1pluginsmavenlibmaven-event-listener.jar" "-javaagent:C:Program FilesJetBrainsIntelliJ IDEA 2020.1libidea_rt.jar=57250:C:Program FilesJetBrainsIntelliJ IDEA 2020.1bin" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJetBrainsIntelliJ IDEA 2020.1pluginsmavenlibmaven3bootplexus-classworlds-2.6.0.jar" org.codehaus.classworlds.Launcher -Didea.version2020.1 -DskipTests=true package [INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.study:spring-boot-day6 >--------------------- [INFO] Building spring-boot-day6 0.0.1-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ spring-boot-day6 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ spring-boot-day6 --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 6 source files to D:Software_DevelopmentIDEA_codeSpringBootspring-boot-day6targetclasses [INFO] [INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ spring-boot-day6 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory D:Software_DevelopmentIDEA_codeSpringBootspring-boot-day6srctestresources [INFO] [INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ spring-boot-day6 --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:Software_DevelopmentIDEA_codeSpringBootspring-boot-day6targettest-classes [INFO] [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ spring-boot-day6 --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-war-plugin:3.3.2:war (default-war) @ spring-boot-day6 --- [INFO] Packaging webapp [INFO] Assembling webapp [spring-boot-day6] in [D:Software_DevelopmentIDEA_codeSpringBootspring-boot-day6targetspring-boot-day6-0.0.1-SNAPSHOT] [INFO] Processing war project [INFO] Copying webapp resources [D:Software_DevelopmentIDEA_codeSpringBootspring-boot-day6srcmainwebapp] [INFO] Building war: D:Software_DevelopmentIDEA_codeSpringBootspring-boot-day6targetspring-boot-day6-0.0.1-SNAPSHOT.war [INFO] [INFO] --- spring-boot-maven-plugin:2.7.0:repackage (repackage) @ spring-boot-day6 --- [INFO] Replacing main artifact with repackaged archive [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 16.423 s [INFO] Finished at: 2022-06-04T17:58:56+08:00 [INFO] ------------------------------------------------------------------------
出现上述提示信息,表示项目打包成功,生成的war包如下:
2.4 部署
1.Linux
安装jdk、安装tomcat、安装数据库、将打包好的war复制到tomcat webapps目录下
双击startup.sh启动
2.Windows
安装jdk后,将打包好的war复制到tomcat webapps目录下
双击startup.bat启动
此处以Windows为例进行说明:
(1)将打包好的war包重命名为springboot.war,复制到tomcat/webapps目录下
(2)双击bin目录下的startup.bat运行项目
出现上面错误是因为8080端口被占用,解决方案参考链接:Tomcat 8080 端口被占用解决方案_Wbbp的博客-CSDN博客_tomcat8080端口被占用怎么解决,可以在conf/server.xml中修改端口号:
保存修改后,继续双击startup.bat启动
出现上述提示,表示项目启动成功。
打开浏览器,访问地址:http://127.0.0.1:8888/springboot/interceptor/test 或者 http://127.0.0.1:8888/springboot/download.jsp 可以访问成功,表示项目部署成功!
3. Jar包部署配套视频:【编程不良人】2021年SpringBoot最新最全教程_哔哩哔哩_bilibili
3.1 思路a.3.2 打包部署具体配置jar 默认就是部署方式就是ja包部署 b.在target目录中获取对应打包好的jar文件 c.启动jar包 java -jar 对应jar文件名字 nohup &(其中,nohup &表示后台直接启动,不要日志) d.注意事项: 1.springboot项目在使用jsp模板时,jar包部署默认无法找到jsp页面,解决办法:修改插件版本为1.4.2,注意只有这唯一一个版本可用 2.指定jsp文件打包位置 org.springframework.boot spring-boot-maven-plugin1.4.2.RELEASE e.重新打包测试 src/main/webapp META-INF/resources **** false
a.pom.xml设置打包方式为jar
com.study spring-boot-day50.0.1-SNAPSHOT spring-boot-day5 jar Demo project for Spring Boot 1.8
注意:默认方式也是jar
b.执行打包
d.控制台命令窗口运行项目
e.测试访问:http://localhost:8888/spring-boot-day5/upload.jsp
jsp页面不能成功加载,注意:springboot在部署jsp时,插件版本必须指定为1.4.2版本,并进行jsp打包配置才可以,其他版本均不支持!!!即:其他版本无法读取到jsp文件
f.修改插件版本:在项目的pom.xml配置文件中build标签中修改
org.springframework.boot spring-boot-maven-plugin1.4.2.RELEASE org.apache.maven.plugins maven-resources-plugin2.4.3
h.指定jsp打包配置:在项目的pom.xml配置文件中build标签中加入resources配置
org.springframework.boot spring-boot-maven-plugin1.4.2.RELEASE org.apache.maven.plugins maven-resources-plugin2.4.3 src/main/webapp META-INF/resources **** false
重新打包部署测试即可!
重新clean报错:
Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:3.2.0:clean (default-clean) on project spring-boot-day5: Failed to clean project: Failed to delete D:Software_DevelopmentIDEA_codeSpringBootspring-boot-day5targetspring-boot-day5-0.0.1-SNAPSHOT.jar
原因:jar包正在被占用,无法使用,需要在资源任务管理器关闭Java(TM) Platform SE binary,再重新clean即可
Windows控制台部署运行: 打开浏览器访问路径:http://localhost:8888/spring-boot-day5/upload.jsp
上传文件后:
以上均测试成功,表示项目部署成功!
4. Thymeleaf配套视频:【编程不良人】2021年SpringBoot最新最全教程_哔哩哔哩_bilibili
待完善



