Spring MVC教程四
程序开发
2023-09-20 18:54:32
好的!本节我们介绍SpringMVC使用servlet原生api作为参数,SpringMVC处理模型数据。
SpringMVC使用servlet原生api作为参数
SpringMVC的Handler方法可以接受参数:
- HttpServletRequest;
- HttpServletResponse;
- HttpSession;
- java.security.Principal;
- Local;
- InputStream;
- OutputStream;
- Reader;
- Writer;
添加servlet依赖:
javax.servlet servlet-api 2.5
controller:
@RequestMapping("/testServletAPI")public void testServletAPI(HttpServletRequest request, HttpServletResponse response, Writer out) throws IOException {System.out.println("testServletAPI, "+request+" , "+response );out.write("hello SpringMVC");// return "success";}
html:
testServletAPI
结果:
处理数据模型ModelAndView
ModelAndView既包含视图信息,也包含模型数据信息。
controller:
@RequestMapping("/testModelAndView")public ModelAndView testModelAndView(){String viewName = "success";ModelAndView modelAndView=new ModelAndView(viewName);modelAndView.addObject("time",new Date());return modelAndView;}
index.html:
testModelAndView
success.html:
success
time:${requestScope.time}
Map
通过map传递数据
controller:
@RequestMapping("/testMap")public String testMap(Map map){map.put("names", Arrays.asList("tom","Jerry","Mike"));return "success";}
index.html:
testMap
success.html:
success
time:${requestScope.time}
names:${requestScope.names}
标签:
上一篇:
为什么要使用组件化???
下一篇:
相关文章
-
无相关信息