springboot使用addViewController减少控制器代码的编写

最近模仿了sping-sagan项目的代码,感觉不错就打算一一记录下来

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Service;

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

//静态文件统配类
@Service
public class StaticPagePathFinder {

    private ResourcePatternResolver resourcePatternResolver;

    @Autowired
    public StaticPagePathFinder(ResourcePatternResolver resourcePatternResolver){
        this.resourcePatternResolver = resourcePatternResolver;
    }

    public static class PagePaths{
        private String filePath;
        private String urlPath;

        public PagePaths(String urlPath,String filePath){
            this.filePath = filePath;  //实际的资源路径
            this.urlPath = urlPath;   //请求的资源路径
        }

        public String getFilePath() {  return this.filePath; }

        public String getUrlPath()  {  return this.urlPath;  }

        @Override
        public String toString() {
            return this.getUrlPath()+";"+this.getFilePath();
        }
    }

    public List<PagePaths> findPath()throws IOException{
       Resource baseResource =  resourcePatternResolver.getResource("classpath:/templates/pages");
       String baseUrl = baseResource.getURL().getPath();
       Resource[] resources = resourcePatternResolver.getResources("classpath:/templates/pages/**/*.html");
       List<PagePaths> list = new ArrayList<PagePaths>();
       for(Resource resource : resources){
           System.out.println(new PagePaths(buildRequestMapping(resource.getURL().getPath()),relativeFliePath(baseUrl,resource))+"hahaha");
           list.add(new PagePaths(buildRequestMapping(resource.getURL().getPath()),relativeFliePath(baseUrl,resource)));
       }
       return list;
    }

    private String relativeFliePath(String basePath,Resource resource)throws IOException{
        return resource.getURL().getPath().substring(basePath.length()).replace(".html","");
    }

    private String buildRequestMapping(String filePath) {
        return filePath.substring(filePath.lastIndexOf("/")).replace(".html","");
    }
}

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import com.theone.project.blogcommon.support.StaticPagePathFinder;

import java.io.IOException;

@Configuration
public class ViewConfig extends WebMvcConfigurerAdapter{

    @Autowired
    private StaticPagePathFinder staticPagePathFinder;

    /*
    addViewControllers可以方便的实现一个请求直接映射成视图,而无需书写controller
    registry.addViewController("请求路径").setViewName("请求页面文件路径")
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        try{
            for(StaticPagePathFinder.PagePaths pagePaths :staticPagePathFinder.findPath()){
                String urlPath = pagePaths.getUrlPath();
                System.out.println(pagePaths.getUrlPath());
                System.out.println(pagePaths.getFilePath());
                registry.addViewController(urlPath).setViewName("pages/"+pagePaths.getFilePath());
                if(!urlPath.isEmpty()){
                    registry.addViewController(urlPath).setViewName("pages/"+pagePaths.getFilePath());
                }
            }
        }catch(IOException e){
            throw new RuntimeException("Unable to locate static pages:"+e.getMessage(),e);
        }
    }
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值