Spring MVC @RequestMapping继承 [英] Spring MVC @RequestMapping Inheritance

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

问题描述

来自Struts2我习惯于在超类上声明 @Namespace 注释(或 package-info.java )继承类随后将获取其祖先的 @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]到处理程序
      'twitterController'

      org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/tweeter/updateStatus] onto handler 'twitterController'

      我可以启用哪个设置来扫描超类 @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 / tweeter / updateStatus

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

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

      这是预期的行为,因为你已经覆盖了你声明的原始 @RequestMapping AbstractController AbstractUserController

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

      实际上当你声明 AbstractUserController 它还覆盖了 @RequestMapping AbstractController 。它只是让你觉得 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继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