0

I am new to spring mvc3 development and was facing a minor issue (which I was didn't face with ASP.Net MVC3). I want to know the process of defining a default (or landing) URL for a controller.

I have an accounts controller where I do all account management related stuff. So all my urls are mapped to this controller. I want to know that how can I map my "/accounts" url request to hit openAccountsDashboard method?

Code -

.... imports...

@Controller
@RequestMapping(value = "/accounts/*")
public class AccountController {

      @RequestMapping( value = "/", method = RequestMethod.GET)
      public ModelAndView openAccountsDashboard(HttpServletRequest request) {
           .....
           return new ModelAndView("accounts/landing");
      }

      @RequestMapping( value = "/change-password", method = RequestMethod.GET)
      public ModelAndView openPasswordChangePage(HttpServletRequest request) {
           .....
           return new ModelAndView("accounts/passwordChange");
      }
... other actions...

}

Any help would be great!

Thanks

saarthak
  • 1,004
  • 2
  • 12
  • 26

1 Answers1

0

Try something like this:

    .... imports...

@Controller
@RequestMapping(value = "/accounts/")
public class AccountController {

      @RequestMapping( value = "", method = RequestMethod.GET)
      public ModelAndView openAccountsDashboard(HttpServletRequest request) {
           .....
           return new ModelAndView("accounts/landing");
      }

      @RequestMapping( value = "/change-password", method = RequestMethod.GET)
      public ModelAndView openPasswordChangePage(HttpServletRequest request) {
           .....
           return new ModelAndView("accounts/passwordChange");
      }
... other actions...

}

Then you can use url like this:

http://localhost:8080/yourwebapp/accounts/

to hit openAccountsDashboard method. Regards, Arek

Arek Woźniak
  • 695
  • 3
  • 12
  • 25
  • it hits the method but only when I use url - '/accounts/' and not with '/accounts' (gives 404) – saarthak Aug 17 '12 at 07:12
  • 1
    see 13.11.3 static.springsource.org/spring/docs/2.5.4/reference/mvc.html – srikanth yaradla Aug 17 '12 at 07:37
  • But is that problem with url ends with '/' or not? I think this is logical, that we have some url prefix for specific controller and if you want to hit method from this controller - you should use whole prefix. If you want to hit this method by passing url like 'localhost:8080/youtwebapp/accounts', then you should map your whole controller to '/' and then map method openAccountsDashboard to 'accounts', which is not elegant (in my opinion), because this approach generate mishmash with controllers logics. – Arek Woźniak Aug 17 '12 at 07:49
  • 1
    if you map your whole controller to / all urls will be mapped to this controller which is unnecessary at class level you can just define /account and then at method level define the relative url from class like /password so that /account/password is mapped to method see this http://stackoverflow.com/questions/10425794/can-anybody-explain-me-difference-between-class-level-controller-and-method-leve – srikanth yaradla Aug 17 '12 at 08:54
  • @srikanthyaradla - thanks. this worked for me :) both /accounts and /accounts/ are working if I dont give requestmapping value attribute at method level and define class level mapping as /accounts – saarthak Aug 17 '12 at 09:27