• SpringMVC(二)


    1. 接受的参数为日期类型

    如何接受日期类型格式的参数?

    (1)接受单个日期参数

    网页输出:

    //后台接受参数为日期类型
        @RequestMapping("toDate.do")
        public String toDate(Date date) {
            System.out.println(date);
            return "index";
        }

    使用initBinds 加入到UsersController.java  当你接受的参数日期类型时先经过该方法进行处理

    @InitBinder
    
        public void initBinder(ServletRequestDataBinder binder){
    
            //只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型
    
            binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),  true));
    
        }

    (2)接受多个参数时

    网页输出:

    // 注册页面 register.jsp

    <%
    @ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="users/register.do?name=zs&age=25">链接到list.do</a> <form action="users/register.do" method="post"> 姓名:<input type="text" name="unames"/><br> 密码:<input type="text" name="password"/><br> 性别:<input type="text" name="sex"/><br> 年龄:<input type="text" name="age"/><br> 地址:<input type="text" name="address"/><br> 手机:<input type="text" name="phone"/><br> 出生日期:<input type="text" name="birthday"/><br> <input type="submit" value="提交"/> </form> </body> </html>
      //接受多个参数
    @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 }

    2.2 数据保存到session作用域的方式

      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})

    3. 静态资源的映射关系。

    静态资源可以正常的显示

     

    需要在springmvc的配置文件中添加

    4.Springmvc完成ajax功能

     

    1.加入jackson的jar包. springmvc

    2.在响应的方法上加上@ResponseBody :把java对象转化为json对象

    3.方法的返回值可以是对象集合字符串。

     

     

    4.如果ajax返回的为字符串,那么就会出现乱码。

     因为:

     

     

     

     

    修改:

     

  • 相关阅读:
    第2章 数据类型、运算符和表达式
    全国计算机等级考试二级教程(2021年版)C++语言程序设计 目录
    F# 中的异步快排
    Define a static property for F# class
    Get the memory address of Array in F#
    在递归中使用Continuation来避免StackOverflow(查找第K大的数)
    Details about constant value in C#( from CLR via C#)
    How to check whether an F# function/method has been initialized
    F#实现图及其部分操作
    Multi constructor for classes in F#
  • 原文地址:https://www.cnblogs.com/xg123/p/11456672.html
Copyright © 2020-2023  润新知