Singerw's Repository Singerw's Repository
首页
  • 相关文章

    • HTML相关文章
    • CSS相关文章
    • JavaScript相关文章
  • 学习笔记

    • JavaScript笔记
    • ES6笔记
    • Vue笔记
  • 相关文章

    • Spring相关文章
    • SpringBoot相关文章
    • MyBatis相关文章
    • MySQL相关文章
  • 学习笔记

    • SpringBoot笔记
    • Spring笔记
    • MyBatis笔记
    • MySQL笔记
    • JavaWeb笔记
    • JavaCore笔记
  • 学习笔记

    • Linux笔记
    • Git笔记
    • 技术文档
  • 偏门技术

    • GitHub技巧
    • 博客搭建
    • 科学上网
  • 安装教程

    • JDK
    • MySQL
    • Node.js
    • Linux
  • 终身学习
  • 面试人生
  • 心情杂货
  • 生活随笔
  • 归档
  • 标签
GitHub (opens new window)

Singerw

谁能够凭爱意将富士山私有
首页
  • 相关文章

    • HTML相关文章
    • CSS相关文章
    • JavaScript相关文章
  • 学习笔记

    • JavaScript笔记
    • ES6笔记
    • Vue笔记
  • 相关文章

    • Spring相关文章
    • SpringBoot相关文章
    • MyBatis相关文章
    • MySQL相关文章
  • 学习笔记

    • SpringBoot笔记
    • Spring笔记
    • MyBatis笔记
    • MySQL笔记
    • JavaWeb笔记
    • JavaCore笔记
  • 学习笔记

    • Linux笔记
    • Git笔记
    • 技术文档
  • 偏门技术

    • GitHub技巧
    • 博客搭建
    • 科学上网
  • 安装教程

    • JDK
    • MySQL
    • Node.js
    • Linux
  • 终身学习
  • 面试人生
  • 心情杂货
  • 生活随笔
  • 归档
  • 标签
GitHub (opens new window)
  • Spring

  • SpringMVC

    • 《Spring MVC》笔记
    • Spring MVC入门
    • Spring MVC请求处理流程
    • 非Ajax在SpringMVC使用
    • Ajax在SpringMVC使用
    • 控制器中的注解介绍
    • 非Ajax的数据传递
    • Ajax的数据传递
      • 1、Ajax的控制器传递数据给视图层
        • 1.1 基于Layui的数据分页显示模糊查询
      • 2、Ajax的视图层传递数据控制器
        • 2.1 普通值的传递
        • 2.2、富文本编辑器向控制器传值
      • 3、其他操作值传递
        • 增加操作@PostMapping
        • 修改操作@PutMapping
        • 删除操作@DeleteMapping
        • 删除所有@DeleteMapping
    • RestFul通用风格案例
    • 页面转发和重定向
    • 实现文件上传下载
    • 富文本编辑器文件上传
    • Post请求中文乱码问题
    • springmvc-xml的配置文件
    • SpringMVC拦截器
    • Web-xml配置文件
    • 批量物理删除和逻辑删除
  • SSM整合

  • 《Spring》学习笔记
  • SpringMVC
Singerw
2021-08-30

Ajax的数据传递

# Ajax的控制器与视图层数据传递

# 1、Ajax的控制器传递数据给视图层

Get请求通过ID查询博客文章

@GetMapping("/blog/{blogid}")
public BlogEntity getBlogByID(@PathVariable("blogid") int blogid) {
    return blogService.getBlogByID(blogid);
}
1
2
3
4

# 1.1 基于Layui的数据分页显示模糊查询

分页查询所有的博客列表,并实现模糊查询@GetMapping

Controller:

@GetMapping("/blog")
public ResponseData<BlogEntity> getBlog(
    @RequestParam(name = "keywords", required = true, defaultValue = "") String keywords,
    @RequestParam(name = "page", required = true, defaultValue = "1") int page,
    @RequestParam(name = "limit", required = true, defaultValue = "10") int limit) {
    PageData<BlogEntity> blogList = blogService.getBlogList(page, limit, keywords);
    ResponseData<BlogEntity> responseData = new ResponseData(0, "操作成功", blogList.getTotal(), blogList.getData());
    return responseData;
}
1
2
3
4
5
6
7
8
9

JS:

table.render({
    elem: '#user-table',
    url: '../../../../admin/getarticle',
    page: true,
    cols: cols,
    skin: 'line',
    toolbar: '#user-toolbar',
    defaultToolbar: [{
        title: '刷新',
        layEvent: 'refresh',
        icon: 'layui-icon-refresh',
    }, 'filter', 'print', 'exports']
});
1
2
3
4
5
6
7
8
9
10
11
12
13

# 2、Ajax的视图层传递数据控制器

# 2.1 普通值的传递

# 【示例】基于Layui删除一篇文章

JS:

