2012-12-12 31 views
2

我的應用程序的文件夾結構如下。Spring <mvc:annotation-driven />和<mvc:resources不能一起使用嗎?

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://本地主機:8080 /資訊/家庭/ index.html的)。

  • 沒有<mvc:resources mapping="/resources/**" location="/resources/css/Home_files" />標記視圖已解決,但圖像答覆無法解決。
  • 沒有<mvc:annotation-driven />標記視圖無法解析,但圖像和CSS可以解決。

我如何同時加載視圖和靜態內容?

這裏是我的配置xml文件和homeController。

信息-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> 

回答

4

看來,路徑是不正確的<mvc:resources>標籤。在位置末尾添加正斜槓(/)。

而不是

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

使用此:

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

謝謝viralpatel。這是工作 :) – userDuR

相關問題