Spring MVC url映射

Spring MVC url映射,spring,jsp,spring-mvc,url-pattern,Spring,Jsp,Spring Mvc,Url Pattern,我用SpringMVC制作了一个简单的web应用程序 我想使用这些URL /使用者 /用户/{id} /用户/创建 /用户/编辑/{id} 在web.xml中 第一例 <servlet-mapping> <servlet-name>SpringMVC1</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> SpringMV

我用SpringMVC制作了一个简单的web应用程序

我想使用这些URL

  • /使用者
  • /用户/{id}
  • /用户/创建
  • /用户/编辑/{id}
在web.xml中

第一例

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

SpringMVC1
/ 
它工作得很好。
但是我不识字 -404错误
在{my project path}/WebContent/res/images/logo.png中

第二种情况

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

SpringMVC1
/* 
我可以在屏幕上看到图像 但是-404错误


怎么了???

您的XML中需要这样的内容:

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


请参见

在XML中需要类似的内容:

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

有关详细说明,请参见

在我的spring配置xml文件中

我附加

<mvc:resources mapping="/res/**" location="/path/to/your/resources"/>
并附加到xsi:schemaLocation

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
并附加mvc:注释驱动节点

<mvc:annotation-driven />

这是我的spring配置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>

它工作得很好。
谢谢肖恩·帕特里克·弗洛伊德。

更多详细说明

在我的spring配置xml文件中

我附加

<mvc:resources mapping="/res/**" location="/path/to/your/resources"/>
并附加到xsi:schemaLocation

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
并附加mvc:注释驱动节点

<mvc:annotation-driven />

这是我的spring配置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>

它工作得很好。
谢谢肖恩·帕特里克·弗洛伊德