2016-09-23 18 views
0

静的リソース(CSSファイル)を提供しようとしています。 私はすでに静的リソースをSpring MVCで提供できません

@Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/"); 
    } 
} 

はそうTomcatのロガーは、タイプ[クラスorg.springframeworkのハンドラに

マップされたURLパス[/リソース/ **]をリソースへの正しいマッピングを表示する場所とハンドラを登録します.web.servlet.resource.ResourceHttpRequestHandler]

ブラウザがビューをレンダリングすると、インスペクタは静的リソースを取得しようとすると404エラーを表示します。

enter image description here

AppInitializer.java

@Configuration 
@ComponentScan("com.learning") 
@EnableWebMvc 
public class ApplicationInitializer extends WebMvcConfigurerAdapter implements WebApplicationInitializer { 

    private final Logger LOGGER = Logger.getLogger(ApplicationInitializer.class.getName()); 
    public static final String DISPATCHER_SERVLET_NAME = "dispatcher"; 

    @Autowired 
    private ApplicationContext applicationContext; 

    public ApplicationInitializer() { 
    } 

    //region Context Initialization Area 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     WebApplicationContext springContext = getSpringApplicationContext(); 
     MyDispatcherServlet dispatcherServlet = new MyDispatcherServlet(springContext); 

     servletContext.addListener(new ContextLoaderListener(springContext)); 
     servletContext.setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE)); 
     servletContext.getSessionCookieConfig().setHttpOnly(true); 

     ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME, dispatcherServlet); 
     dispatcher.addMapping("/"); 
     dispatcher.setLoadOnStartup(1); 

    } 

    private WebApplicationContext getSpringApplicationContext() { 
     AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 
     LOGGER.info(String.format("Registering springApplicationContext: %s", context)); 
     // Loads into container first 
     context.register(ApplicationInitializer.class); 
     LOGGER.info(String.format("Registration success of springApplicationContext: %s", context)); 
     return context; 
    } 
    //endregion 

    //region ViewResolver Region 
    @Bean 
    public ViewResolver viewResolver() { 
     //Runs after coontroller ends its execution. It receives the view name to be processed. 
     ThymeleafViewResolver resolver = new ThymeleafViewResolver(); 
     resolver.setTemplateEngine(templateEngine()); 
     resolver.setCharacterEncoding("UTF-8"); 
     return resolver; 
    } 

    @Bean 
    public TemplateEngine templateEngine() { 
     // Processes the template 
     SpringTemplateEngine engine = new SpringTemplateEngine(); 
     engine.setEnableSpringELCompiler(true); 
     engine.setTemplateResolver(templateResolver()); 
     return engine; 
    } 

    private ITemplateResolver templateResolver() { 
     //Resolves templates with provided preffix and suffix 
     SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver(); 
     resolver.setApplicationContext(applicationContext); 
     resolver.setPrefix("/WEB-INF/views/"); 
     resolver.setSuffix(".html"); 
     resolver.setTemplateMode(TemplateMode.HTML); 
     return resolver; 
    } 
    //endregion 

    //region ResourceHandler Region 
    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/"); 
    } 
    //endregion 
} 

Hello.html

h1 { 
 
    color: red; 
 
    text-align: center; 
 
}
<!DOCTYPE html> 
 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title></title> 
 
    <link rel="stylesheet" href="/resources/css/MyCss.css" th:href="@{/resources/css/MyCss.css}"/> 
 
</head> 
 
<body> 
 
    <h1 th:text="'Hello ' + ${name}">Hello World</h1> 
 
</body> 
 
</html>

これは、実行中のスニペット... Bなどのディスプレイになって私が言及したように、アプリはリソースを見つけてロードすることができません。

Log File

classpath

enter image description here

enter image description here

任意のヘルプ?

+0

デバッグまたはトレースにログを回し:

<link rel="stylesheet" href='<spring:url value="/resources/bootstrap/bootstrap.min.css"/>' type="text/css" /> 

はこのように、タグライブラリを追加することを忘れないでください:ここで

は私がbootstrap.min.cssをインポートするために使用する方法です私たちに春が吐き出すものを見せてください。 –

+0

完了、編集を確認してください。 @SotiriosDelimanolis –

+0

これらのログは_正常に完了した要求を示しています_。 –

答えて

1

http://localhost:8080/resources/css/MyCss.css

あなたはWebアプリケーションの名前が欠落しています

ご以内

http://localhost:8080/webapp_name/resources/css/MyCss.css

link rel="stylesheet" ...

は、より良いあなたのURLを解決するために、春のURLタグを使用してください。 、

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 
+0

私はビューにThymeleafと.htmlファイルを使用していますが、どのようにそれらのファイルでtaglibを参照できますか? あなたが指摘した方法でインポートを使用すると、ブラウザのインス​​ペクタで2http:// localhost:8080 /%3Cspring:url%20value =%22/resources/css/MyCss.css%22 /%このエラーが発生します。 3Eリソースを読み込めませんでした:サーバは404の状態で応答しました –

+0

私はThymeleafに精通していませんが、wikiからはサーブレットベースのものであり、JSPファイルは基本的にTomcatのJasperコンポーネントのサーブレットファイルです。 。このtaglibをJSPファイルにディレクティブとして追加するとします。 –

関連する問題