• SpringMVC——Controller如何将数据响应给客户端


    SpringMVC注解

    @ResponseBody:

    将Controller方法返回的对象通过适当的转换器(HttpMessageConverter)转换为指定的格式,
    并将其写入到response对象得body区,一般用于返回JSON数据或者XML数据,
    在使用此注解后,SpringMVC将不会再走视图处理器,而是直接将数据写入流中,效果等同于通过response对象输出指定格式的数据

    @QequestParam:

    绑定单个请求参数值
    三个参数:

    • value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;
    • required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将抛出异常;
    • defaultValue:默认值,表示如果请求中没有同名参数时的默认值,设置该参数时,自动将required设为false

    Controller将数据响应给客户端示例

    实体类Users:定义了Users类的四个属性:分别是id,name,birthday,email

    package com.mvc.beans;
    import com.fasterxml.jackson.annotation.JsonFormat;
    import lombok.Data;
    import java.util.Date;
    /**
     * @author Una
     * @date 2022/8/13 18:55
     * @description:
     */
    @Data
    public class Users {
        private Integer id;
        private String name;
        //日期格式的规范
        @JsonFormat(pattern = "yyyy年MM月dd日")
        private Date birthday;
        private String email;
        public Users() {
        }
        public Users(Integer id, String name, Date birthday, String email) {
            this.id = id;
            this.name = name;
            this.birthday = birthday;
            this.email = email;
        }
    }
    
    • 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

    表单文件:一个登录界面,包含姓名、密码和提交按钮

    <html>
    <head>
        <title>登录</title>
    </head>
    <style>
        div{
            width: 500px;
            margin: 50px auto;
        }
    </style>
    <body>
    <div>
        <h2>-------用户登录--------</h2>
        <br/><br/>
        <form action="login5" method="post">
            UserName:
            <input type="text" name="userName" />
            <br/><br/>
            PassWord:
            <input type="password" name="passWord"/>
            <br/><br/>
            <button type="submit">Login</button>
        </form>
    </div>
    </body>
    </html>
    
    • 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

    success.html
    成功界面

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>登陆成功!!!!!title>
    head>
    <body>
    <h1>登陆成功!!!!h1>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    register.jsp
    注册页面:包含登录名、密码、真实姓名;注册按钮

    <%--
      Created by IntelliJ IDEA.
      User: ThinkPad
      Date: 2022/8/13
      Time: 19:53
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>注册页面</title>
    </head>
    <body>
    <div style="text-align: center" >
        <form action="/reg" method="post">
            <table>
                <tr>
                    <td>登录名:</td>
                    <td>
                        <label>
                            <input type="text" name="loginName"/>
                        </label>
                    </td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td>
                        <label>
                            <input type="password" name="password"/>
                        </label>
                    </td>
                </tr>
                <tr>
                    <td>真实姓名:</td>
                    <td>
                        <label>
                            <input type="text" name="userName"/>
                        </label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <button type="submit">注册</button>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    </body>
    </html>
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    login.jsp
    登录界面:登录名和密码和登录按钮

    <%--
      Created by IntelliJ IDEA.
      User: ThinkPad
      Date: 2022/8/13
      Time: 19:55
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登录页面</title>
    </head>
    <body>
    <div style="width:600px;margin: 20px auto;">
        <form action="login" method="post">
            <table>
                <tr>
                    <td>登录名:</td>
                    <td>
                        <label>
                            <input type="text" name="loginName" />
                        </label>
                    </td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td>
                        <label>
                            <input type="password" name="password"/>
                        </label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <button type="submit">登录</button>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    </body>
    </html>
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    控制器文件:

    使用@ResponseBody的响应

    使用@ResponseBody时;SpringMVC将不会再走视图处理器,而是直接将返回的字符串str写入流中

    package com.mvc.controller;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.mvc.beans.Users;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author Una
     * @date 2022/8/13 18:58
     * @description:
     */
    
    @Controller
    public class UsersController {
        @PostMapping(value = "login5", produces = "text/html; charset=utf-8")
        @ResponseBody
        public String getUsers() throws ParseException, JsonProcessingException {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            List<Users> usersList=new ArrayList<>();
            Users users1=new Users(12323,"小明",sdf.parse("2002-12-15"),"22264522@qq.com");
            Users users2=new Users(12322,"小红",sdf.parse("2001-12-15"),"226522@qq.com");
            Users users3=new Users(12324,"小黄",sdf.parse("2008-12-15"),"2422@qq.com");
            Users users4=new Users(12325,"小谢",sdf.parse("2001-12-18"),"22422@qq.com");
            usersList.add(users1);
            usersList.add(users2);
            usersList.add(users3);
            usersList.add(users4);
            ObjectMapper om=new ObjectMapper();
            String str=om.writeValueAsString(usersList);//格式
            return str;
        }
    }
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    使用@ResponseBody的响应效果

    在这里插入图片描述
    在这里插入图片描述

    使用 @QequestParam的响应

    register方法使得填写信息后跳转到login登录界面;
    loginCheck方法使得如果输入的密码和姓名有匹配,则跳转到welcome页面;否则跳转到login界面
    在这里插入图片描述

    package com.mvc.controller;
    import com.mvc.beans.Admin;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author Una
     * @date 2022/8/13 19:27
     * @description:
     */
    
    @Controller
    public class AdminController {
        private List<Admin> adminList;
    
        public AdminController() {
            super();
            this.adminList = new ArrayList<>();
        }
        @GetMapping(value = "/register" )
        public String registerPage(){
            return "register";
        }
        //在地址栏输入/register跳转到register.html界面
    
        @PostMapping(value = "/reg")
        public String register(@RequestParam(value = "loginName") String loginName,
                               @RequestParam(value = "password") String password,
                               @RequestParam(value = "userName") String userName){
            Admin admin=new Admin();
            admin.setLoginName(loginName);
            admin.setPassword(password);
            admin.setUserName(userName);
    
            adminList.add(admin);
            return "login";
        }
        @PostMapping(value = "login")
        public String loginCheck(@RequestParam(value = "loginName") String loginName,
                                 @RequestParam(value = "password") String password,
                                 Model model){
            System.out.println("登录名:"+loginName);
            System.out.println("密码:"+password);
            for (Admin a:adminList){
                if (a.getLoginName().equals(loginName) && a.getPassword().equals(password)){
                    model.addAttribute("admin",a);
                    return "welcome";//如果有匹配,则跳转到welcome页面
                }
            }
            return "login";//如果没有匹配,则跳转到login页面
        }
    }
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    效果测试

    在地址栏输入register跳转到register.jsp页面:进行注册
    在这里插入图片描述
    注册完毕按注册按钮;跳转到login.jsp页面进行登录;
    在这里插入图片描述
    匹配成功跳转到success.html页面
    在这里插入图片描述

  • 相关阅读:
    【面经】讲一下BASE理论
    如何防止企业代码被抄袭?源代码加密软件来救援!
    如何看待 Three.js / WebGL 等前端 3D 技术?
    telnet无效指令,telnet找不到命令
    C# 实现电子签名
    bat脚本 创建计划任务 一分钟设置ntp同步周期为60s
    spark算子简单案例 - Python
    基于springboot实现人事管理系统项目【项目源码+论文说明】
    Ompal138+Spartan-6 FPGA开发板硬件数据说明书(上)
    java八股文面试[JVM]——如何打破双亲委派模型
  • 原文地址:https://blog.csdn.net/m0_50744075/article/details/126325410