Spring MVC和带注释的控制器问题: [英] Spring MVC and annotated controllers issue:

查看:138
本文介绍了Spring MVC和带注释的控制器问题:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了带注释的控制器和Spring MVC的奇怪问题。我试图使用Annotated控制器为Spring提供的示例MVC应用程序及其文档。我使用的是2.5版本。

I'm having a strange issue with annotated controllers and Spring MVC. I was trying to use Annotated controllers for the sample MVC application that Spring ships with its documentation. I used 2.5 version.

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

When I specify @RequestMapping at type level, I get "HTTP ERROR: 500 No adapter for handler [Controller class name]: Does your handler implement a supported interface like Controller?

如果我将它包含在方法级别中,它可以解决问题。添加或删除默认句柄适配器上下文文件没有区别:

If I include it in the method level, it works fine with out an issue. Adding or removing the default handle adapters to context files made no difference:


最后,我在控制器上使用@RequestMapping级别,以及方法级别的一个,它工作。任何人都知道可能是什么问题?

Finally, I resorted to having @RequestMapping at controller level, as well as one at the Method level, and it worked. Anyone know what could be the issue?

以下是示例代码:

这不起作用:

@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,问题类似。

Any ideas, what's happening here? I did a lot of search and no solution. I also tried with 2.5.6, and the issue was similar.

推荐答案

你需要把 @对方法的RequestMapping 因为将它放在类上是不够的信息 - Spring需要知道调用哪个方法来处理请求。

You need to put @RequestMapping on the method because putting it on the class isn't enough information - Spring needs to know which method to call to handle the request.

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

If your class only has one method, then understandably you might think that it would pick that, but it doesn't.

考虑带注释控制器的好处之一是你可以在你的课程中拥有尽可能多的 @RequestMapping - 注释方法。

Consider that one of the benefits of annotated controllers is that you can have as many @RequestMapping-annotated methods in your class as you want.

这篇关于Spring MVC和带注释的控制器问题:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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