支持自定義類似於”.do”, “.php”, “.aspx”這樣的擴展名, 或者不使用擴展名. 在不使用擴展名的情況下, 所有被識別為action的鏈接, 最後一個foward slash後不能再出現”.”號, 以免和常用的mime types混淆.
public class Dispatcher implements Filter {
private static Logger logger = Logger.getLogger(Dispatcher.class);
private String attribute = null;
private FilterConfig filterConfig = null;
private static String ext =SimpleConfig.getConfig("extension");
// Initialise the dispatcher filter rule
private static String regex;
static
{
if (ext==null || ext.length() == 0 || ext.length() == 1)
{
ext = "";
regex = ".*?/[^\\.]*?$";
}
else
{
if (ext.charAt(0)!='.') {ext = "."+ext;}
regex = ".*?\\."+ ext.substring(1) + "$";
}
}
public void init(FilterConfig filterConfig) throws ServletException
{
this.filterConfig = filterConfig;
this.attribute = filterConfig.getInitParameter("attribute");
}
public void destroy()
{
this.attribute = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
String servletPath = request.getServletPath();
if (servletPath.matches(regex))
{
long startTime = System.currentTimeMillis();
String name = servletPath.substring(1, servletPath.length() - ext.length()).replaceAll("/", ".");
ActionContext context = SimpleConfig.getActionContext(name);
if (context != null)
{
ActionContainer container = new ActionContainer(context, request, response);
container.process();
}
long stopTime = System.currentTimeMillis();
logger.info( "Dispatcher: " + servletPath + ": " + (stopTime - startTime) + " milliseconds");
}
else
{
chain.doFilter(req, res);
}
}
}