一静态资源
1.1webapp 默认支持静态资源
在 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 中使用 ServletConfiguration 配置不依赖 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 以上
来源: http://www.bubuko.com/infodetail-2490249.html