2009-12-29 103 views
1

我遇到了帶註釋的控制器和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試過了,問題是相似的。

回答

7

您需要在該方法上放置@RequestMapping,因爲將它放在類上是不夠的 - Spring需要知道調用哪個方法來處理請求。

如果你的課只有一種方法,那麼可以理解你可能認爲它會選擇,但它不會。

請注意,註釋控制器的好處之一就是您可以根據需要在您的課堂中擁有儘可能多的@RequestMapping -annotated方法。

+0

謝謝。我真的瘋了。我遵循這個博客的想法,不知道它是如何工作的博客作者: http://cchweblog.wordpress.com/2009/06/20/developing-spring-mvc-application-annotation-based-controller-and -jpa/ 編輯:我理解在每種方法下的利益部分。事實上,即使是在課堂上使用單一方法也是一個好主意,誰知道我可能會在以後添加更多方法! – Shaw 2009-12-29 14:49:53

+0

他的方法也有'@ RequestMapping'。 – skaffman 2009-12-29 14:51:15

+0

啊!趕上..我真的錯過了..不知道我是如何忽略..再次感謝! – Shaw 2009-12-29 14:53:19

1

這是因爲第一個配置並沒有告訴類方法調用的方法。

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(...){} 
... 
相關問題