本文导读
- 本文承接《 Web 项目 tiger 之11 Servlet 容器配置修改》
- Spring Boot 由于默认以 Jar 包方式启动嵌入式的 Servlet 容器来启动 Spring Boot 的 web 应用,没有 web.xml 文件,所以无法再像以前一样通过在 web.xml 配置来使用 Servlet ,但是 Spring Boot 提供了其它的方式。
- 以前使用 Servlet ,第一步自定义类继承 javax.servlet.http.HttpServlet,然后重写其 doGet 与 doPost 方法,在方法中编写控制代码;第二步在 web.xml 中配置<servlet></servlet> 请求映射路径即可。
- Spring Boot 注册 Servlet:
- 第一步自定义类继承 HttpServlet ,然后重写其 doGet 与 doPost 方法,在方法中编写控制代码仍然需要
- 第二步将 ServletRegistrationBean 组件添加到 Spring 容器中
自定义 Servlet
- 如下所示,仍然和以前一样书写 Servlet 代码
package com.lct.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by Administrator on 2018/8/11 0011.
* 标准的 Servlet 实现 HttpServlet;
* 重新其 doGet 、doPost 方法
*/
public class BookServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(":com.lct.servlet.BookServlet:" + req.getRequestURL());
/**讲求转发到后台的 user/users 请求去,即会进入*/
req.getRequestDispatcher("user/users").forward(req, resp);
}
}
- 上面 Serlvet 就是转发到下面的 UserControllr 控制器中,这是以前写好的,在此不再累述
添加 ServletRegistrationBean
- @Configuration 配置类相当于以前的 beans.xml 中的配置,也是 tiger 项目之前写好的,现在将 ServletRegistrationBean 也添加到 Spring 容器中来
package com.lct.config;
import com.lct.component.MyLocaleResolve;
import com.lct.servlet.BookServlet;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
/**
* Created by Administrator on 2018/7/28 0028.
* 自定义配置类
*/
@Configuration
public class MyMvcConfig {
/**
* 将我们自己的 LocaleResolver 组件交于 Spring 容器管理
* 从而覆盖 Spring Boot默认的区域信息解析器
*
* @return
*/
@Bean
public LocaleResolver localeResolver() {
return new MyLocaleResolve();
}
/**
* 自定义嵌入式Servlet容器定制器组件
* 然后修改应用上下文路径以及 Tomcat 端口
*
* @return
*/
@Bean
public WebServerFactoryCustomizer webServerFactoryCustomizer() {
/**
* 使用 ConfigurableServletWebServerFactory
*/
return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>() {
@Override
public void customize(ConfigurableServletWebServerFactory configurableServletWebServerFactory) {
/**
* 修改应用上下文路径以及 Tomcat 端口
*/
configurableServletWebServerFactory.setContextPath("/cat");
configurableServletWebServerFactory.setPort(8084);
}
};
}
/**
* 注册 Servlet 三大组件 之 Servlet
* 添加 ServletRegistrationBean ,就相当于以前在 web.xml 中配置的 <servlet></servlet>标签
*/
@Bean
public ServletRegistrationBean myServlet() {
/**第二个参数是个不定参数组,可以配置映射多个请求
* 相当于以前在 web.xml中配置的 <servlet-mapptin></servlet-mapptin>*/
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new
BookServlet(), "/bookServlet");
return registrationBean;
}
}
运行测试
// 第一次请求 “bookServlet”
:com.lct.servlet.BookServlet:http://localhost:8084/cat/bookServlet
// bookServlet 中开始转发到 user/users,此时因为没有登录,所以被拦截器拦截重定向去了登录页
com.lct.component.LoginHandlerInterceptor ::: http://localhost:8084/cat/user/users
// 登录成功重定向到 user/users,此时拦截器放行
com.lct.component.LoginHandlerInterceptor ::: http://localhost:8084/cat/user/users
// 登录成功后 再次请求 bookServlet
:com.lct.servlet.BookServlet:http://localhost:8084/cat/bookServlet
// bookServlet转发到user/users,登录成功后拦截器放行
com.lct.component.LoginHandlerInterceptor ::: http://localhost:8084/cat/user/users
:com.lct.servlet.BookServlet:http://localhost:8084/cat/bookServlet
com.lct.component.LoginHandlerInterceptor ::: http://localhost:8084/cat/user/users