本文介绍了Spring MVC @RequestMapping 继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Struts2 我习惯于在超类(或 package-info.java)上声明 @Namespace 注释并且继承类随后会获取该值在其祖先的 @Namespace 注释中,并将其添加到 Action 的请求路径中.我现在正在尝试使用 @RequestMapping 注释在 Spring MVC 中做类似的事情(代码为简洁起见):

Coming from Struts2 I'm used to declaring @Namespace annotation on super classes (or package-info.java) and inheriting classes would subsequently pick up on the value in the @Namespace annotation of its ancestors and prepend it to the request path for the Action. I am now trying to do something similar in Spring MVC using @RequestMapping annotation as follows (code trimmed for brevity):

package au.test

@RequestMapping(value = "/")
public abstract class AbstractController {
    ...
}

au.test.user

@RequestMapping(value = "/user")
public abstract class AbstractUserController extends AbstractController {

    @RequestMapping(value = "/dashboard")
    public String dashboard() {
        ....
    }
}

au.test.user.twitter

@RequestMapping(value = "/twitter")
public abstract class AbstractTwitterController extends AbstractUserController {
    ...
}

public abstract class TwitterController extends AbstractTwitterController {

    @RequestMapping(value = "/updateStatus")
    public String updateStatus() {
        ....
    }
}

  • / 按预期工作
  • /user/dashboard 按预期工作
  • 然而,当我期望 /user/twitter/updateStatus 正常工作时,它并没有运行,并且检查日志我可以看到一个类似于以下内容的日志条目:
    • / works as expect
    • /user/dashboard works as expected
    • However when I would have expected /user/twitter/updateStatus to work it does not and checking the logs I can see a log entry which looks something like:
    • org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping- 将 URL 路径 [/tweeter/updateStatus] 映射到处理程序'推特控制器'

      是否有我可以启用的设置来扫描 @RequestMapping 注释的超类并构建正确的路径?

      Is there a setting I can enable that will scan the superclasses for @RequestMapping annotations and construct the correct path?

      我也认为在 package-info.java 中的包上定义 @RequestMapping 是非法的?

      Also I take it that defining @RequestMapping on a package in package-info.java is illegal?

      推荐答案

      下面基本上变成了 /tweeter/updateStatus 而不是 /user/推特/更新状态

      The following basically becomes /tweeter/updateStatus and not /user/tweeter/updateStatus

      public abstract class TwitterController extends AbstractTwitterController {
      
          @RequestMapping(value = "/updateStatus")
          public String updateStatus() {
              ....
          }
      }
      

      这是预期的行为,因为您已经覆盖了您在 AbstractControllerAbstractUserController 中声明的原始 @RequestMapping.

      That's the expected behavior since you've overriden the original @RequestMapping you've declared in the AbstractController and AbstractUserController.

      事实上,当您声明AbstractUserController 时,它也覆盖了AbstractController@RequestMapping.它只是让你错觉 AbstractController 中的/已经被继承了.

      In fact when you declared that AbstractUserController it also overriden the @RequestMapping for AbstractController. It just gives you the illusion that the / from the AbstractController has been inherited.

      有没有我可以启用的设置来扫描 @RequestMapping 注释的超类并构建正确的路径?"从来没听说过.

      "Is there a setting I can enable that will scan the superclasses for @RequestMapping annotations and construct the correct path?" Not that I know of.

      这篇关于Spring MVC @RequestMapping 继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 05:20