2013-02-27 49 views
0

我想有兩個servlet(我使用碼頭)服務的URL是這樣的:路徑規格和多個servlet

host/aaa/submit 
host/bbb/submit 

我已經試過分別servlet的pathspecs設置aaabbb,但我沿着

two controller methods annotated with @RequestMapping(value = {"/submit"}) 

(即使所討論的兩種方法在由兩個不同的servlet使用兩個單獨的控制器類中定義)的線得到一個異常。
如果我將servlets的pathspecs設置爲/並將@RequestMappings更改爲aaa/submitbbb/submit,我得到404s。 (我想,這並不令人驚訝 - 不知道它應該如何有效地使用兩個'默認'servlet)

我應該如何映射這些URL? (先發制人 - 他們必須是獨立的servlet - aaa部分應該有或沒有DB工作,bbb部分應該失敗沒有DB)

以防萬一,這裏的碼頭方面:

<property name="servletHandler"> 
     <bean class="org.mortbay.jetty.servlet.ServletHandler"> 
      <property name="servlets"> 
       <list> 
        <bean name="aaaServlet" class="org.mortbay.jetty.servlet.ServletHolder"> 
         <property name="name" value="aaa" /> 
         <property name="servlet"> 
          <bean class="org.springframework.web.servlet.DispatcherServlet" /> 
         </property> 
         <property name="initParameters"> 
          <map> 
           <entry key="contextConfigLocation" value="classpath:aaa-context.xml" /> 
          </map> 
         </property> 
        </bean> 
        <bean name="bbbServlet" class="org.mortbay.jetty.servlet.ServletHolder"> 
         <property name="name" value="bbb" /> 
         <property name="servlet"> 
          <bean class="org.springframework.web.servlet.DispatcherServlet" /> 
         </property> 
         <property name="initParameters"> 
          <map> 
           <entry key="contextConfigLocation" value="classpath:bbb-context.xml" /> 
          </map> 
         </property> 
        </bean> 
       </list> 
      </property> 
      <property name="servletMappings"> 
       <list> 
        <bean class="org.mortbay.jetty.servlet.ServletMapping"> 
         <property name="servletName" value="aaa" /> 
         <property name="pathSpec" value="/" /> 
        </bean> 
        <bean class="org.mortbay.jetty.servlet.ServletMapping"> 
         <property name="servletName" value="bbb" /> 
         <property name="pathSpec" value="/" /> 
        </bean> 
       </list> 
      </property> 
     </bean> 
    </property> 

而且兩個控制器看起來像這樣:

@Controller 
public class AaaController { 
    @RequestMapping(value = {"/aaa/submit"}, method = (RequestMethod.POST)) 
    public void handleAaaSubmitPostRequest(final HttpServletRequest request, 
             final HttpServletResponse response, 
             @RequestBody String body) throws IOException { 
} 

而且

@Controller 
public class BbbController { 
    @RequestMapping(value = {"/bbb/submit"}, method = (RequestMethod.POST)) 
    public void handleBbbSubmitPostRequest(final HttpServletRequest request, 
             final HttpServletResponse response, 
             @RequestBody String body) throws IOException { 
} 

回答

0

因爲你的控制器似乎是在不同的MVC的背景下,你需要改變你的控制器根據此:

@Controller 
public class AaaController { 
    @RequestMapping(value = {/*aaa*/"/submit"}, method = (RequestMethod.POST)) 
    public void handleAaaSubmitPostRequest(final HttpServletRequest request, 
             final HttpServletResponse response, 
             @RequestBody String body) throws IOException { 
} 

@Controller 
public class BbbController { 
    @RequestMapping(value = {/*bbb*/"/submit"}, method = (RequestMethod.POST)) 
    public void handleBbbSubmitPostRequest(final HttpServletRequest request, 
             final HttpServletResponse response, 
             @RequestBody String body) throws IOException { 
} 

這是因爲Spring MVC的總是使用路徑沒有servlet上下文。

+0

我假定你的意思是我也應該將servlet的路徑規格設置爲'/ aaa/*'和'/ bbb/*'。我在我的問題中涵蓋了這種情況 - 在啓動時我得到一個像這樣的異常:'不能將處理程序'bbbController'映射到URL路徑[/ submit]:已經有類型爲[class AaaController]的處理程序映射。「 – seminolas 2013-02-27 12:51:48

+0

然後,請確保你只能在不同的mvc上下文中訪問你的控制器,例如只有'aaa-context.xml'中的'AaaController'和''bbb-context.xml'中的'BbbController'。否則,我會建議使用帶有路徑「/」的單個上下文,並且您最初的@ @ RequestMapping解決方案將起作用。 – n1ckolas 2013-02-27 12:59:50

+0

順便說一下,在你當前的配置中,這樣的URL是可訪問的:'aaa/aaa/submit','aaa/bbb/submit','bbb/bbb/submit'和'bbb/aaa/submit',是不是這樣? – n1ckolas 2013-02-27 13:05:52