您好,登錄后才能下訂單哦!
怎么在springboot項目中訪問靜態(tài)資源?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
@Configuration public class SystemConfigurer implements WebMvcConfigurer { @Value("${jeewx.path.upload}") private String upLoadPath; //配置的文件的存儲位置如 “E://image” @Value("${spring.resource.static-locations}") private String staticLocations; //其他靜態(tài)資源位置,如js,css文件 /**默認(rèn)攔截器排除資源*/ private List<String> EXCLUDE_PATHS= Arrays.asList("/plug-in/**","/content/**","/upload/**","/system/*.do","/error"); @Autowired private LoginInterceptor loginInterceptor; @Autowired private AccessSignInterceptor accessSignInterceptor; @Value("${jeewx.interceptor.is-open}") private boolean isOpen; @Value("${jeewx.interceptor.excludeUrls.login-interceptor}") private String loginInterceptorExcludeUrls; @Value("${jeewx.interceptor.excludeUrls.access-sign-interceptor}") private String accessSignInterceptorExcludeUrls; /** * 靜態(tài)資源的配置 - 使得可以從磁盤中讀取 Html、圖片、視頻、音頻等 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations("file:" + upLoadPath + "//") .addResourceLocations(staticLocations.split(",")); } /** * 登錄驗證碼 */ @Bean @SuppressWarnings({ "rawtypes", "unchecked" }) public ServletRegistrationBean randCodeImageServlet() { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new RandCodeImageServlet(), "/randCodeImage"); return servletRegistrationBean; } /** * 攔截器(登錄 + 簽名) */ public void addInterceptors(InterceptorRegistry registry) { if(isOpen) { log.info("loginInterceptorExcludeUrls: "+loginInterceptorExcludeUrls); log.info("accessSignInterceptorExcludeUrls: "+accessSignInterceptorExcludeUrls); registry.addInterceptor(loginInterceptor).addPathPatterns("/**/back/**/*").excludePathPatterns(EXCLUDE_PATHS).excludePathPatterns(loginInterceptorExcludeUrls.split(",")); registry.addInterceptor(accessSignInterceptor).addPathPatterns("/**").excludePathPatterns("/**/back/**").excludePathPatterns(EXCLUDE_PATHS).excludePathPatterns(accessSignInterceptorExcludeUrls.split(",")); } } /** * 默認(rèn)跳轉(zhuǎn)登錄頁面 */ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("redirect:/system/login.do"); registry.setOrder(Ordered.HIGHEST_PRECEDENCE); } }
springboot一種全新的編程規(guī)范,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程,SpringBoot也是一個服務(wù)于框架的框架,服務(wù)范圍是簡化配置文件。
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。