SpringBoot框架详解(三)Web组件
第三章Web组件
拦截器,Servlet,Filter
1. 拦截器
拦截器是springmvc中的一种对象,能拦截对controller的请求。
拦截器框架中有系统的拦截器,还可以自定义拦截器。实现对请求预先处理。
springmvc中实现自定义拦截器:
-
创建类实现springmvc框架的HandlerInterceptor接口。 接口有三个方法。
public interface HandlerInterceptor {
default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true;
}
default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
}
default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
}
}
-
在springmvc的配置文件中,声明拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.sunny.handler.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
springboot中注册拦截器:
-
实现连接器 package com.firewolf.web;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
System.out.println("执行了LoginInterceptor的preHandle");
return true;
}
}
-
注册拦截器 package com.firewolf.config;
import com.firewolf.web.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyAppConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
HandlerInterceptor interceptor= new LoginInterceptor();
String[] path={"/user/**"};
String[] excludePath={"/user/login"};
registry.addInterceptor(interceptor)
.addPathPatterns(path)
.excludePathPatterns();
}
}
-
controller @Controller
public class BootController {
@RequestMapping("/user/account")
@ResponseBody
public String userAccount(){
return "访问user/account地址";
}
@RequestMapping("/user/login")
@ResponseBody
public String userLogin(){
return "访问user/login地址";
}
}
2.servlet
在springboot框架中使用servlet对象。
-
创建servlet类。创建类继承HttpServlet。
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println("执行的是Servlet对象");
out.flush();
out.close();
}
}
-
注册servlet,让框架能找到servlet package com.fire.config;
import com.fire.web.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WebApplicationConfig {
@Bean
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean bean= new ServletRegistrationBean();
bean.setServlet(new MyServlet());
bean.addUrlMappings("/login","/test");
return bean;
}
}
3.过滤器Filter
Filter是Servlet规范中的过滤器,可以处理请求,对请求的参数,属性进行调整。常常在过滤器中处理字符编码。
在框架中使用过滤器:
第一种方式
-
创建自定义过滤器类。
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("执行了MyFilter.doFilter");
filterChain.doFilter(servletRequest,servletResponse);
}
}
-
注册Filter过滤器对象。 @Configuration
public class WebApplicationConfig {
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new MyFilter());
bean.addUrlPatterns("/user/*");
return bean;
}
}
4.字符集过滤器
CharacterEncodingFilter : 解决post请求中乱码的问题
在springmvc框架,在web.xml注册过滤器。配置它的属性。
使用步骤:
-
配置字符集过滤器 package com.firewolf.config;
@Configuration
public class WebSystemConfig {
@Bean
public ServletRegistrationBean servletRegistrationBean(){
MyServlet myServlet= new MyServlet();
ServletRegistrationBean reg = new ServletRegistrationBean(myServlet,"/myservlet");
return reg;
}
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean reg = new FilterRegistrationBean();
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("utf-8");
filter.setForceEncoding(true);
reg.setFilter(filter);
reg.addUrlPatterns("/*");
return reg;
}
}
-
修改application.properties文件,让自定义的过滤器起作用。 # SpringBoot中默认已经配置了CharacterEncodingFilter 编码默认ISO-8859-1
# enabled=false 关闭默认设置好的CharacterEncodingFilter过滤器。
server.servlet.encoding.enabled=false
第二种方式
修改application.properties文件
server.port=9001
server.servlet.context-path=/myboot
# 让系统的CharacterEncodingFilter生效
server.servlet.encoding.enabled=true
#指定使用的编码方式
server.servlet.encoding.charset=utf-8
#强制request,response都使用charset属性的值
server.servlet.encoding.force=true
|