2013-08-22 22 views
6

私は、これは私が欲しいもの

enter image description here春のMVC:リソースタグと404エラー

私のプロジェクト構造であるタグ<mvc:resources>

を使用しようとしているがjsのフォルダとCSSでJavaScriptにアクセスすることです
しかしスタイルフォルダに、私は私のディスパッチャ-servlet.xmlにこれを追加するたびに

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

私は、これはウェブでエラー404、実行するとプロジェクト(プロジェクトがlogin.jspをページであるlogin.htmにリダイレクトされます)

が、これは私のディスパッチャ-servlet.xmlコードです

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     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-3.1.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 

    <context:component-scan base-package="com.swcommodities.wsmill.controller"/> 

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

    <bean id="viewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
      p:prefix="/WEB-INF/" 
      p:suffix=".jsp" /> 
    <tx:annotation-driven/> 
    . 
    . 
    . 
</beans> 

ました.xmlの

<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>2</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>redirect.jsp</welcome-file> 
    </welcome-file-list> 

私はプロジェクトが正しく実行ライン<mvc:resources...を削除する理由を私は知らないが、プロジェクトにこれを追加するとき、私はすぐに404エラーが発生します。私はこれが現在のプロジェクトといくらか矛盾していると思う。

+0

有効な答えは正確です。あなたは "私はプロジェクトを実行するとエラー404が発生しました"と書いています - どのURLをリクエストしていますか?どのファイル(場所)を期待していますか? – Ralph

+0

ブラウザでどのURLにヒットしましたか?そして、あなたのbean設定ファイル –

+0

が必要です。プロジェクトを実行するとlogin.htmlにリダイレクトされます。これはlogin.jspページを意味します。 –

答えて

-1

リソースファイルは倍のjsとスタイルにあるので、あなたにマッピングを変更する必要があります。

<mvc:resources mapping="/js/**" location="/js/" /> 
<mvc:resources mapping="/styles/**" location="/styles/" /> 

とJSPでそれらをロードします。

<script type="text/javascript" src="/js/file.js"></script> 
<link type="text/css" href="/styles/file.css" /> 
1

あなたは、フルパスを設定する必要があります場所の属性は、次のようになります。

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

1つの別の注釈があります現在の設定では、悪意のあるユーザーがWEB-INF /の下のものにアクセスすることができます。これは明らかにあなたが望むものではないためです。

これが動作するかどうかを教えてください。

アイマン

+1

要求されたパスに 'WEB-INF'、' META-INF'が含まれていたり、有効なパスが ".."で始まっている場合、スプリング 'ResourceHttpRequestHandler'はリソースを返さないので、WEB-INFフォルダに関する注釈は間違っています! ( 'ResourceHttpRequestHandler.isValidPath(String)'で実装されています) – Ralph