All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.datalking.web.config.DefaultServletHandlerConfigurer Maven / Gradle / Ivy

The newest version!
package com.github.datalking.web.config;

import com.github.datalking.util.Assert;
import com.github.datalking.web.HttpRequestHandler;
import com.github.datalking.web.servlet.handler.AbstractHandlerMapping;
import com.github.datalking.web.servlet.handler.SimpleUrlHandlerMapping;
import com.github.datalking.web.support.DefaultServletHttpRequestHandler;

import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yaoo on 5/4/18
 */
public class DefaultServletHandlerConfigurer {

    private final ServletContext servletContext;

    private DefaultServletHttpRequestHandler handler;

    public DefaultServletHandlerConfigurer(ServletContext servletContext) {
        Assert.notNull(servletContext, "A ServletContext is required to configure default servlet handling");
        this.servletContext = servletContext;
    }

    public void enable() {
        enable(null);
    }

    /**
     * 启用 Web容器的default Servlet处理未匹配的url,常用于静态资源处理
     */
    public void enable(String defaultServletName) {

        handler = new DefaultServletHttpRequestHandler();

        handler.setDefaultServletName(defaultServletName);
        handler.setServletContext(servletContext);
    }

    protected AbstractHandlerMapping getHandlerMapping() {
        if (handler == null) {
            return null;
        }

        Map urlMap = new HashMap<>();
        urlMap.put("/**", handler);

        SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
        handlerMapping.setOrder(Integer.MAX_VALUE);
        handlerMapping.setUrlMap(urlMap);

        return handlerMapping;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy