1

以下は私の mvc-dispatcher.xml ファイルです

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

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

<mvc:annotation-driven />
<mvc:resources location="/static/" mapping="/static/**" />
<context:component-scan base-package="in.codejava.personal.controllers" />
</beans>

どこが間違っていますか?すべての static/* URL は、静的リソースの代わりに作成した 404 コントローラーによってマップされています。

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">

<display-name>Personal Web Blogs</display-name>
<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<!--    <servlet-mapping> -->
<!--        <servlet-name>default</servlet-name> -->
<!--        <url-pattern>/static/*</url-pattern> -->
<!--    </servlet-mapping> -->
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

コメントセクションを削除すると、正常に動作します。

4

2 に答える 2

5

パントを取り、次のように、JS、CSS、画像リソースに従来のディレクトリ構造を使用していると仮定します。

  • src/main/webapp/[js|css|images]

この場合、次のmvc:resourcesようになります。

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

そして、次のように JSP でそれらを参照する必要があります。"${pageContext.request.contextPath}/static/js/foo.js"

于 2012-08-14T00:48:41.043 に答える
0

web.xml に新しいサーブレット マッピングを追加するだけです。 <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/resources/*</url-pattern> </servlet-mapping>

次に、html のリソース パスを参照します。

<link rel="stylesheet" href="resources/css/style.css" type="text/css" media="screen">
于 2015-03-15T18:04:23.443 に答える