基于springboot+mybatis+vue的项目实战之页面参数传递

如图所示,删除操作可以用按钮实现,也可以用超链接来实现。

1、第一种情况,用按钮实现。

html页面相关:

 <button type="button" @click="deleteId(peot.id)">删除</button>

<script>
    new Vue({
        el:"#app",
        data() {
            return {
                peotList:[]
            }
        },

        methods:{
            findAll:function () {
                var _this = this;
                axios.post('/findAllJsoon', {
                })
                    .then(function (response) {
                        _this.peotList = response.data.data;//响应数据给peotList赋值
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            deleteId:function (id) {
                var _thisd = this;
                if (window.confirm("确定要删除该条数据吗???")){
                    axios.post('/deletePeot?id='+id)
                        .then(function (response) {
                            alert("删除成功")
                            _thisd.findAll();
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }
            }

        },
        created() {
            // 获得参数id值
           // this.id = location.href.split("?id=")[1]
            // 通过id查询详情
            this.findAll();
        },
    })


</script>

controller文件相关:

 @RequestMapping("/deletePeot")
    public void deletePeot(Integer id){
        peotService.deletePeot(id);
    }

可以看到,这里的controller文件中没有进行参数传递。这是因为,是在当前页面操作,参数直接传递给js,不需要通过url来传递。

2、第二种情况,通过超链接来删除,并跳转回查询所有的页面。

<a :href="'peot_delete.html?id='+peot.id">删除</a>
peot_delete.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./js/vue.js"></script>
    <script src="./js/axios-0.18.0.js"></script>

</head>
<body>
<h1 align="center">诗人信息列表</h1>
<div id="app" align="center">
    <a href="peot_insert.html">新增</a>
    <table  border="1">
        <tr>
            <th>id</th>
            <th>author</th>
            <th>gender</th>
            <th>dynasty</th>
            <th>title</th>
            <th>style</th>
            <th>操作</th>
        </tr>
        <tr v-for="peot in peotList">
            <td>{{peot.id}}</td>
            <td>{{peot.author}}</td>
            <td>{{peot.gender}}</td>
            <td>{{peot.dynasty}}</td>
            <td>{{peot.title}}</td>
            <td>{{peot.style}}</td>
            <td>
                <button type="button" @click="deleteId(peot.id)">删除</button>
                <a :href="'peot_delete.html?id='+peot.id">删除</a>
                <a :href="'peot_edit.html?id='+peot.id">修改</a>
            </td>
        </tr>

    </table>
</div>

</body>

<script>
    new Vue({
        el:"#app",
        data() {
            return {
                peotList:[]
            }
        },

        methods:{
            findAll:function () {
                var _this = this;
                axios.post('/findAllJsoon', {
                })
                    .then(function (response) {
                        _this.peotList = response.data.data;//响应数据给peotList 赋值
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            deleteId:function (id) {
                var _thisd = this;
                var url = `deletePeot_url/${this.id}`  //注意这里是反引号
                if (window.confirm("确定要删除该条数据吗???")){
                    axios.post(url)
                        .then(function (response) {
                            alert("删除成功")
                           // _thisd.findAll();
                            location.href = 'peot_listAll.html'
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }
            }

        },
        created() {
          // 获得参数id值
             this.id = location.href.split("?id=")[1]
            // 通过id查询详情
            this.deleteId();
        },
    })


</script>

</html>

controller页面相关:

    @RequestMapping("/deletePeot_url/{id}")
    public void deletePeot_url(@PathVariable("id") Integer id){
        peotService.deletePeot(id);
    }

可以看出,这里的controller是使用标准@PathVariable("id")进行了参数传递。

3、总结

从一个页面到另外一个页面,如果需要传递参数,需要在controller文件中进行相关的操作。

有关参数的传递,参见文章:Springboot之页面参数传递-CSDN博客

最近更新

  1. TCP协议是安全的吗?

    2024-05-13 18:40:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-13 18:40:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-13 18:40:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-13 18:40:05       20 阅读

热门阅读

  1. matlab实现K均值聚类

    2024-05-13 18:40:05       12 阅读
  2. 图像下载的新趋势:Kotlin技术探索与实践

    2024-05-13 18:40:05       14 阅读
  3. SQL优化

    SQL优化

    2024-05-13 18:40:05      9 阅读
  4. MySQL中的查询语法

    2024-05-13 18:40:05       12 阅读
  5. 头歌C语言课程实验(递归函数、嵌套函数)

    2024-05-13 18:40:05       10 阅读
  6. centos7时间同步教程

    2024-05-13 18:40:05       12 阅读
  7. 传输层的TCP流量控制比数据链路层作用范围更广

    2024-05-13 18:40:05       14 阅读
  8. 音频提取特征

    2024-05-13 18:40:05       13 阅读