013-Spring Boot web静态资源ServletFilterlistenter

Posted 木子旭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了013-Spring Boot web静态资源ServletFilterlistenter相关的知识,希望对你有一定的参考价值。

一、静态资源

1.1、webapp默认支持静态资源

在src/main/webapp下建立user.html默认支持访问

1.2、默认内置静态资源目录。可被直接访问

查看包:spring-boot-autoconfigure-1.5.9.RELEASE.jar下的:org.springframework.boot.autoconfigure.web;

查看:ResourceProperties,其中

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/" };

默认放在以上四个目录均可被访问。

可以通过修改配置项,在src/main/resources下的application.properties中

spring.resources.staticLocations=classpath:/html/

二、spring boot中使用Servlet【原始】以来Servlet3.x注解功能

2.1、使用Servlet

新建一个UserServlet.java

技术分享图片
package com.lhx.spring.springboot_web_ext;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/user.do")
public class UserServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().print("user servlet");
    }
}
View Code

并且在启动类上增加Servlet扫描注解:ServletComponentScan

技术分享图片
@ServletComponentScan
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
        //context.close();
    }
}
View Code

访问即可

2.2、使用Filter

新建一个LogFilter.java拦截User.do

技术分享图片
package com.lhx.spring.springboot_web_ext;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter("/user.do")
public class LogFilter implements Filter {

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
            throws IOException, ServletException {
        System.out.println("income log filter " + arg0.getRemoteHost());
        arg2.doFilter(arg0, arg1);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub

    }

}
View Code

此时运行即可

2.3、使用Listener

技术分享图片
package com.lhx.spring.springboot_web_ext;

import java.time.LocalDate;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class MyContextListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println("app startup at " + LocalDate.now().toString());

    }

}
View Code

此时运行即可

三、spring boot中使用Servlet【Configuration配置】不依赖Servlet3.0,使用Servlet2.x可以使用

3.1、使用Servlet

增加BookServlet.java,注意没有注解

技术分享图片
package com.lhx.spring.springboot_web_ext.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BookServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("test");
    }
}
View Code

增加配置类,ServletConfiguration.java

技术分享图片
package com.lhx.spring.springboot_web_ext.servlet;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootConfiguration
public class ServletCongfiguration {

    @Bean
    public ServletRegistrationBean createBookServlet() {
        ServletRegistrationBean servlet = new ServletRegistrationBean(new BookServlet(),"book.do");
        return servlet;
    }

}
View Code

此时访问即可。

3.2、使用Filter

增加EchoFilter.java

技术分享图片
package com.lhx.spring.springboot_web_ext.servlet;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class EchoFilter implements Filter {

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) arg0;
        System.out.println("test" + req.getRequestURI());
        arg2.doFilter(arg0, arg1);

    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub

    }

}
View Code

在配置类,ServletConfiguration.java增加

技术分享图片
    @Bean
    public FilterRegistrationBean createFilterRegistrationBean() {
        FilterRegistrationBean filter = new FilterRegistrationBean();
        filter.setFilter(new EchoFilter());
        filter.setUrlPatterns(Arrays.asList("/book.do"));
        return filter;
    }
View Code

3.3、使用Listner

增加StartupListener .java

技术分享图片
package com.lhx.spring.springboot_web_ext.servlet;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class StartupListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println("=================");
    }

}
View Code

在配置类,ServletConfiguration.java增加

技术分享图片
    @Bean
    public ServletListenerRegistrationBean<StartupListener> createServletListenerRegistrationBean() {
        ServletListenerRegistrationBean<StartupListener> registrationBean = new ServletListenerRegistrationBean<StartupListener>(new StartupListener());
        return registrationBean;
    }
View Code

注:一般使用2中注解,因为Spring boot基于spring 4.x ,spring 4.x 开发的web一般使用Servlet3.x以上

 

以上是关于013-Spring Boot web静态资源ServletFilterlistenter的主要内容,如果未能解决你的问题,请参考以下文章

更改 Spring-boot 静态 Web 资源位置?

Spring Boot 的静态资源处理

Spring Boot + Spring Security + Thymeleaf 中的静态 Web 资源

Springboot 系列Spring Boot web 开发之静态资源和模版引擎

Spring Boot 2从入门到入坟 | Web场景开发篇:源码分析之静态资源配置原理

Spring Boot开发Web应用