⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 handlermapping.java

📁 扩展spring地址方法映射
💻 JAVA
字号:
package org.ext.springframework.handler;

import org.springframework.core.io.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.BeansException;
import org.springframework.util.Assert;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver;
import org.springframework.web.servlet.mvc.Controller;
import org.dom4j.Element;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;

import java.util.Properties;
import java.util.Iterator;

public class HandlerMapping extends Properties implements BeanFactoryAware, InitializingBean {
    private BeanFactory beanFactory;
    private Resource configLocation;

    public void afterPropertiesSet() throws Exception {
        Assert.notNull(configLocation, "configLocation Required!");
        SAXReader reader = new SAXReader();
        Document doc = reader.read(configLocation.getURL());
        Element handlerMappings = doc.getRootElement();
        Iterator handlerMappingIterator = handlerMappings.elementIterator("handlerMapping");
        while (handlerMappingIterator.hasNext()) {
            Element handlerMapping = (Element) handlerMappingIterator.next();
            String beanName = handlerMapping.attributeValue("controller");
            generateMappings(handlerMapping, beanName);
        }
    }

    private void generateMappings(Element handlerMapping, String controllerName) {
        Controller controller = (Controller) beanFactory.getBean(controllerName);
        boolean multiActionController = controller instanceof PropertyMultiActionController;
        Iterator urlMappingIter = handlerMapping.elementIterator("urlMapping");
        Properties urlMethodMappings = multiActionController? new Properties(): null;
        while (urlMappingIter.hasNext()) {
            Element urlMapping = (Element) urlMappingIter.next();
            String url = urlMapping.attributeValue("url");
            put(url, controllerName);
            if (multiActionController) {
                String method = urlMapping.attributeValue("method");
                Assert.notNull(method, "method Required!");
                urlMethodMappings.put(url, method);
            }
        }
        if (multiActionController) {
            PropertiesMethodNameResolver methodNameResolver = new PropertiesMethodNameResolver();
            methodNameResolver.setMappings(urlMethodMappings);
            methodNameResolver.afterPropertiesSet();
            MultiActionController mulicationActionController = (MultiActionController) controller;
            mulicationActionController.setMethodNameResolver(methodNameResolver);    
        }
    }

    public void setConfigLocation(Resource configLocation) {
        this.configLocation = configLocation;
    }

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -