2011-10-29 40 views
1

我正在研究映射所有請求的代理servlet。 我指定下列方式web.xml中:Spring MVC - 靜態資源的HTTP GET請求由自定義servlet處理

<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/admin/*</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/home</url-pattern> 
</servlet-mapping> 

<servlet> 
    <description>Servlet to proxy all requests.</description> 
    <display-name>ProxyServlet</display-name> 
    <servlet-name>ProxyServlet</servlet-name> 
    <servlet-class>com.epam.alpha.servlets.ProxyServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>ProxyServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<welcome-file-list> 
    <welcome-file>home</welcome-file> 
</welcome-file-list> 

這樣,所有的請求都被我的代理servlet處理,除/ home和/管理/ *保留用於主頁和後臺管理頁面。這很好!

現在,我想在我的jsp頁面中使用javascript和css文件。他們被放置在資源文件夾下,所以這些都在正確的地方。

在servlet的context.xml的下面給出:

<resources mapping="/resources/**" location="/resources/" /> 

的問題是,我的servlet捕獲所有的GET請求靜態資源。如果我註釋掉ProxyServlet的servlet映射,則會找到並使用這些資源,當然代理servlet在這種情況下不起作用。另一方面,如果我沒有從jsp引用JavaScript文件,則代理servlet將工作(不會請求靜態資源)。幫幫我!

+0

我不明白你的問題。你想用代理servlet做什麼?它實際上做了什麼? –

+0

有些映射到URL的別名,目標是訪問這些頁面,因爲它將在本地主機上運行。示例: - 別名「google」映射到http://www.google.com。 當我輸入http:// localhost:8080/ProxyServlet/google時,會看到google.com。這是目標。代理servlet處理上述請求並讀取響應並在瀏覽器中顯示結果。幾乎所有的請求都應該由這個代理servlet來處理(我們創建的),除了一些保留的別名,比如home或者admin,它們將用於維護目的。 – cslacika

+0

如果您的js和css文件應該由Spring的資源機制提供服務,那麼應該將/ resources路徑映射到Spring調度程序servlet,不是嗎?因此,將/ resources/*映射添加到appServlet應該可以解決所有問題。 –

回答

0

我已經能夠通過彈簧控制器以服務頁面,並使用類似的配置通過此頁面加載JS腳本:

  • 調度員的servlet
  • 一個servlet映射到/(在代理servlet)

我不得不

  • 路徑/資源映射/ *到dispatc她在web.xml的servlet(除了其他映射)
  • 使用下面的彈簧上下文文件:

servlet的context.xml中:

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

    <context:component-scan base-package="fr.free.jnizet.springtest"/> 
    <mvc:annotation-driven/> 
    <mvc:resources location="/resources/**" mapping="/resources"/> 
    <mvc:default-servlet-handler/> 
</beans> 

的關鍵點是所述<mvc:default-servlet-handler/>元件。沒有這個元素,Spring在/ resources /中找不到JS文件的映射。

+0

它的工作!我仍然不明白整個故事,爲什麼當我的代理servlet不在圖片中時,Spring發現了js映射,但無論如何。底部是這解決了問題!謝謝! – cslacika

+0

我覺得Spring沒有找到它。默認的servlet發現它,因爲JS的路徑是它的實際路徑。您只需要Spring資源映射來擊敗代理servlet「映射到所有內容」映射。 –

+0

哦,我想我明白了!再次感謝!! :) – cslacika