2013-02-27 47 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