【问题标题】:Path specs and multiple servlets路径规范和多个 servlet
【发布时间】:2013-02-13 03:29:58
【问题描述】:

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

host/aaa/submit  
host/bbb/submit

我尝试将 servlet 的路径规范分别设置为 aaa 和 bbb,但随后出现异常

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

(尽管所讨论的两种方法是在两个不同的控制器类中定义的,由两个不同的 servlet 使用)。
相反,如果我将两个 servlet 的路径规范都设置为 / 并将 @RequestMappings 更改为 aaa/submit 和 bbb/submit,我会得到 404s。 (我想这并不令人惊讶——不确定它应该如何有效地与两个“默认”servlet 一起工作)

我应该如何映射这些 URL? (先发制人 - 他们确实需要是单独的 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 {
}

【问题讨论】:

    标签: servlets spring-mvc jetty


    【解决方案1】:

    由于您的控制器似乎在不同的 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 上下文的路径。

    【讨论】:

    • 我假设您的意思是我还应该将 servlet 的路径规范设置为 /aaa/* 和 /bbb/*。我在我的问题中提到了这个案例——在启动时我得到一个这样的异常:Cannot map handler 'bbbController' to URL path [/submit]: There is already handler of type [class AaaController] mapped.
    • 那么请确保您的控制器只能在不同的 mvc 上下文中访问,例如仅限aaa-context.xml 中的AaaController 和bbb-context.xml 中的BbbController。否则,我建议使用带有路径“/”的单一上下文,您的初始 @RequestMapping 解决方案将起作用。
    • 顺便说一句,在您当前的配置中,可以访问这样的 URL:aaa/aaa/submit、aaa/bbb/submit、bbb/bbb/submit 和 bbb/aaa/submit,不是吗?
    猜你喜欢
    • 2017-11-12
    • 2016-08-21
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 2012-08-19
    • 1970-01-01
    相关资源
    最近更新 更多