我遇到了帶註釋的控制器和Spring MVC的奇怪問題。我正在嘗試使用Annotated控制器來處理Spring隨其文檔提供的示例MVC應用程序。我使用2.5版本。Spring MVC和帶註釋的控制器問題:
當我在指定類型級別@RequestMapping,我得到「HTTP錯誤:500 無適配器處理程序[控制器類名]:請問您的處理程序實現諸如控制器支持的接口
如果我包括它?該方法的水平,它工作正常進行的問題添加或刪除默認的手柄適配器上下文文件並沒有區別:
最後,我使出具有控制器級別@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試過了,問題是相似的。
謝謝。我真的瘋了。我遵循這個博客的想法,不知道它是如何工作的博客作者: http://cchweblog.wordpress.com/2009/06/20/developing-spring-mvc-application-annotation-based-controller-and -jpa/ 編輯:我理解在每種方法下的利益部分。事實上,即使是在課堂上使用單一方法也是一個好主意,誰知道我可能會在以後添加更多方法! – Shaw 2009-12-29 14:49:53
他的方法也有'@ RequestMapping'。 – skaffman 2009-12-29 14:51:15
啊!趕上..我真的錯過了..不知道我是如何忽略..再次感謝! – Shaw 2009-12-29 14:53:19