开发者

RequestMapping for controllers for different servlets

开发者 https://www.devze.com 2023-01-21 08:50 出处:网络
I have a problem with my spring controller/request mapping struture. In web.xml I have defined 2 dispatcher servlets, that map the following request paths:

I have a problem with my spring controller/request mapping struture. In web.xml I have defined 2 dispatcher servlets, that map the following request paths:

  1. Servlet: /pathA/*
  2. Servlet /pathB/*

All my controllers are defined in the package com.myproject.controllers, so both controllers serving for paths under /pathA/* and /pathB/*. I am doing a component-scan in both of my servlets. How do I need to set the ReuestMapping annotations for the following Controller:

@Controller
public class MyController {

  // /pathA
  public void action1() {
  }
开发者_开发问答
  // /pathA/action2
  public void action2() {
  }
}

I really get confused here, i have tried so many different things, I hope you can help me!

Sincerely, Heinrich


If you actually need to use several DispatcherServlet, perhaps the best approach is to place their controllers into separate packages and limit component scan of each servlet to its own package.

If you can't do it for some reason, you can configure your servlets as follows:

<bean class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name = "alwaysUseFullPath" value = "true" />
</bean>

In this case you can use servlet paths in @RequestMapping, as stas showed.


try smt like this

 @RequestMapping(method = RequestMethod.GET, value = "/pathA")
 @RequestMapping(method = RequestMethod.GET, value = "/pathA/bla-bla")

if it wouldn't help there is some variant with urlrewriters.

0

精彩评论

暂无评论...
验证码 换一张
取 消