【问题标题】:Spring MVC: RequestMapping URL issue with ant-styleSpring MVC:蚂蚁风格的 RequestMapping URL 问题
【发布时间】:2018-05-29 01:37:49
【问题描述】:

Spring MVC:使用 ant 样式的 RequestMapping URL 问题

这是我的代码:

@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";
    }
}

当我运行我的 Tomcat 服务器时,在与 URL 映射相关的控制台部分中会打印以下行:

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'

我可以成功执行以下网址:

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

但是在尝试执行下面的 URL 时,我遇到了 404 页面:

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

但是使用以下 URL 成功执行了相同的页面:

http://localhost:8080/Spring/employees

为什么这种行为变化只针对一个特定的 URL? 如何使用相同的实现成功执行以下 URL:

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

【问题讨论】:

    标签: spring-mvc


    【解决方案1】:

    这是因为 emp 也存在于员工中。所以你的请求被映射到类上的 RequestMapping。

    将您的代码更改为以下 -

                    @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";
               }
         }
    

    这应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      相关资源
      最近更新 更多