Spring <mvc:annotation-driven>和</mvc:annotation-driven>

时间:2012-12-12 08:25:20

标签: spring model-view-controller freemarker

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

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

web/resources/css/Home_files
    -test.css

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

  • 没有<mvc:resources mapping="/resources/**" location="/resources/css/Home_files" />标记视图已解决,但图片ans css无法解析。
  • <mvc:annotation-driven />标记视图无法解析,但可以解析图像和CSS。

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

这是我的config 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>

1 个答案:

答案 0 :(得分:4)

<mvc:resources>标记中的路径似乎不正确。在位置末尾添加正斜杠(/)。

而不是

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

使用此:

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