1

Spring mvc で簡単な Web アプリケーションを作成しました。

このURLを使いたい

  • /ユーザー
  • /ユーザーID}
  • /ユーザー/作成
  • /ユーザー/編集/{id}

web.xml で

最初のケース

<servlet-mapping> 
    <servlet-name>SpringMVC1</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

それはうまくいきます。しかし、 http://localhost:8080/res/images/image.png
を読み取れません - {my project path}/WebContent/res/images/logo.png の404 エラー

2番目のケース

<servlet-mapping> 
    <servlet-name>SpringMVC1</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

http://localhost:8080/res/images/image.pngで画像を見ることができます が、http://localhost:8080/user/create - 404 エラー

どうしたの??

4

2 に答える 2

5

XML には次のようなものが必要です。

<mvc:resources mapping="/res/**" location="/path/to/your/resources"/>

16.14.5を参照してください。リソースの提供の構成

于 2012-03-22T07:31:08.953 に答える
4

もっと詳しく説明..

私の春の設定xmlファイルで

私は追加します

<mvc:resources mapping="/res/**" location="/path/to/your/resources"/>

次に追加する必要があります..

ルートノードに追加 - Bean

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation に追加します

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

mvc:annotation-driven ノードを追加します。

<mvc:annotation-driven />

それは私の春の設定xmlです

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="com.test" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <mvc:annotation-driven />
    <mvc:resources mapping="/res/**" location="/res/" />
</beans>

それはうまくいきます。
ありがとう、ショーン・パトリック・フロイド。

于 2012-03-23T06:15:12.080 に答える