如何在SpringMVC中使用@RequestMapping实现某种url格式

发布于 2024-10-16 13:17:56 字数 987 浏览 2 评论 0原文

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

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

在 DispatcherServlet 的 servlet 映射中指定的 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

但是我希望 url是的,

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

我尝试在该方法的顶部使用 @RequestMapping("/contact/list.html") 但它没有帮助(显示 404 错误,并描述了请求的资源 () 不可用)。

这怎么办?

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

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

谢谢

I am trying to create a simple application using SpringMVC for learning purpose. I want to have the urls for various actions in this format

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

The url-pattern specified inside the servlet-mapping for the DispatcherServlet is

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

here is one of my methods in the ContactController

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

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

    return modelandview;
}

Now everything works fine when i navigate to,

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

However I want the url to be,

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

I tried using @RequestMapping("/contact/list.html") on top of the method but it doesnt help (shows 404 error with description The requested resource () is not available) .

How can this be done ?

Also, is it possible to have multiple url-patterns for servlet mapping for eg. *.html or *.do ?

PS. i am using apache-tomcat on Ubuntu desktop

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

別甾虛僞 2024-10-23 13:17:56

添加

@RequestMapping("/controllername")

在类声明之前。

Add

@RequestMapping("/controllername")

before class Declaration.

自由如风 2024-10-23 13:17:56

你试过这个吗?

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

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

Have you tried this?

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

Also, @RequestMapping accepts an array of strings to allow multiple mappings.

豆芽 2024-10-23 13:17:56

顺便说一句,你可以进一步简化!

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-mvc

BTW, you may simplify even more!

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.

For details, see chapter Removing Class-Level Request Mappings at http://www.infoq.com/articles/spring-2.5-ii-spring-mvc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文