SSMP整合案例交互之在idea中利用vue和axios发送异步请求进行前后端调用

前后端调用

数据层开发的代码都已经书写完毕

表现层也书写完毕

接下来我们要书写的是功能

及把数据返回到页面的指定部分

或者是在前端使用功能 然后进行操作

正常情况我们是应该把前端页面放在前端服务器上面的

我们这边直接用tomcat

我们在后端完成全栈开发

首先会拿到前端页面 我们需要做的是补全script标签里的代码

如果单体工程的页面放置在resource下

出现了bug

用maven的clean功能

除了问题先clean重新编译一下

我们把前端页面引入

axios发送异步请求

static文件夹

查看页面

页面结构

<!DOCTYPE html>

<html xmlns: xmlns:>

<head>

    <!-- 页面 -->

    <meta charset="utf-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title>大数据实验室人员管理</title>

    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">

    <!-- 引入样式 -->

    <link rel="stylesheet" href="../plugins/elementui/index.css">

    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">

    <link rel="stylesheet" href="../css/style.css">

</head>


<body class="hold-transition">

<div id="app">

    <div class="content-header">

        <!--logo-->
        <img src='../img/logo.png' style="align-content: center" alt="Centered Image">

        <!--大标题-->
        <h1 style="text-align:center;font-size: 50px;color: #3a8ee6;">大数据应用与开发实验室人员管理系统</h1>


    </div>

    <div class="app-container">

        <div class="box">

            <div class="filter-container">
                <el-input placeholder="人员类别" v-model="pagination.type" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-input placeholder="人员名称" v-model="pagination.name" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-input placeholder="人员描述" v-model="pagination.description" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="getAll()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>

            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>

                <el-table-column type="index" align="center" label="序号"></el-table-column>

                <el-table-column prop="type" label="人员类别" align="center"></el-table-column>

                <el-table-column prop="name" label="人员名称" align="center"></el-table-column>

                <el-table-column prop="description" label="描述" align="center"></el-table-column>

                <el-table-column label="操作" align="center">

                    <template slot-scope="scope">

                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>

                        <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>

                    </template>

                </el-table-column>

            </el-table>

            <!--分页组件-->
            <div class="pagination-container">

                <el-pagination
                        class="pagiantion"

                        @current-change="handleCurrentChange"

                        :current-page="pagination.currentPage"

                        :page-size="pagination.pageSize"

                        layout="total, prev, pager, next, jumper"

                        :total="pagination.total">

                </el-pagination>

            </div>

            <!-- 新增标签弹层 -->

            <div class="add-form">

                <el-dialog title="新增人员" :visible.sync="dialogFormVisible">

                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">

                        <el-row>

                            <el-col :span="12">

                                <el-form-item label="人员类别" prop="type">

                                    <el-input v-model="formData.type"/>

                                </el-form-item>

                            </el-col>

                            <el-col :span="12">

                                <el-form-item label="人员名称" prop="name">

                                    <el-input v-model="formData.name"/>

                                </el-form-item>

                            </el-col>

                        </el-row>


                        <el-row>

                            <el-col :span="24">

                                <el-form-item label="描述">

                                    <el-input v-model="formData.description" type="textarea"></el-input>

                                </el-form-item>

                            </el-col>

                        </el-row>

                    </el-form>

                    <div slot="footer" class="dialog-footer">

                        <el-button @click="cancel()">取消</el-button>

                        <el-button type="primary" @click="handleAdd()">确定</el-button>

                    </div>

                </el-dialog>

            </div>

            <!-- 编辑标签弹层 -->

            <div class="add-form">

                <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">

                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">

                        <el-row>

                            <el-col :span="12">

                                <el-form-item label="人员类别" prop="type">

                                    <el-input v-model="formData.type"/>

                                </el-form-item>

                            </el-col>

                            <el-col :span="12">

                                <el-form-item label="人员名称" prop="name">

                                    <el-input v-model="formData.name"/>

                                </el-form-item>

                            </el-col>

                        </el-row>

                        <el-row>

                            <el-col :span="24">

                                <el-form-item label="描述">

                                    <el-input v-model="formData.description" type="textarea"></el-input>

                                </el-form-item>

                            </el-col>

                        </el-row>

                    </el-form>

                    <div slot="footer" class="dialog-footer">

                        <el-button @click="cancel()">取消</el-button>

                        <el-button type="primary" @click="handleEdit()">确定</el-button>

                    </div>

                </el-dialog>

            </div>

        </div>

    </div>

</div>

</body>

<!-- 引入组件库 -->

<script src="../js/vue.js"></script>

<script src="../plugins/elementui/index.js"></script>

<script type="text/javascript" src="../js/jquery.min.js"></script>

<script src="../js/axios-0.18.0.js"></script>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            dataList: [],//当前页要展示的列表数据
            dialogFormVisible: false,//添加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            formData: {},//表单数据
            rules: {//校验规则
                type: [{required: true, message: '人员类别为必填项', trigger: 'blur'}],
                name: [{required: true, message: '人员名称为必填项', trigger: 'blur'}]
            },
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                type: "",
                name: "",
                description: ""
            }
        },

        //钩子函数,VUE对象初始化完成后自动执行
        created() {
            
        },

        methods: {
            //列表
            getAll() {
            },

            //弹出添加窗口
            handleCreate() {
            },

            //重置表单
            resetForm() {
            },

            //添加
            handleAdd() {
            },

            //取消
            cancel() {
            },

            // 删除
            handleDelete(row) {
            },

            //弹出编辑窗口
            handleUpdate(row) {
            },

            //修改
            handleEdit() {
            },

            //分页查询

            //切换页码
            handleCurrentChange(currentPage){
            },

            //条件查询
        }
    })

</script>

</html>

我们需要做的是补全js里面的内容

我们首先测试是否控制台能够运行

如果刷新 控制台能返回run

那说明运行起来了

成功运行

接下来书写发送异步请求的代码

先试一下发送信息

getAll() {
    // console.log("run")
    axios.get("/users").then((res)=>{
        console.log(res.data)
    });

},

能成功发送

我们每次都要这样测一下

钩子函数里调getALL

getALL里用axios发送了一个异步请求

请求方式是get

后面是路径

然后是做的事情 我们用箭头函数简化书写

这样我们前端访问后端就跑成功

相关推荐
数据龙傲天9 分钟前
1688商品API接口:电商数据自动化的新引擎
java·大数据·sql·mysql
长天一色9 分钟前
【ECMAScript 从入门到进阶教程】第三部分:高级主题(高级函数与范式,元编程,正则表达式,性能优化)
服务器·开发语言·前端·javascript·性能优化·ecmascript
NiNg_1_23426 分钟前
npm、yarn、pnpm之间的区别
前端·npm·node.js
秋殇与星河29 分钟前
CSS总结
前端·css
NiNg_1_23429 分钟前
Vue3 Pinia持久化存储
开发语言·javascript·ecmascript
读心悦30 分钟前
如何在 Axios 中封装事件中心EventEmitter
javascript·http
带带老表学爬虫37 分钟前
java数据类型转换和注释
java·开发语言
engineer-gxd44 分钟前
MySQL 表的操作
mysql
千里码aicood44 分钟前
【2025】springboot教学评价管理系统(源码+文档+调试+答疑)
java·spring boot·后端·教学管理系统
cyt涛1 小时前
MyBatis 学习总结
数据库·sql·学习·mysql·mybatis·jdbc·lombok