0

I am working on a Spring mvc application in which I have to display a list of locations. I have a controller method for location. Following is my controller method code:

@RequestMapping("/location")
public class LocationController {
    @RequestMapping(value = "/home")
    public String showAllLocations(ModelMap modelMap) {
        logger.info("showAllLocations() begins:");
        try {
            List<LocationModel> locationList = locationService
                    .getAllLocations("");
            modelMap.addAttribute("locationlist", locationList);
        } catch (Exception e) {
            logger.debug("Error while getting locations: " + e.getMessage());
            e.printStackTrace();
        }
        return "LocationHome";
    }
}

It works fine when I user following URL:

http://www.example.com:8080/myapp/location/home

But when I use http://www.example.com:8080/myapp/location, it shows error.

How can I view location list without using 'home', by following URL:

http://www.example.com:8080/myapp/location

2
  • have you tried adding @RequestMapping(value = "") Commented Feb 10, 2015 at 11:39
  • value can be an array. @RequestMapping(value = { "/home", "/" } ) Commented Feb 10, 2015 at 18:05

3 Answers 3

2
@RequestMapping(value = "/home")
public String doHome(ModelMap modelMap) {
   ...
}

@RequestMapping(value = "/**")
public String doDefault(ModelMap modelMap) {
   ...
}

Make sure you put more specific request handler before less specific one.

Sign up to request clarification or add additional context in comments.

Comments

0

@RequestMapping(value = "/", method = { RequestMethod.GET, RequestMethod.POST }) This can map you to a default handler.

Comments

0

You have the request mapping for the LocationController here ,

            @RequestMapping("/location")
            public class LocationController {..}

So all the URL to be intercepted by this controller should have the pattern /location followed by the method request mapping as here ,

        @RequestMapping(value = "/home")
        public String showAllLocations(ModelMap modelMap) {..}

If you need to intercept the request for http://www.example.com:8080/myapp/location . Just remove the url mapping from the controller and assign it to the method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.