【问题标题】:Spring MVC and annotated controllers issue:Spring MVC 和带注释的控制器问题:
【发布时间】:2010-12-30 19:18:39
【问题描述】:

我在使用带注释的控制器和 Spring MVC 时遇到了一个奇怪的问题。我试图将带注释的控制器用于 Spring 随附其文档的示例 MVC 应用程序。我用的是2.5版本。

当我在类型级别指定 @RequestMapping 时,我得到“HTTP ERROR: 500 处理程序 [Controller 类名称] 没有适配器:您的处理程序是否实现了支持的接口,如 Controller?

如果我将它包含在方法级别中,它可以正常工作而不会出现问题。向上下文文件添加或删除默认句柄适配器没有区别:

最后,我在控制器级别使用@RequestMapping,在方法级别使用@RequestMapping,并且它起作用了。有谁知道可能是什么问题?

这里是示例代码:

这不起作用:

@Controller
@RequestMapping("/*")
public class InventoryController {
    protected final Log logger = LogFactory.getLog(getClass());

    @Autowired
    private ProductManager productManager;


    public ModelAndView inventoryHandler() {
        String now = (new java.util.Date()).toString();
        logger.info("returning hello view with " + now);
        Map<String, Object> myModel = new HashMap<String, Object>();
        myModel.put("now", now);
        myModel.put("products", this.productManager.getProducts());
        return new ModelAndView("hello", "model", myModel);
    }
}

这行得通:

@Controller

public class InventoryController {
    protected final Log logger = LogFactory.getLog(getClass());

    @Autowired
    private ProductManager productManager;

    @RequestMapping("/hello.htm")
    public ModelAndView inventoryHandler() {
        String now = (new java.util.Date()).toString();
        logger.info("returning hello view with " + now);
        Map<String, Object> myModel = new HashMap<String, Object>();
        myModel.put("now", now);
        myModel.put("products", this.productManager.getProducts());
        return new ModelAndView("hello", "model", myModel);
    }
}

这也有效:

@Controller
@RequestMapping("/*")
public class InventoryController {
    protected final Log logger = LogFactory.getLog(getClass());

    @Autowired
    private ProductManager productManager;

    @RequestMapping( method = RequestMethod.GET, value = "/hello.htm" )
    public ModelAndView inventoryHandler() {
        String now = (new java.util.Date()).toString();
        logger.info("returning hello view with " + now);
        Map<String, Object> myModel = new HashMap<String, Object>();
        myModel.put("now", now);
        myModel.put("products", this.productManager.getProducts());
        return new ModelAndView("hello", "model", myModel);
    }
}

任何想法,这里发生了什么?我做了很多搜索,但没有解决方案。我也试过2.5.6,问题类似。

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    您需要将@RequestMapping 放在方法上,因为将它放在类上并没有足够的信息 - Spring 需要知道调用哪个方法来处理请求。

    如果你的类只有一种方法,那么可以理解,你可能认为它会选择那个方法,但事实并非如此。

    请注意,带注释的控制器的好处之一是您可以在类中拥有任意数量的 @RequestMapping-annotated 方法。

    【讨论】:

    • 谢谢。我真的要疯了。我遵循了这个博客的想法,想知道它对博客作者是如何工作的:cchweblog.wordpress.com/2009/06/20/… 编辑:我理解在每种方法下都有它的好处部分。事实上,将它放在类中的单个方法上也是一个好主意,谁知道我以后可能会添加更多方法!
    • 他的方法也有@RequestMapping
    • 啊!好收获..我真的错过了..不知道我是怎么忽略的..再次感谢!
    【解决方案2】:

    这是因为第一个配置没有告诉类调用的 wich 方法。

    【讨论】:

      【解决方案3】:

      你不需要在类上使用@RequestMapping。这只是一种方便。您确实需要方法级别的 @RequestMapping 注释。

      鉴于此:

      @Controller
      public class InventoryController {
      ...
      @RequestMapping("/inventory/create/{id}")
      public void create(...){}
      
      @RequestMapping("/inventory/delete/{id}")
      public void delete(...){}
      ...
      

      您可以将 URI 的库存部分分解出来。

      @Controller
      @RequestMapping("/inventory")
      public class InventoryController {
      ...
      @RequestMapping("create/{id}")
      public void create(...){}
      
      @RequestMapping("delete/{id}")
      public void delete(...){}
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-21
        • 2015-01-15
        • 2011-06-09
        • 2011-03-12
        • 2011-12-24
        • 2010-09-14
        • 2010-10-27
        相关资源
        最近更新 更多