2017-03-26 11 views
0

問題は一般的ですが、私は多くのソリューションを試しましたが、何も私のためには機能しません。 私は春にはかなり新しいので、私はいくつかのことを理解していないかもしれません。Java Spring-MVCアプリケーションは、CSSおよびJSファイルを送信しません。エラー404

私は次のファイル構造を持っている:ページ内の

Files structure of project

私のリンクは、このようThymeleafでテンプレート化されています

<link rel="stylesheet" th:href="@{css/bootstrap.css}" type="text/css"/> 

また、HTMLの頭の中で属性があります。

[OK]を、そして私の春-context.xmlには、次のとおりです。

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 
    <!--to pick up all annotation in the package--> 
    <context:component-scan base-package="langquiz"/> 

    <mvc:annotation-driven /> 
    <mvc:resources mapping="/css/**" location="/css/"/> 
</beans> 

私は(場所を書い= "/テンプレート/ CSS /" または場所= "CSS /" のように)マッピングにいくつかの操作を試してみましたまた、コンフィグレーションクラスでリソースハンドラを試しました。しかし何も役に立たない。

ありがとうございました!

UPD:

<mvc:resources mapping="/css/**" location="/templates/"/> 

<mvc:resources mapping="/css/**" location="/css/"/> 

を変更しても

<mvc:resources mapping="/css/**" location="/"/> 

を試みたが、まだ結果を持っていません。

+0

上のウェブサイトを参照する方法があるかどう

はまだ不思議だから、春のブートを使用していませんか? – chrylis

+0

@chrylisはい、それはSpring-MVC + ThymeLeafです – utkillr

答えて

1

使用

<mvc:resources mapping="/css/**" location="/templates/css/"/> 

またはリソースハンドラを追加し、それを作るためにするように変更

@Configuration 
public class WebConfig extends WebMvcConfigurerAdapter { 

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
    registry.addResourceHandler("/css/**").addResourceLocations("/templates/css/"); 
} 

}

1

あなたのマッピングがlocationがあなたのtemplatesディレクトリだけでなく、以下のように含めるべきでは正しくありません。

<mvc:resources mapping="/css/**" location="/templates/css/"/> 

あなたは、この上の春のドキュメントからhereを参照することができますし、例えば

/resources/から公共リソースディレクトリのURLパターンでリソースリクエストを送信するあなたが使用するウェブ アプリケーションのルートをithin:

<mvc:resources mapping="/resources/**" location="/public-resources/"/> 
+0

私はそれを変更しましたが、それでも動作しません – utkillr

+0

他に何が欠けていますか? – utkillr

+0

リソースを電子メールで受け取る場所を確認する機能はありますか? – utkillr

関連する問題