IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> SpringBoot的前后端分离--上传数据 -> 正文阅读

[JavaScript知识库]SpringBoot的前后端分离--上传数据

目录

来个vue 表单:

获取 el-input内容

效果展示:


推荐先看:https://zhangjq.blog.csdn.net/article/details/118927470

前端:https://gitee.com/zhangjiqun/vue-annuoyun/tree/master

后台:https://gitee.com/zhangjiqun/background-development-demo/tree/master

数据库:

?

来个vue 表单:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">

</head>

<body>
    <div id="app">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="姓名:" prop="name">
                <el-input  v-model="ruleForm.name" ></el-input>
            </el-form-item>
            <el-form-item label="等级:" prop="id">
                <el-input  v-model="ruleForm.id" ></el-input>
            </el-form-item>
            <el-form-item label="年龄:" prop="age">
                <el-input v-model.number="ruleForm.age"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="getInfo('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>

</body>

</html>

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/element-ui@2.15.3/lib/index.js"></script>

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>


<script>
    var Main = {
        data() {
  
            return {
                ruleForm: {
                    name: '',
                    id: '',
                    age: ''
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        getInfo();
                        alert('submit!');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            
            getInfo(){
					this.$http.get('http://localhost:10000/userInfo/insertUser',{params:{id:this.ruleForm.id,name:this.ruleForm.name,age:this.ruleForm.age}},{
                            emulateJSON:true
                        }).then(result =>{
						console.log(result.body.rows)
						this.list=result.body.rows
					});
                    alert('submit!');

				}
        }
    }
    var Ctor = Vue.extend(Main)
    new Ctor().$mount('#app')

</script>

重要划线:

?

后台代码:

package com.example.demo.easy.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.easy.entity.UserInfo;
import com.example.demo.easy.service.UserInfoService;
import org.json.JSONException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (UserInfo)表控制层
 *
 * @author makejava
 * @since 2021-07-19 17:41:13
 */
@RestController
@RequestMapping(value = "userInfo")
public class UserInfoController {
    /**
     * 服务对象
     */
    @Resource
    private UserInfoService userInfoService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("selectOne")
    public UserInfo selectOne(@RequestParam (name = "id")Integer id) {
        UserInfo userInfo=this.userInfoService.queryById(id);
        return userInfo;
    }

    @GetMapping("selectTne")
    public UserInfo selectTne(@RequestParam (name = "id")Integer id) {
        UserInfo userInfo=this.userInfoService.queryById(id);
        return userInfo;
    }
    @GetMapping("selectAll")
    @CrossOrigin
    public  List<UserInfo> selectAll() {
        StringBuilder stringBuilder=new StringBuilder();
        List<UserInfo> list=this.userInfoService.queryAllByLimit(1,100);
//        for (UserInfo userInfo :list) {
//            stringBuilder.append(userInfo.toString());
//
//        }
        return list;
    }
    @GetMapping("selectAllJson")
    @CrossOrigin
    public  String selectAllJson() throws JSONException {
        List<UserInfo> list=this.userInfoService.queryAllByLimit(1,100);
        String str = JSON.toJSONString(list); // List转json

            return str;


    }
    @GetMapping("insertUser")
    public UserInfo insertUser(@RequestParam (name = "id")Integer id,
                               @RequestParam (name = "name")String name,
                               @RequestParam (name = "age")String age) {

        UserInfo userInfo=new UserInfo();
        userInfo.setId(id);
        userInfo.setName(name);
        userInfo.setAge(age);
        this.userInfoService.insert(userInfo);
        return userInfo;
    }

}

获取 el-input内容

<script>
    var Main = {
        data() {
  
            return {
                ruleForm: {
                    name: '',
                    id: '',
                    age: ''
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        getInfo();
                        alert('submit!');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            
            getInfo(){
					this.$http.get('http://localhost:10000/userInfo/insertUser',{params:{id:this.ruleForm.id,name:this.ruleForm.name,age:this.ruleForm.age}},{
                            emulateJSON:true
                        }).then(result =>{
						console.log(result.body.rows)
						this.list=result.body.rows
					});
                    alert('submit!');

				}
        }
    }
    var Ctor = Vue.extend(Main)
    new Ctor().$mount('#app')

</script>

效果展示:

插入数值

访问数据显示:

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-07-23 10:38:28  更:2021-07-23 10:38:36 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/6 8:24:19-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码