• javaee SpringMVC中json的使用


    jsp

    <%--
      Created by IntelliJ IDEA.
      User: 呆萌老师:QQ:2398779723
      Date: 2019/12/6
      Time: 15:55
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%
        String baseurl=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
        pageContext.setAttribute("baseurl",baseurl);
    %>
    <html>
    <base href="${baseurl}" />
    <head>
        <title>Titletitle>
        <script src="${baseurl}/static/js/jquery1.11.3.min.js">script>
        <script src="${baseurl}/static/js/reg.js">script>
    head>
    <body>
    <from action="${baseurl}/users/reg" method="post">
        <pre>
        用户名:<input type="text" name="uname" /><span id="s1">span>
        密码:<input type="password" name="pwd" />
        确认密码:<input type="password" name="repwd" />
        专业:<select id="major" >
    
             select>
        <input type="submit" name="sub" value="注册" />
        pre>
    from>
    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

    js

    
    $(function(){
    
        //给文本框做失去焦点事件
        $(":text[name='uname']").blur(function(){
    
              //1.获得用户名
            var uname=$(this).val();
            //2.通过ajax将用户名传到服务器端 判断 将结果返回
            $.get("http://localhost:8080/TestSpringMVC4/users/checkUname?uname="+uname,function(msg){
    
                //3.处理返回的结果
                if(msg=="exists")
                    $("#s1").html("已存在");
                else
                    $("#s1").html("可以使用");
    
            })
        })
    
        //加载所有的专业信息
        $("#major").click(function(){
    
            //通过ajax到服务器端取回所有的专业信息
            $.getJSON("http://localhost:8080/TestSpringMVC4/users/getMajorList2",function(arr){
    
                //清除之前的option
                $("#major").empty();
    
                $.each(arr,function(k,v){
    
                    //返回的数据 组装成option添加到select中
                    var option=$("");
    
                    option.val(v.id);
    
                    option.html(v.name);
    
                    $("#major").append(option);
    
                })
    
            })
    
        })
    
    })
    
    • 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

    java

    package com.test.controller;
    
    import com.alibaba.fastjson.JSON;
    import com.test.pojo.Major;
    import com.test.pojo.Users;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    import java.util.ArrayList;
    import java.util.List;
    
    @Controller
    @RequestMapping("/users")
    public class UsersController {
    
        @RequestMapping("/loginUi")
        public String loginUi()
        {
            return "login";
        }
    
        @RequestMapping("/checkLogin")
        public String checkLogin(Users user, HttpServletRequest request)
        {
            if(user.getUname().equals("daimenglaoshi") && user.getPwd().equals("123"))
            {
                //存入session
                HttpSession session= request.getSession();
    
                session.setAttribute("loginUser",user);
    
                //重定向
                return "redirect:/index.jsp";
    
            }
            else
                return "login";
    
        }
    
        @RequestMapping("/regUi")
        public String regUi()
        {
            return "reg";
        }
    
        @RequestMapping("/checkUname")
        //返回响应体(响应的内容)  不做转发
        @ResponseBody
        public String checkUname(String uname)
        {
            if(uname.equals("daimenglaoshi"))
                return "exists";
            else
                return "not exists";
        }
    
        @RequestMapping("/getMajorList")
        @ResponseBody
        //框架会帮你转换成json字符串的
        public List<Major> getMajorList()
        {
    
            List<Major> majorList=new ArrayList<Major>();
    
            System.out.println("2222");
    
            majorList.add(new Major(1,"aaa"));
    
            majorList.add(new Major(2,"bb"));
    
            return majorList;
        }
    
        @RequestMapping(value="/getMajorList2",produces = "text/html;charset=utf-8")
        @ResponseBody
        public String getMajorList2()
        {
    
            List<Major> majorList=new ArrayList<Major>();
    
            majorList.add(new Major(1,"计算机"));
    
            majorList.add(new Major(2,"英语"));
    
            return JSON.toJSONString(majorList);
        }
    
    
    
    
    
    }
    
    
    • 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
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97

    项目结构

    在这里插入图片描述

  • 相关阅读:
    LeetCode //C - 124. Binary Tree Maximum Path Sum
    自动驾驶开源数据集汇总
    中科创达邹鹏程:与openEuler的结缘并非偶然,操作系统的未来离不开创新
    OpenWrt安装配置Tailscale
    申请专利的好处!这份清单告诉你,为什么要申请专利?
    python自动化操作邮箱
    【第2章 Node.js基础】2.1 JavaScript基本语法
    C++ 模板泛型编程
    2022Vue经典面试题及答案汇总(持续更新)
    代码随想录笔记_动态规划_718最长重复子数组
  • 原文地址:https://blog.csdn.net/Rockandrollman/article/details/133498687