2011-07-06 5 views
4

스프링 3.0.5를 사용하고 있습니다. 내 정적 자산은 내 웹 응용 프로그램의 루트에있는 "정적"폴더에 있습니다 (WEB-INF와 동일한 수준). "http://mydomain.com/context-path/static/some-asset"형식의 URL을 "정적"폴더에 매핑하는 방법은 무엇입니까? Spring 애플 리케이션에서 정적 자산을 어떻게 매핑합니까?

이 어떤 도움을 ... 나는 (내 web.xml 파일에서) 루트 컨텍스트에 매핑되는 뷰 리졸버를 가지고 있다는 사실에 의해

<!-- Declare a Spring MVC DispatcherServlet as usual --> 
<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext 
     instead of the default XmlWebApplicationContext --> 
    <init-param> 
     <param-name>contextClass</param-name> 
     <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

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

좋아, 덕분에 복잡 - 데이브

추 신 : mvc : resources를 추가해도 고통을 치유하지 못하는 것 같습니다. 의 Servlet.service() 서블릿 디스패처에 대한 예외를 javax.servlet.ServletException 던졌다 : 나는 SEVERE은 "... 내 parentContext.xml 파일에

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns="http://www.springframework.org/schema/beans" 
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"> 

<mvc:resources mapping="/static/**" location="/static/"/> 

을 추가하지만 예외를 가지고 핸들러 없음 어댑터를 [COM .myco.systems.leadsmonitor.web.controller.HomeController @ 6870c52d] : 처리기가 Controller와 같은 지원되는 인터페이스를 구현합니까? " 내 홈페이지 "/"를 방문했을 때.

<mvc:annotation-driven /> 

나는 그것이 왜 일어나는지 알고 있지만 때하지 않습니다

+0

parentcontext.xml은 무엇입니까? 대개 dispatcher-servlet.xml 파일에 넣습니다. – Bozho

답변

4

을 사용하는 것입니다 당신은 <mvc:resources />을 사용합니다. MVC 메커니즘의 중요한 일부분, 즉 제가 생각하기에 Controller와 RequestMapping 주석을 존중합니다. 나는 <mvc:annotation-driven /> 당신의 컴퓨터에 정말로, 정말로 그것을 필요로한다고 생각한다 (?).

행운을 빈다. 나는 그저 똑같은 문제를 겪었습니다. 내 최종 모습은 다음과 같습니다.

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <!-- Explicitly enables the Spring MVC @Controller programming model --> 
    <mvc:annotation-driven /> 

    <mvc:resources mapping="/static/**" location="/static/" /> 

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

    <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> 
</beans> 
4

가장 좋은 옵션은 같은 파일에이 라인을 가지고 있는지 확인 <mvc:resources />을 사용하는 것 외에도 <mvc:resources />

+0

mvc : resources를 추가 한 방법을 볼 수 있도록 내 답장을 편집했습니다. 그것을 추가하면 내 응용 프로그램이 완전히 손상되었습니다. 오류 메시지가 포함되었습니다. – Dave

+0

@ 컨트롤러에 대한 handlerAdapter 구성이 누락되었습니다. – Bozho

관련 문제