2

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

naiquevin
  • 7,588
  • 12
  • 53
  • 62

3 Answers3

1

Add

@RequestMapping("/controllername")

before class Declaration.

danny.lesnik
  • 18,479
  • 29
  • 135
  • 200
0

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.

OrangeDog
  • 36,653
  • 12
  • 122
  • 207
0

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

Slava Semushin
  • 14,904
  • 7
  • 53
  • 69