ViV
ViV

Reputation: 317

Spring MVC: RequestMapping URL issue with ant-style

Spring MVC: RequestMapping URL issue with ant-style

This is my code:

@Controller
@RequestMapping("/*emp*")   // Ant Style
public class EmployeeController {

    @Autowired
    EmployeeService service;

    @RequestMapping("/add")
    public ModelAndView employee() {
        ModelAndView modelAndView = new ModelAndView("emp/add", "command", new Employee());
        return modelAndView;
    }

    @RequestMapping("/employees")
    public ModelAndView getEmployeeList() {
        ModelAndView modelAndView = new ModelAndView("/emp/employees", "list", service.getEmployeeList());
        return modelAndView;
    }

    @RequestMapping(value = "/create")
    public String createEmployee(@ModelAttribute Employee employee, ModelMap model) {
        service.newEmployee(employee);
        model.addAttribute("name", employee.getName());
        model.addAttribute("age", employee.getAge());
        model.addAttribute("id", employee.getId());
        return "/emp/create";
    }
}

When I run my Tomcat server, following lines are printed in the Console section related to URL mapping:

May 29, 2018 6:38:10 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/*emp*/add] onto handler 'employeeController'
May 29, 2018 6:38:10 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/*emp*/add.*] onto handler 'employeeController'
May 29, 2018 6:38:10 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/*emp*/add/] onto handler 'employeeController'
May 29, 2018 6:38:10 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/employees] onto handler 'employeeController'
May 29, 2018 6:38:10 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/employees.*] onto handler 'employeeController'
May 29, 2018 6:38:10 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/employees/] onto handler 'employeeController'
May 29, 2018 6:38:10 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/*emp*/employees/] onto handler 'employeeController'
May 29, 2018 6:38:10 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/*emp*/create] onto handler 'employeeController'
May 29, 2018 6:38:10 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/*emp*/create.*] onto handler 'employeeController'
May 29, 2018 6:38:10 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/*emp*/create/] onto handler 'employeeController'

I can successfully execute the following URL:

http://localhost:8080/Spring/emp/add
http://localhost:8080/Spring/emp/create

But while trying to execute the following URL, I encountered 404 page:

http://localhost:8080/Spring/emp/employees

But the same page is executed successfully using the following URL:

http://localhost:8080/Spring/employees

Why is this change of behaviour for only one particular URL? How can I execute the following URL successfully with the same implementation:

http://localhost:8080/Spring/emp/employees

Upvotes: 0

Views: 186

Answers (1)

Indraneel Bende
Indraneel Bende

Reputation: 3486

Its because emp is present in employees too. So your request is getting mapped to the RequestMapping on the Class.

Change your code to the following -

                @Controller
                @RequestMapping("/*emp*/")   // Ant Style
                public class EmployeeController {

               @Autowired
               EmployeeService service;

              @RequestMapping("add")
              public ModelAndView employee() {
              ModelAndView modelAndView = new ModelAndView("emp/add", "command", new Employee());
              return modelAndView;
             }

             @RequestMapping("employees")
             public ModelAndView getEmployeeList() {
             ModelAndView modelAndView = new ModelAndView("/emp/employees", "list", service.getEmployeeList());
             return modelAndView;
           }

           @RequestMapping(value = "create")
           public String createEmployee(@ModelAttribute Employee employee, ModelMap model) {
           service.newEmployee(employee);
           model.addAttribute("name", employee.getName());
           model.addAttribute("age", employee.getAge());
          model.addAttribute("id", employee.getId());
          return "/emp/create";
           }
     }

This, should do the trick.

Upvotes: 1

Related Questions