
重定向和转发
ModelAndView
ServletAPI
@Controller
public class ModelTest1 {
@RequestMapping("/m1/t1")
public String test(HttpServletRequest request, HttpServletResponse response){
HttpSession session = request.getSession();
System.out.println(session.getId());
return "test";
}
}
SpringMVC
数据处理
处理提交数据
数据显示到前端
ModelAndView
public class ControllerTest1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","ControllerTest1");
modelAndView.setViewName("test");
return modelAndView;
}
}
Model
@Controller
@RequestMapping("/user")
public class UserController {
@GetMapping("/t1")
public String test(@RequestParam("username") String name, Model model) {
System.out.println("接收到前端的参数为:" + name);
model.addAttribute("msg",name);
return "test";
}
}
ModelMap
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/t3")
public String test3(ModelMap map){
map.addAttribute("msg","HELLO ModelMap");
return "test";
}
}
总结
过滤器解决乱码
Javaweb过滤器
SpringMVC过滤器
