但是如果想讓 Spring 控管這類靜態資源,或者讓下載動態產生的 pdf 或者 csv 時可以使用正確的副檔名(pdf 或 csv),就得將這些 request 也轉到 Spring 控管。
先在 web.xml 裡使用「/」將所有 request 都導到 Spring MVC 裡。
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-app.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springMVC312</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-web.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC312</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
再到 spring-web.xml 裡設定 <mvc:resources />,可以設定多筆。
<?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-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 "> <mvc:resources location="/images/*" mapping="/images/**"/> <mvc:resources location="/style/*" mapping="/style/**"/> <mvc:resources location="/js/*" mapping="/js/**"/> </beans>最後就可以在 Console 裡看到啟動時設定的資訊。
八月 01, 2012 9:19:29 上午 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler 資訊: Mapped URL path [/images/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0' 八月 01, 2012 9:19:30 上午 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler 資訊: Mapped URL path [/style/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#1' 八月 01, 2012 9:19:30 上午 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler 資訊: Mapped URL path [/js/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#2'
2012/08/20 補充
因為將「/」都導到 Spring,所以在 Controller 裡要定義一個處理「/」的 RequestMapping,不然使用「http://localhost/」時會找不到頁面,即使在 web.xml 設定「welcome-file-list」也無用,因為所有 request 都導到 Spring 去了,進不到 index.jsp 裡,除非路徑明確指定「http://localhost/index.jsp」。
@RequestMapping(value = { "/", "/index" }) public String index(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException { // ... return "index"; }---
沒有留言:
張貼留言