2

我正在尝试使用 SpringMVC 创建一个简单的应用程序以用于学习目的。我想拥有这种格式的各种操作的网址

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

在 DispatcherServlet 的 servlet-mapping 中指定的 url-pattern 是

<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 错误与描述请求的资源()不可用)。

如何才能做到这一点 ?

此外,是否可以有多个 url 模式用于 servlet 映射,例如。*.html or *.do?

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

谢谢

4

3 回答 3

1

添加

@RequestMapping("/controllername")

在类声明之前。

于 2011-02-10T14:38:47.877 回答
0

你试过这个吗?

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

此外,@RequestMapping接受一个字符串数组以允许多个映射。

于 2011-02-10T14:31:43.527 回答
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.

有关详细信息,请参阅 http://www.infoq.com/articles/spring-2.5-ii-spring-mvcRemoving Class-Level Request Mappings上的章节

于 2011-03-06T16:32:22.953 回答