美文网首页
SpringBoot 整合JaveEE、静态资源访问

SpringBoot 整合JaveEE、静态资源访问

作者: 末池桑 | 来源:发表于2019-12-08 17:19 被阅读0次

整合 Servlet、Filter、Listener 方式一

1. 配置 Maven 依赖

省略了部分不相关配置

    <!-- SpringBoot 父依赖,使用 SpringBoot 必须得添加的父依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
    </parent>
    <dependencies>
        <!-- SpringBoot的 MVC 启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
2. 编写 Servlet 、Filter、Listener类

这里使用 @WebServlet、 @WebFilter、@WebListener 三个注解来代替了 web.xml 配置文件,其中省略了部分不相关代码。

@WebServlet("/first")
public class FirstServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("FirstServlet测试成功!");
    }
}

@WebFilter("/first")
public class FirstFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            System.out.println("开始FirstFilter!");
            chain.doFilter(request,response);
            System.out.println("结束FirstFilter! ");
    }
}

@WebListener
public class FirstListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println("FirstListener初始化成功");
    }
}
3. 编写启动类

省略了部分不相关代码

@SpringBootApplication
@ServletComponentScan //在springBoot启动时会扫描 @WebServlet,并将该类实例化
public class Application1 {
    public static void main(String[] args) {
        SpringApplication.run(Application1.class,args);
    }
}
4.测试结果

整合 Servlet、Filter、Listener 方式二

1. Maven 配置

与方式一的配置相同。

2. 编写 Servlet 、Filter、Listener 类

写法也与方式一大致相同,只是去掉了 @WebServlet、 @WebFilter、@WebListener 三个注解,以 Servlet 类写法为例(省略了部分不相关代码):

public class SecondServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("SencondServlet测试成功!");
        super.doGet(req, resp);
    }
}
3. 编写启动类

使用 Java config 的方式注册 Servlet、Filter、Listener。

@SpringBootApplication
public class Application2 {
    public static void main(String[] args) {
        SpringApplication.run(Application2.class,args);
    }
    @Bean
    public ServletRegistrationBean getServletRegistrationBean(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
        bean.addUrlMappings("/second");
        return bean;
    }
    @Bean
    public FilterRegistrationBean getFilterRegistrationBean(){
        FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
        //bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
        bean.addUrlPatterns("/second");
        return bean;
    }
    @Bean
    public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
        ServletListenerRegistrationBean<SecondListener> bean= new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
        return bean;
    }
}
4. 测试结果

点击进入 GitHub 源码地址

SpringBoot 加载静态资源

1. Maven 配置文件和上面一样
2. 编写 SpringBoot 启动类
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
3. 创建静态资源目录

默认情况下,SpringBoot 会去下面这几个路径访问静态资源(客户端可直接访问到的资源):

# 当五个目录中有相同静态资源文件名时,按照优先级从上致下的顺序去加载资源。
classpath:/META‐INF/resources
classpath:/resources/
classpath:/static/
classpath:/public/
"/":当前项目的根

在 src/main/resource 创建如下图的目录:

注意:这里的 webapp 目录便是 "/" (项目的根目录),就是 JavaWeb 项目中的 WebContext 或者 WebRoot 目录。

4. 创建静态资源文件

在各个静态资源目录创建静态文件 index.html

5. 测试结果
6. 配置静态资源目录

SpringBoot 是约定大于配置的,有许多的默认配置,这里的静态资源目录也是默认的配置,我们也可以通过全局配置文件(application.properties)来重新配静态资源目录,修改配置内容如下图:

注:修改过后,之前的默认路径将全部失效,只会从这个重新配置的目录中加载静态资源。

点击进入 GitHub 源码地址

IDEA 无法访问 webapp 资源问题

之前使用 Eclipse 情况下一直都没有问题,能够访问到 webapp 目录下的静态资源,但是这次使用 Itellij IDEA 就发生了没法访问 webapp 目录下没法访问的情况,按照以下方法即可解决问题:

  1. File > Project structure .. > Modules ,找到相应的模块,设置webapp resource 目录。
  1. run > Edit Configurations... 重新配置启动类的启动方式,将 Working directory 改成如下图所示即可。

相关文章

网友评论

      本文标题:SpringBoot 整合JaveEE、静态资源访问

      本文链接:https://www.haomeiwen.com/subject/kijmgctx.html