Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update WebMvcConfiguration, 希望 mvc mapping 可配置 #6320

Closed
wants to merge 2 commits into from

Conversation

icoco
Copy link

@icoco icoco commented Jun 17, 2024

现有 mvc viewcontroller 定义固化:

registry.addViewController("/").setViewName("doc.html");

希望可配置,

此次更新的实现方案:

1)增加注解标示类: MvcMapping
2)增加 mvc 定义类: MvcDef
3)修改 WebMvcConfiguration.java

代码:

 /**
     * 方案一: 默认访问根路径跳转 doc.html页面 (swagger文档页面)
     * 方案二: 访问根路径改成跳转 index.html页面 (简化部署方案: 可以把前端打包直接放到项目的 webapp,上面的配置)
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //registry.addViewController("/").setViewName("doc.html");
       handleMappingViewControllers(registry);
    }

    @Bean
    @MvcMapping
    public Map defaultMvcMapping() {
        Map result = new HashMap();
        MvcDef root = MvcDef.create("/","doc.html");
        result.put(root.getPath(),root);
        return result;
    }

    private void handleMappingViewControllers(ViewControllerRegistry registry) {

        ApplicationContext context = SpringContextUtils.getApplicationContext();

        Map<String, Object> beanMaps = context.getBeansWithAnnotation(MvcMapping.class);

        if (null == beanMaps) {
            return;
        }
        HashMap<String,MvcDef> mvc = new HashMap();
        for (Object val : beanMaps.values()) {
            if (!(val instanceof Map)) {continue;}
            Map mvcMaps = (Map)val;
            for (Object def : mvcMaps.values()) {
                if (def instanceof MvcDef) {
                    handleOverrideMapping(mvc, (MvcDef)def);
                }
            }
        }

        for (MvcDef def : mvc.values()) {
           registry.addViewController(def.getPath()).setViewName(def.getView());
        }
    }
    private void handleOverrideMapping(HashMap mvc,MvcDef def){
        Object val = mvc.get(def.getPath());
        if (null == val){
            mvc.put(def.getPath(),def);
            return ;
        }
        MvcDef existDef = (MvcDef)val;
        if (existDef.getPriority()< def.getPriority()){
            mvc.put(def.getPath(),def);
        }
    }

usage

    @Bean
    @MvcMapping
    public Map indexMvcMapping() {
        Map result = new HashMap();
        MvcDef root = MvcDef.create("/","index.html",999);
        result.put(root.getPath(),root);
        return result;
    }

replace exist path '/' with new page

this way should easy extends the mvc mapping feature .

@icoco icoco closed this Jun 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant