• SpringMVC识 乱码过滤+ajax请求+SpringMVC四种跳转方式和默认的参数类型以及日期处理


    书上说,天下没有不散的宴席;不要怕,书上还说了,人生何处不相逢


    前言

    昨天中秋,劳逸结合


    一、中文乱码解决方案

    配置过滤器:

    <!--    中文编码过滤器配置-->
        <filter>
            <filter-name>encode</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <!--
            参数配置
            private String encoding;
            private boolean forceRequestEncoding;
            private boolean forceResponseEncoding;
            -->
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceRequestEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
            <init-param>
                <param-name>forceResponseEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>encode</filter-name>
            <url-pattern>/*
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    二、action方法的返回值

    1. String:客户端资源的地址,自动拼接前缀和后缀,还可以屏蔽自动拼接字符串,可以指定返回的路径
    2. Object:返回json格式的对象。自动将对象或集合转为json,使用jackson工具进行转换,必须要添加jackson依赖。一般用于ajax请求
    3. void:无返回值,一般用于ajax请求
    4. 基本数据类型,用于ajax请求
    5. ModelAndView:返回数据和视图对象,现在用的很少

    三、完成ajax请求访问服务器,返回学生集合

    1. 添加jackson依赖
      <!--  添加jackson的依赖-->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.9.8</version>
        </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 在webapp目录下新建js目录,添加jQuery函数库
      直接去网上搜jquery-3.3.1再放到js目录中即可
    2. 在index.jsp页面上导入函数库
    <script type="text/javascript">
        function showStu() {
            //使用jQuery封装的ajax()方法发送请求
            $.ajax({
                url: "${pageContext.request.contextPath}/list.action",//获取发布的项目的根路径
                type: "get",
                dataType: "json",
                success: function (stuList) {
                    var s = "";
                    $.each(stuList, function (i, stu) {
                        s += stu.name + "----" + stu.age + "
    "
    ; }); //回显数据 $("#mydiv").html(s); } }); } </script>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    1. 在action上添加注解@ResponseBody,用来处理ajax请求
    package com.bjpowernode.controller;
    
    import com.bjpowernode.pojo.Student;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author Yangqing
     * @date 2022/9/11  19:53
     */
    @Controller  //使用@RestController注解类,可以不用@ResponseBody注解方法
    public class StudentListAction {
        @RequestMapping("/list")
        @ResponseBody //解析ajax请求,必须要在springmvc.xml文件中添加注解驱动
        public List<Student> list(){
            List<Student> list = new ArrayList<>();
            Student stu1 = new Student("张三",23);
            Student stu2 = new Student("李四",24);
            Student stu3 = new Student("王五",25);
            list.add(stu1);
            list.add(stu2);
            list.add(stu3);
    
            return list;//SpringMVC框架负责将集合转为json数组
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    1. 在springmvc.xml文件中添加注解驱动,它用来解析@ResponseBody注解
    <!--    必须要添加注解驱动,为了支持ajax请求的-->
        <mvc:annotation-driven></mvc:annotation-driven>
    
    • 1
    • 2

    四、SpringMVC的四种跳转方式

    请求转发是基于服务器端的跳转
    重定向是基于客户端的跳转
    (弹幕老哥们:)
    (请求转发与重定向,发起请求的对象不同,内连接是服务器转发的,而重定向是浏览器)
    (1.服务器内部转发2.客户端重定向)
    请求转发的地址栏是http://localhost:8080/one.action (接电话不挂断,给别人,别人来解决)
    而重定向的地址栏是http://localhost:8080/main.jsp (接电话,挂断,重新打,打给别人,别人来解决)
    四种跳转方式如下:
    在这里插入图片描述
    观察不同请求的地址栏,来理解请求与重定向
    关键字使用

        public static final String REDIRECT_URL_PREFIX = "redirect:";
        public static final String FORWARD_URL_PREFIX = "forward:";
    
    • 1
    • 2

    例如:

        @RequestMapping("/two")
        public String two(){
            System.out.println("这是请求转发action跳转....");
    
            //如果是return"/other.action"就相当于下一行
            //   /admin//other.action.jsp
    
            //forward:这组字符串可以屏蔽前缀和后缀的拼接.实现请求转发跳转
            return "forward:/other.action";//默认是请求转发,使用视图解析器拼接前缀后缀进行页面跳转
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    具体操作看代码

    五、SpringMVC默认的参数类型

    不需要去创建,直接拿来使用即可。

    1. HttpServletRequest
    2. HttpServletResponse
    3. HttpSession
    4. Model
    5. Map
    6. ModelMap
      注意:Map,Model,ModelMap和Request一样,都使用请求作用域进行数据转递。所以服务器端的跳转必须是请求转发(重定向只有HttpSession)

    去看代码:springmvcall/springmvc_004_jump

    六、日期处理

    1. 日期的提交处理
    • 单个日期处理:
      要使用注解@DateTimeFormat,此注解必须搭配springmvc.xml文件中的
    • 类中全局日期处理
      注册一个注解,用来解析本类中所有的日期类型,自动转换(甚至不需要在springmvc.xml添加
        @InitBinder
        public void initBinder(WebDataBinder dataBinder){
            //注册自定义编辑器
            dataBinder.registerCustomEditor(Date.class,new CustomDateEditor(sf,true));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 日期的显示处理

    在页面上显示好看的日期,必须使用JSTL
    步骤1. 添加依赖JSTL2.在页面上导入标签库 3.使用标签显示数据
    如果是单个日期对象,直接转为好看的格式化的字符串进行显示
    如果是list中的实体类对象的成员变量是日期类型,则必须使用JSTL进行显示
    首先导入依赖:

        <!--添加JSTL依赖-->
        <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在页面上导入标签库:

    <%--导入jstl核心标签库--%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%--导入jstl格式化标签库--%>
    <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    
    • 1
    • 2
    • 3
    • 4

    页面显示部分:

    <table width="800px" border="1">
        <tr>
            <th>姓名</th>
            <th>生日</th>
        </tr>
        <c:forEach items="${list}" var="stu">
            <tr>
                <td>${stu.name}</td>
                <td>
                        ${stu.birthday}-------<fmt:formatDate value="${stu.birthday}" pattern="yyyy-MM-dd"></fmt:formatDate>
                </td>
            </tr>
        </c:forEach>
    </table>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    关键的JSTL语句:

    <fmt:formatDate value="${stu.birthday}" pattern="yyyy-MM-dd"></fmt:formatDate>
    
    • 1

    看代码即可:springmvcall/springmvc_004_jump


    总结

    jackson
    ajax
    json

  • 相关阅读:
    初识MySQL
    Java Web学习笔记6——盒子模型
    RCNN系列网络的理解
    实施MES管理系统前,这三个问题要考虑好
    详解mfc140.dll文件,修复mfc140.dll缺失的多种方法
    C++——string的封装
    Windows Server操作系统概述
    为什么在线个人品牌对企业家至关重要
    数字滤波器和模拟滤波器(一)
    【愚公系列】2022年08月 Go教学课程 029-面向对象简介
  • 原文地址:https://blog.csdn.net/tcben/article/details/126805766