window.remove = function (obj) {
    layer.confirm('确定要删除此文章', {
        icon: 3,
        title: '提示'
    }, function (index) {
        layer.close(index);
        let loading = layer.load();
        $.ajax({
            url: '../../../../admin/delarticle/' + obj.data['article_id'],
            dataType: 'json',
            type: 'delete',
            success: function (result) {
                layer.close(loading);
                layer.msg(result.msg, {
                    icon: 1,
                    time: 1000
                });
            }
        })
    });
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Contorller:

@DeleteMapping("/delarticle/{articleID}")
public ResponseData deleteArticleByID(@PathVariable("articleID") int articleID) {
    ResponseData responseData = articleService.deleteArticleByID(articleID);
    return responseData;
}
1
2
3
4
5

# 【示例】基于Layui修改文章状态

JS:

form.on('switch(article-status)', function (obj) {
    let status = obj.elem.checked ? 1 : 0
    $.ajax({
        url: '../../../../admin/updatest/'+this.value+"/"+status,
        type: 'put',
        success: function (res) {
            layer.tips(res.msg, obj.othis)
        },
    });
});
1
2
3
4
5
6
7
8
9
10

Controller:

@PutMapping("/updatest/{articleStatus}/{articleID}")
public ResponseData updateArticleStatus(
    @PathVariable("articleStatus") int articleStatus,
    @PathVariable("articleID") int articleID) {
    ResponseData responseData = articleService.updateArticleStatus(articleID, articleStatus);
    return responseData;
}
1
2
3
4
5
6
7

# 2.2、富文本编辑器向控制器传值

TinyMCE:功能强大、所见即所得的富文本编辑器👻http://tinymce.ax-z.cn/

【示例】:Layui.css和Layui.js

<!-- 引入 layui.css -->
<link rel="stylesheet" href="//unpkg.com/layui@2.6.8/dist/css/layui.css">

<!-- 引入 layui.js -->
<script src="//unpkg.com/layui@2.6.8/dist/layui.js">
1
2
3
4
5

【示例】:HTML

<!--layui富文本编辑器-->
<div class="layui-card-body">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
        <legend>文章内容</legend>
    </fieldset>
    <textarea id="edit" cols="30" rows="10" name="article_content"></textarea>
    <div class="layui-btn-group" style="margin-top: 10px">
        <button class="layui-btn" lay-submit lay-filter="form-fabu">发布</button>
    </div>
</div>
1
2
3
4
5
6
7
8
9
10

【示例】:Js

<!--Layui富文本编辑器-->
    <script>
    layui.use(['tinymce', 'form', 'jquery'], function () {
    let form = layui.form;
    let $ = layui.jquery;

    var tinymce = layui.tinymce
    var edit = tinymce.render({
        elem: "#edit",
        height: 400
    });
    //获取文本框中的值
    edit.getContent();
    form.on('submit(form-fabu)', function (data) {
        data.field.article_content = edit.getContent()
        console.log(data.field.article_content)
        let roleIds = "";
        $('input[type=checkbox]:checked').each(function () {
            roleIds += $(this).val() + ",";
        });
        roleIds = roleIds.substr(0, roleIds.length - 1);
        data.field.roleIds = roleIds;
        console.log(data.field);
        $.ajax({
            url: '../../../../admin/addarticle',
            data: JSON.stringify(data.field),
            dataType: 'json',
            contentType: 'application/json',
            type: 'post',
            success: function (result) {
                if (result.success) {
                    layer.msg(result.msg, {icon: 2, time: 1000});
                } else {
                    layer.msg(result.msg, {icon: 1, time: 1000});
                }
            }
        });
        return false;
    });
});
</script>
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

【示例】:添加文章后端Controller控制器

@PostMapping("/addarticle")
public AjaxOperateResponse addArticle(@RequestBody ArticleEntity article) {
    AjaxOperateResponse response = new AjaxOperateResponse(200, articleService.addArticle(article) ? "增加成功" : "增加失败");
    return response;
}
1
2
3
4
5

# 3、其他操作值传递

# 增加操作@PostMapping

@PostMapping("/blog")
public AjaxOperateResponse addBlog(BlogEntity blog) {
    AjaxOperateResponse response = new AjaxOperateResponse(200, "增加成功");
    return response;
}
1
2
3
4
5
@PostMapping("/addarticle")
public AjaxOperateResponse addArticle(@RequestBody ArticleEntity article) {
    AjaxOperateResponse response = new AjaxOperateResponse(200, articleService.addArticle(article) ? "增加成功" : "增加失败");
    return response;
}
1
2
3
4
5

# 修改操作@PutMapping

@PutMapping("/blog/{blogid}")
public AjaxOperateResponse updateBlog(@PathVariable("blogid") int blogid, BlogEntity blog) {
    AjaxOperateResponse response = new AjaxOperateResponse(200, "增加成功");
    return response;
}
1
2
3
4
5
@PutMapping("/updatest/{articleStatus}/{articleID}")
public ResponseData updateArticleStatus(
    @PathVariable("articleStatus") int articleStatus,
    @PathVariable("articleID") int articleID) {
    ResponseData responseData = articleService.updateArticleStatus(articleID, articleStatus);
    return responseData;
}
1
2
3
4
5
6
7

# 删除操作@DeleteMapping

@DeleteMapping("/blog/{blogid}")
public AjaxOperateResponse deleteBlogByID(@PathVariable("blogid") int blogid) {
    AjaxOperateResponse response = new AjaxOperateResponse(200, "删除成功");
    return response;
}
1
2
3
4
5

# 删除所有@DeleteMapping

@DeleteMapping("/blog/")
public AjaxOperateResponse deleteAllBlogByID() {
    AjaxOperateResponse response = new AjaxOperateResponse(200, "批量删除成功");
    return response;
}
1
2
3
4
5
编辑 (opens new window)
#SpringMVC
非Ajax的数据传递
RestFul通用风格案例

← 非Ajax的数据传递 RestFul通用风格案例→

最近更新
01
Maven资源导出问题终极版
10-12
02
《MyBatis-Plus》学习笔记
10-07
03
MyBatis-Plus—配置日志
10-07
更多文章>
Theme by Vdoing | Copyright © 2020-2021 版权所有 | repository.singerw.com
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×