2

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

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" /> 标签视图已解决,但图像和 css 无法解决。
  • 没有<mvc:annotation-driven /> 标签视图无法解析,但图像和 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>

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

代替

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

用这个:

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

1 回答 1

4

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

代替

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

用这个:

<mvc:resources mapping="/resources/**" location="/resources/css/Home_files/" />
于 2012-12-12T08:57:17.480 回答