【问题标题】:Spring <mvc:annotation-driven /> and <mvc:resources can't use together?Spring <mvc:annotation-driven /> 和 <mvc:resources 不能一起用?
【发布时间】:2012-11-29 21:53:08
【问题描述】:

我的应用程序的文件夹结构如下。

web/WEB-INF/templates/
    -home.ftl

web/resources/css/Home_files
    -test.css

同时使用 &lt;mvc:annotation-driven /&gt;&lt;mvc:resources mapping="/resources/**" location="/resources/css/Home_files" /&gt; 标记时,它无法解析视图 (http://localhost:8080/info/home/index.html)。

  • 没有&lt;mvc:resources mapping="/resources/**" location="/resources/css/Home_files" /&gt; 标记的视图已解析,但图像和css 无法解析。
  • 没有&lt;mvc:annotation-driven /&gt;标签的视图无法解析,但图片和css可以解析。

如何同时加载视图和静态内容?

这是我的配置 xml 文件和 homeController。

info-servlet.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: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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/cache
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        ">

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/templates/"/>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="cache" value="true"/>
    <property name="prefix" value=""/>
    <property name="suffix" value=".ftl"/>
</bean>

<context:component-scan base-package="com.test.web.controllers"/>

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

<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/css/Home_files" />

</beans>

控制器

@Controller

@RequestMapping("/home")

public class HomeController {

    @RequestMapping(value = "/index.html")

    public String getHome(@ModelAttribute("model") ModelMap model) {

        return "home";

    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd" >

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/info-servlet.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>info</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/info-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- The mapping for the default servlet -->
    <servlet-mapping>
        <servlet-name>info</servlet-name>
        <url-pattern>/</url-pattern>

    </servlet-mapping>

【问题讨论】:

    标签: spring model-view-controller freemarker


    【解决方案1】:

    &lt;mvc:resources&gt; 标签中的路径似乎不正确。在位置末尾添加正斜杠 (/)。

    代替

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

    使用这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 2018-02-15
      • 1970-01-01
      • 2012-02-19
      • 2015-05-05
      • 1970-01-01
      相关资源
      最近更新 更多