【发布时间】: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