2012-04-08 20 views
1

我的控制器目前被映射爲類似http://example.com/fix.go的東西,當然,我認爲這是愚蠢的,想要更好的東西,如http://example.com/fixhttp://example.com/mmm/fix沒有擴展名。但是,當我嘗試配置它時,我無法使其工作。我顯然錯過了對整個映射理解的關鍵部分。我正在使用Spring 3.x,tomcat和控制器的註釋。如何配置spring控制器URL以使其沒有擴展名?

我的web.xml如下:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app 
    version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <servlet> 
     <servlet-name>BooBoo</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>BooBoo</servlet-name> 
     <url-pattern>*.*</url-pattern> <!-- was *.go when it worked --> 
    </servlet-mapping> 

    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 

</web-app> 

BOOBOO-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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     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"> 

    <context:component-scan base-package="com.foofoo.booboo"/> 

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
     <property name="prefix" value="/jsp/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 

</beans> 

我的控制器中的一個如此配置:

@Controller 
public class BangBangController extends BaseController { 

      // Used to be fix.go when it worked 
    @RequestMapping(value="fix", method=RequestMethod.GET) 
    public ModelAndView choose(
      HttpSession session, 
      @RequestParam(value="fixId", required=false, defaultValue="-1") Integer fixId, 
      @RequestParam(value="forkId", required=false, defaultValue="-1") Integer forkId 
    ) 
    throws Exception { ... } 
} 

我已經嘗試將web.xml中的映射更改爲/ mmm/*,期望http://example.com/mmm/fix之類的URL可以正常工作,但這也不起作用編輯。當我輸入我認爲瀏覽器中正確的網址時,我收到「缺少資源」錯誤。

我在這裏玩什麼?我缺乏什麼關鍵的理解?我試圖讓沒有擴展的東西在另一個項目上工作,也無法讓它去那裏。我明顯錯過了一些東西。

回答

1

問題是您的調度程序servlet與「。」匹配。在裏面。

將其更改爲

<url-pattern>/*</url-pattern> 

然而,這還糟糕,因爲你將不能夠提供靜態內容。

你真正應該做的是這樣的:

<url-pattern>/webapp/*</url-pattern> 

其中 「web應用」 是一些前綴。所有的網址都需要以它爲前綴,但這樣可以讓您仍然可以提供靜態內容。

+0

+1我還從來沒有見過**之前構建或記錄。 這是我認爲它應該工作,或多或少,但可悲的是,這不適用於我: 「所請求的資源(/ booboo/webapp/fix)不可用。」 – Marvo 2012-04-09 01:49:35

+0

我希望有一種方法,我可以通過調度員正在使決策步驟,當它決定它就是控制器會調用(或不調用)。我的意思是,短春建自己的。 – Marvo 2012-04-09 01:51:59

+0

@Marvo我的歉意,我錯了語法。我從內存中工作,我用一種不同的模式匹配器來混淆它。上述編輯應該可以工作。 – 2012-04-09 04:43:01

0

答案是,我沒有指定我在使用Spring MVC的東西,從而導致spring-servlet.xml文件氾濫。我讚賞克里斯在診斷問題上的幫助。我需要確認一些我嘗試過的東西(但因爲這個錯誤而失敗)是正確的。

我加

xmlns:mvc="http://www.springframework.org/schema/mvc" 

<mvc:annotation-driven /> 

該文件,並蓬勃發展,它開始工作。讓我撓頭的是:「沒有它,它是如何運作的?」因爲它適用於* .go模式。

下面是完整生成的文件:(至少不是在開發環境)對於爲什麼/ **是不是一個好主意的解釋

<?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: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/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     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"> 

    <context:component-scan base-package="com.barbar.foofoo"/> 

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

    <!-- 
    Declare View Resolver: when view 'view_name' is called (from the Controller), 
    the file '/jsp/view_name.jsp' will be used. 
    --> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
     <property name="prefix" value="/jsp/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 

</beans>