2011-02-10 64 views
2

我正在嘗試使用SpringMVC創建一個簡單的應用程序用於學習目的。我想爲各種動作的URL這種格式如何在SpringMVC中使用@RequestMapping實現特定的url格式

http://localhost:8080/appname/controllername/actionname.html 

的servlet的映射裏指定DispatcherServlet的URL模式是

<url-pattern>*.html</url-pattern> 

這裏是我的ContactController中

方法之一
@RequestMapping("list.html") 
public ModelAndView showContacts() {   
    ModelAndView modelandview = new ModelAndView("list"); 

    modelandview.addObject("message","Your contact book"); 

    return modelandview; 
} 

現在一切正常,當我瀏覽到,

http://localhost:8080/appname/list.html 

但是我想要的網址是,

http://localhost:8080/appname/contact/list.html 

我嘗試了該方法的使用最高@RequestMapping("/contact/list.html")但它不幫助(顯示404錯誤與描述所請求的資源()不可用)。

這怎麼辦?

此外,是否有可能有多個網址模式的servlet映射例如。 *.html or *.do

PS。我在Ubuntu桌面上使用apache-tomcat

謝謝

回答

0

你試過嗎?

@RequestMapping("/contact") 
public class ContactController 
{ 
    @RequestMapping("/list.html") 
    public ModelAndView showContacts() 
    { 
    // ... 
    } 
} 

此外,@RequestMapping接受允許多個映射的字符串數組。

+0

是的,這工作。謝謝! – naiquevin 2011-02-11 04:30:47

1

類聲明之前加入

@RequestMapping("/controllername") 

0

順便說一句,你可能會更簡化!

This is easily remedied by using an XML-configured strategy for matching URI paths to controller classes and @RequestMapping annotations for method-level mapping only. 

有關詳細信息,請參閱章Removing Class-Level Request Mappingshttp://www.infoq.com/articles/spring-2.5-ii-spring-mvc

相關問題