如果执行的URL包含相应Controller的@RequestMapping中提到的单词,是否可以调用Controller?
这是我的代码
@Controller
@RequestMapping({"/employee","/nonemployee","/temp"})
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";
}
}
将以上代码与@RequestMapping({"/employee","/nonemployee","/temp"})
和@RequestMapping("/employees")
一起使用,我们可以调用以下网址来列出值:
http://localhost:8080/Spring/employee/employees
http://localhost:8080/Spring/nonemployee/employees
http://localhost:8080/Spring/temp/employees
仔细观察,我们可以在传递给RequestMapping的所有三个单词/值中看到匹配的单词 emp 。所以,我正在寻找的是用于执行Controller的方式,如果URL包含单词 emp 。
执行以下URL时,必须使用相同的方法(getEmployeeList()
)返回值列表,但不要将多个或所有值传递给RequestMapping Annotation:
http://localhost:8080/Spring/employee/employees
http://localhost:8080/Spring/nonemployee/employees
http://localhost:8080/Spring/temp/employees
http://localhost:8080/Spring/exempt/employees
http://localhost:8080/Spring/attempt/employees
答案 0 :(得分:1)
将请求映射更改为 -
@RequestMapping("/*emp*")
这应该适用于您想要做的事情。