Controller两种配置
1.继承Controller接口
public class ControllerTest01 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
mv.addObject("msg","Controller");
mv.setViewName("test");
return mv;
}
}
2.注释@Controller
@Controller
public class ControllerTest02 {
@RequestMapping("/t1")
public String test02(Model model){
model.addAttribute("msg","Controller02");
return "test";
}
}
RequestMapping
可以放在类上或者方法上,放在方法上就是可以直接访问的地址,如果放在类上说明这个类中的方法都要先加上类的地址,然后再走方法上的地址。
@Controller
@RequestMapping("test")
public class ControllerTest02 {
@RequestMapping("/t1")
public String test02(Model model){
model.addAttribute("msg","Controller02");
return "test";
}
}
本文探讨了两种Controller实现方式:通过继承Controller接口和使用@Controller注解配合@RequestMapping。讲解了两者如何映射URL并区分方法直接访问与类路径的配置。
1169

被折叠的 条评论
为什么被折叠?



