mvc的流程注释与请求映射示范 /*核心流程:浏览器-DispatcherServlet-HandlerMapping-HandlerAdapter-Controller-DispatcherServlet-ViewResoler-View-浏览器详解:--1.浏览器 请求 DispatcherServlet(核心Servlet)--2.Dispatch
/*核心流程:浏览器-DispatcherServlet-HandlerMapping-HandlerAdapter-Controller-DispatcherServlet-ViewResoler-View-浏览器 详解: -->1.浏览器 请求 DispatcherServlet(核心Servlet) -->2.DispatcherServlet 调用 HandlerMapping(映射处理类) -->3.HandlerMapping 调用 HandlerAdapter(处理适配器) -->4.HandlerAdapter 分配 到指定的 Controller -->5.Controller 处理 完数据后 返回到DispatcherServlet -->6.DispatcherServlet 根据 Controller返回结果找到 ViewResoler视图解析器 -->7.ViewResoler 找到 指定页面*/ @Controller @RequestMapping("/user") public class UserInfoController { //请求映射:@RequestMapping(value,method,params,headers) //1.对象请求 @RequestMapping(value="/test2") public String test2(UserInfo info){ System.out.println(info); return "index"; } //2.@RequestParam(value,required,defaultValue) //-->value 参数名 //-->required 是否为必须,默认true,必须有参数,无则抛异常 //-->defaultValue 默认值,有此属性则required=false @RequestMapping(value="/test1") public String test1(@RequestParam(value="abc",required=false,defaultValue="aaaaaa") String id){ System.out.println("有用户请求我"+id); return "index"; } //3.@PathVariable("userid"):获取路由参数列表值 //在地址栏直接输入/abc-参数1-参数2 @RequestMapping(value="/abc-{id}-{name}") public String test(@PathVariable("id")String id,@PathVariable("name")String name){ System.out.println("有用户请求我"+id); System.out.println("name1"+name); return "index"; } //4.ajax请求 使用@ResponseBody注解即可 @RequestMapping(value="/ajax",produces="text/plain;charset=utf-8") @ResponseBody public String ajax(String name,HttpServletResponse response){ System.out.println(name); System.out.println(response.getCharacterEncoding()); System.out.println(response.getContentType()); //返回值中文容易乱码,需要设置格式 return "我是AJAX返回的信息:"+name; } //5.参数也支持 实体类,HttpServletRequest,HttpServletResponse @RequestMapping(value="/test3") public String test3(HttpServletRequest request){ System.out.println(request.getParameter("name")); System.out.println(request.getParameter("pwd")); return "index"; } }