接受的参数为日期类型
如何接受日期类型格式的参数?
(1)接受单个日期参数
网页输出:
//后台接受参数为日期类型
@RequestMapping("toDate.do") public String toDate(Date date) { System.out.println(date); return "index"; }
@InitBinder public void initBinder(ServletRequestDataBinder binder){ //只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型 binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); }
(2)接受多个参数时
网页输出:
//接受多个参数 @RequestMapping("register.do") public String register(Users users) { System.out.println(users); return "index"; }
接受多个参数时直接在对应的实体类(bean)中加入
2.controller进行数据保存
2.1 数据保存到request作用域的方式.
jsp调用:${requestScope.name }
1.使用ModelAndView,那么该方法的返回类型必须是ModelAndView
//1.可保存到ModelAndView,那么方法的返回类型必须是ModelAndView @RequestMapping("index.do") public ModelAndView index() { ModelAndView mv=new ModelAndView("index"); mv.addObject("name","张三"); return mv; }
2.使用Model, 方法的返回值还是字符串类型。
//2.保存到Model中,方法的返回值还可以是字符串 @RequestMapping("index.do") public String index(Model model) { model.addAttribute("name","李四"); return "index"; }
3.使用Map.方法的返回值还是字符串类型
//3.保存到Map中 @RequestMapping("index.do") public String index(Map<String,Object> map) { map.put("name","王五"); return "index"; }
jsp调用:${sessionScope.name }
4.原始的HttpServletRequest对象保存
//4.原始的HttpServletRequest对象保存 @RequestMapping("index.do") public String index(HttpSession session) { session.setAttribute("name","田七"); return "index"; }
jsp调用:${applicationScope.name }
1.使用原始的HttpSession保存
//1.使用原始的HttpSession保存。 @RequestMapping("index.do") public String index(Model model,HttpSession session) { //session.getServletContext():得到application对象 session.getServletContext().setAttribute("name","我在application中"); return "index"; }
2.使用注解@SessionAttributes(name={key1,key2})
静态资源的映射关系。
静态资源可以正常的显示
需要在springmvc的配置文件中添加
4.Springmvc完成ajax功能
1.加入jackson的jar包. springmvc
2.在响应的方法上加上@ResponseBody :把java对象转化为json对象
3.方法的返回值可以是对象集合字符串。
4.如果ajax返回的为字符串,那么就会出现乱码。
修改:
来源:博客园
作者:丶我喜欢就行
链接:https://www.cnblogs.com/xg123/p/11456672.html