最近模仿了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); } } }