路径规范和多个servlet

时间:2013-02-27 10:48:46

标签: servlets spring-mvc jetty

我想要两个servlet(我正在使用Jetty)提供这样的URL:

host/aaa/submit  
host/bbb/submit

我已经尝试将servlets的pathspecs分别设置为aaabbb,但后来我得到了一个例外

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

(即使这两个方法是在两个不同的servlet使用的两个独立的控制器类中定义的) 如果相反我将两个servlet的pathspecs设置为/并将@RequestMappings更改为aaa/submitbbb/submit,我将获得404。 (我想,这不太令人惊讶 - 不确定如何有效地使用两个'默认'servlet)

我应该如何映射这些网址? (先发制人 - 他们需要成为单独的servlet - aaa部分应该使用或不使用DB,bbb部分应该在没有DB的情况下失败)

以防万一,这是Jetty背景:

    <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 {
}

1 个答案:

答案 0 :(得分:0)

由于您的控制器似乎位于不同的mvc-contexts中,因此您需要根据此更改控制器:

@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上下文的路径。