旅游网站开发文档,什么做网站的公司好,wordpress 头像 很慢,工信部官网查询系统查询手机目录
一.向 request 域对象共享数据
1.1使用ServletAPI向request域对象共享数据
1.2使用ModelAndView向request域对象共享数据
1.3使用Model向request域对象共享数据
1.4使用map向request域对象共享数据
1.5使用ModelMap向request域对象共享数据
二.Model、ModelMap、Ma…目录
一.向 request 域对象共享数据
1.1使用ServletAPI向request域对象共享数据
1.2使用ModelAndView向request域对象共享数据
1.3使用Model向request域对象共享数据
1.4使用map向request域对象共享数据
1.5使用ModelMap向request域对象共享数据
二.Model、ModelMap、Map的关系
三.向session域共享数据
四.向application域共享数据 一.向 request 域对象共享数据
1.1使用ServletAPI向request域对象共享数据
RequestMapping(/testServletAPI)
public String testServletAPI(HttpServletRequest request){request.setAttribute(testScope, hello,servletAPI);return success;
} 1.2使用ModelAndView向request域对象共享数据 ModelAndView对象在 SpringMVC 中是一个用于封装视图和模型数据的重要对象。我们可以在控制器方法中创建一个ModelAndView对象并且通过这个对象向 request 域对象共享数据。
RequestMapping(/testModelAndView)
public ModelAndView testModelAndView(){/*** ModelAndView有Model和View的功能* Model主要用于向请求域共享数据* View主要用于设置视图实现页面跳转*/ModelAndView mav new ModelAndView();//向请求域共享数据mav.addObject(testScope, hello,ModelAndView);//设置视图实现页面跳转mav.setViewName(success);return mav;
} 1.3使用Model向request域对象共享数据 在 SpringMVC 的控制器方法中我们可以接收一个Model对象作为参数并且通过这个对象向 request 域对象共享数据。
RequestMapping(/testModel)
public String testModel(Model model){model.addAttribute(testScope, hello,Model);return success;
} 1.4使用map向request域对象共享数据 我们也可以在控制器方法中接收一个Map对象作为参数并且通过这个对象向 request 域对象共享数据。
RequestMapping(/testMap)
public String testMap(MapString, Object map){map.put(testScope, hello,Map);return success;
} 1.5使用ModelMap向request域对象共享数据 ModelMap是一个扩展了LinkedHashMap的类它可以用于在控制器方法中向 request 域对象共享数据。
RequestMapping(/testModelMap)
public String testModelMap(ModelMap modelMap){modelMap.addAttribute(testScope, hello,ModelMap);return success;
} 二.Model、ModelMap、Map的关系
Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的
1Model是一个接口它定义了一组用于向视图传递数据的方法。这个接口就像是一个数据传递的规范为不同的实现提供了统一的标准。 2ModelMap是一个实现了Model接口的类它扩展了LinkedHashMap可以用于在控制器方法中向 request 域对象共享数据。这个类就像是一个数据传递的具体实现为数据的共享提供了实际的操作方法。 3Map是 Java 中的一个接口它定义了一组用于存储键值对的方法。在 SpringMVC 中我们可以使用Map对象作为参数来接收模型数据并将其传递给视图。这个接口就像是一个数据存储的通用容器为不同的数据类型提供了统一的存储方式。
public interface Model{}
public class ModelMap extends LinkedHashMapString, Object {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {} 三.向session域共享数据 在 SpringMVC 中我们可以通过注入HttpSession对象来向 session 域对象共享数据。
RequestMapping(/testSession)
public String testSession(HttpSession session){session.setAttribute(testSessionScope, hello,session);return success;
} 四.向application域共享数据 通过session对象获取application
RequestMapping(/testApplication)
public String testApplication(HttpSession session){ServletContext application session.getServletContext();application.setAttribute(testApplicationScope, hello,application);return success;
}