8

この質問が何度も出されていることは知っていますが、何が問題なのかわかりません。src/main/webapp フォルダーの下に images フォルダーがあります (これは Maven Web プロジェクトです)。src/main/webapp/WEBINF/views フォルダーに index.jsp があります。

次のように、画像やcssやjsなどの他のリソースにアクセスしようとしています:

<img src="/images/left_arrow.png" alt="" />

しかし、画像が表示されません。

ここに web.xml ファイルがあります

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

WEB-INF/mvc-dispatcher-servlet.xml ファイルは次のとおりです。

<beans xmlns="http://www.springframework.org/schema/beans"
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/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.ravi.WebApp" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

</beans>

これがコントローラー パッケージ com.ravi.WebApp です。

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

@RequestMapping("/")
public String printWelcome(Model model) {
    return "index";

}

}
4

6 に答える 6

11

次のリソース宣言を Spring 構成に追加してみてください。

<!-- Handles HTTP GET requests for /images/** by efficiently serving up static resources in the ${webappRoot}/images directory -->
<resources mapping="/images/**" location="/images/" />    

または、より一般的な方法は、resourcesすべてのリソース (画像、css、js など) を含むフォルダーをサブディレクトリに分割することです。

構成は次のようになります。

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

また、リソースは次のように参照されます。

<link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/screen.css" />" />
<script type="text/javascript" src="<c:url value="/resources/js/jquery-1.6.4.min.js" />"></script>
<img src="<c:url value="/resources/images/left_arrow.png" />" alt="" />
于 2012-10-11T19:49:50.070 に答える
2

注釈を使用する場合は、ユーザーに確認してください

<mvc:annotation-driven/>

リソース付き

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

それ以外の場合、注釈コントローラーは機能しません

于 2014-06-20T09:24:53.717 に答える
1

Spring MVC構成ファイルに画像フォルダーへの参照を追加するだけです

WEB-INF/spring-context.xml:

<mvc:resources mapping="/images/*" location="/images/" />
于 2013-06-09T16:58:54.253 に答える
0

静的リソースを Web ルートの WEB-INF フォルダー外に保持し、コンテナーで静的リソース要求を処理する場合は、これをアプリケーション コンテキスト ファイルに追加する必要があります。

<mvc:default-servlet-handler />

リソース マッピングを追加するという @BeauGrantham の提案も機能します。

于 2012-10-11T20:03:39.143 に答える
0

上記の提案は私にとってもうまくいきました。しかし、関連付けられた名前空間に問題がある場合は、mvc 部分を追加する必要がありました。mvc-dispatcher-serlvet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
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/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">

...

<mvc:annotation-driven /> 
<mvc:resources mapping="/images/**" location="/images/" />
于 2014-09-18T21:04:44.517 に答える