重写DispatcherServlet实现动态后缀。Spring-MVC

原创
2017/02/21 14:51
阅读数 4.3K

Spring mvc已经被我们玩坏了,还能怎么玩,像笔者这种闲着没事还蛋疼的无聊份子。想了这么一个花样。

我们知道,spring默认后缀do。也可以无后缀,有些人喜欢配置action。而笔者喜欢配置啥呢,asp。php。cgi。py。或者配置个sb。当然,纯粹为了好玩。

 

思路。spring分发器,即DispatcherServlet。实质是一个servlet。什么是servlet我就不去多说了,那么要实现任意后缀怎么办呢。凉拌。真凉拌,一个servlet你怎么实现。

然而,如果这个servlet变成了filter呢,那么不就好办了么。笔者坏坏的一笑。

首先自定义一个DiyDispatcherServlet,继承DispatcherServlet,实现Filter。

于是乎。初始化DispatcherServlet需要一个servletConfig的。而初始化Filter是一个FilterConfig

其实这两个东西性质差不多。基本方式也差不多,是两个接口,于是定义一个类,实现这两个玩意,做一个转换。

private static class ServletFilter  implements ServletConfig,FilterConfig{

		private FilterConfig filterConfig;
		
		public ServletFilter(FilterConfig filterConfig){
			this.filterConfig=filterConfig;
		}
		@Override
		public String getFilterName() {
			return filterConfig.getFilterName();
		}

		@Override
		public String getInitParameter(String s) {
			return filterConfig.getInitParameter(s);
		}

		@Override
		public Enumeration<String> getInitParameterNames() {
			return filterConfig.getInitParameterNames();
		}

		@Override
		public ServletContext getServletContext() {
			return filterConfig.getServletContext();
		}

		@Override
		public String getServletName() {
			return filterConfig.getFilterName();
		}

	}

 

来看看我们的DiyDispatcherServlet:

package com.blog.filter;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.DispatcherServlet;

import com.blog.comm.util.RequestUtil;
import com.blog.comm.util.SpringContextHelper;
import com.blog.service.SuffixService;

@SuppressWarnings("serial")
public class DiyDispatcherServlet extends DispatcherServlet implements Filter{

	public DiyDispatcherServlet(){
		super();
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req=(HttpServletRequest)request;
		HttpServletResponse res=(HttpServletResponse)response;
		SuffixService suffixService=SpringContextHelper.getBean(SuffixService.class);
		List<String> suffixs=suffixService.loadSpringSuffixs();
		List<String> staList = suffixService.loadStaSuffix();
		String suffix=RequestUtil.getURLSuffix(req);
		if (staList.contains(suffix)) {
			res.setHeader("Cache-Control", "max-age=600");
			chain.doFilter(req, res);
			return;
		}
		String defSuffix = suffixService.loadSpringDefaultSuffix();
		req.setAttribute("suffix", defSuffix);
		if(suffixs.contains(suffix)){
			service(req, res);
			return;
		}
		chain.doFilter(req, res);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		super.init(new ServletFilter(filterConfig));
	}
	
	private static class ServletFilter  implements ServletConfig,FilterConfig{

		private FilterConfig filterConfig;
		
		public ServletFilter(FilterConfig filterConfig){
			this.filterConfig=filterConfig;
		}
		@Override
		public String getFilterName() {
			return filterConfig.getFilterName();
		}

		@Override
		public String getInitParameter(String s) {
			return filterConfig.getInitParameter(s);
		}

		@Override
		public Enumeration<String> getInitParameterNames() {
			return filterConfig.getInitParameterNames();
		}

		@Override
		public ServletContext getServletContext() {
			return filterConfig.getServletContext();
		}

		@Override
		public String getServletName() {
			return filterConfig.getFilterName();
		}

	}

}

那么。List<String> suffixs=suffixService.loadSpringSuffixs();获得后缀。如果存在后缀,就进入spring mvc去。不存在,就chain.doFilter(req, res);

 

那么web.xml怎么配置呢。看下面

<filter>
		<filter-name>SpringMVC</filter-name>
		<filter-class>com.blog.filter.DiyDispatcherServlet</filter-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext.xml</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>SpringMVC</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 

 

于是乎:

 

是不是有点爽歪歪,有点羞涩啥的

 

JAVA交流群:218481849

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
2 收藏
0
分享
返回顶部
顶部