常用web组件
使用思想
- 创建自定义类
- 实现或者继承框架里的接口或类
- 将自定义类注册到框架中
- 使用自定义类
拦截器
说明
- 拦截器是SpringMVC中的一种对象,能拦截对Controller的请求
- 拦截器框架中有系统的拦截器,还可以自定义拦截器,实现对请求的预先处理
自定义拦截器
- 创建自定义的拦截器类实现SpringMVC框架的HandlerInterceptor接口,根据对拦截时机的需求,重写接口里的对应方法
package com.example.web.interceptor;
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.example.web.config;
import com.example.web.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
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) {
LoginInterceptor loginInterceptor = new LoginInterceptor();
String[] interceptorUrl = {"/user/**"};
String[] excludeUrl = {"/user/login"};
registry.addInterceptor(loginInterceptor)
.addPathPatterns(interceptorUrl)
.excludePathPatterns(excludeUrl);
}
}
package com.example.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class InterceptorTest {
@RequestMapping("/user/account")
@ResponseBody
public String account(){
return "访问账户页面";
}
@RequestMapping("/user/login")
@ResponseBody
public String login(){
return "访问用户登录页面";
}
}
Servlet
package com.example.web.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
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("--- springboot的servlet被调用 ---");
out.flush();
out.close();
}
}
package com.example.web.config;
import com.example.web.servlet.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyWebConfig {
@Bean
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new MyServlet());
servletRegistrationBean.addUrlMappings("/servlet1.do", "/servlet2.do");
return servletRegistrationBean;
}
}
过滤器
说明
- Filter是Servlet规范中的过滤器,可以处理请求,对请求的参数,属性进行调整。常常在过滤器中处理字符编码
使用自定义过滤器
package com.example.web.filter;
import javax.servlet.*;
import java.io.IOException;
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletResponse.setContentType("text/html;charset=utf-8");
System.out.println("MyFilter的doFilter方法被调用");
filterChain.doFilter(servletRequest, servletResponse);
}
}
package com.example.web.config;
import com.example.web.filter.MyFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyFilterConfig {
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new MyFilter());
filterRegistrationBean.addUrlPatterns("/user/*");
return filterRegistrationBean;
}
}
使用自定义字符集过滤器
package com.example.web.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
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");
PrintWriter out = resp.getWriter();
out.println("使用springboot字符过滤器");
out.flush();
out.close();
}
}
package com.example.web.config;
import com.example.web.servlet.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
@Configuration
public class MyWebConfig {
@Bean
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new MyServlet());
servletRegistrationBean.addUrlMappings("/test");
return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean filterRegistrationBean(){
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("utf-8");
characterEncodingFilter.setForceEncoding(true);
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(characterEncodingFilter);
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
}
- 修改application.properties文件,让自定义的过滤器起作用
server.servlet.encoding.enabled=false
使用框架内置的字符集过滤器
package com.example.web.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
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");
PrintWriter out = resp.getWriter();
out.println("**** 使用springboot字符过滤器 ****");
out.flush();
out.close();
}
}
package com.example.web.config;
import com.example.web.servlet.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyWebConfig {
@Bean
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new MyServlet());
servletRegistrationBean.addUrlMappings("/test");
return servletRegistrationBean;
}
}
- 修改application.properties文件,让框架内置的字符集过滤器实现个性化定制
server.servlet.encoding.enabled=true
server.servlet.encoding.charset=utf-8
server.servlet.encoding.force=true