• ElementUI之CUD+表单验证


    一.ELementUI之CUD

     后端CUD编写

    1. @Controller
    2. @RequestMapping("/book")
    3. public class BookController {
    4. @Autowired
    5. private IBookService bookService;
    6. @RequestMapping("/addBook")
    7. @ResponseBody
    8. public JsonResponseBody addBook(Book book){
    9. try {
    10. bookService.insert(book);
    11. return new JsonResponseBody<>("新增书本成功",true,0,null);
    12. } catch (Exception e) {
    13. e.printStackTrace();
    14. return new JsonResponseBody<>("新增书本失败",false,0,null);
    15. }
    16. }
    17. @RequestMapping("/editBook")
    18. @ResponseBody
    19. public JsonResponseBody editBook(Book book){
    20. try {
    21. bookService.updateByPrimaryKey(book);
    22. return new JsonResponseBody<>("编辑书本成功",true,0,null);
    23. } catch (Exception e) {
    24. e.printStackTrace();
    25. return new JsonResponseBody<>("编辑书本失败",false,0,null);
    26. }
    27. }
    28. @RequestMapping("/delBook")
    29. @ResponseBody
    30. public JsonResponseBody delBook(Book book){
    31. try {
    32. bookService.deleteByPrimaryKey(book.getId());
    33. return new JsonResponseBody<>("删除书本成功",true,0,null);
    34. } catch (Exception e) {
    35. e.printStackTrace();
    36. return new JsonResponseBody<>("删除书本失败",false,0,null);
    37. }
    38. }
    39. @RequestMapping("/queryBookPager")
    40. @ResponseBody
    41. public JsonResponseBody> queryBookPager(Book book, HttpServletRequest req){
    42. try {
    43. PageBean pageBean=new PageBean();
    44. pageBean.setRequest(req);
    45. List books = bookService.queryBookPager(book, pageBean);
    46. return new JsonResponseBody<>("OK",true,pageBean.getTotal(),books);
    47. } catch (Exception e) {
    48. e.printStackTrace();
    49. return new JsonResponseBody<>("分页查询书本失败",false,0,null);
    50. }
    51. }
    52. @RequestMapping("/queryBookCharts")
    53. @ResponseBody
    54. public JsonResponseBody queryBookCharts(){
    55. try{
    56. Map charts = bookService.queryBookCharts();
    57. return new JsonResponseBody<>("OK",true,0,charts);
    58. }catch (Exception e){
    59. e.printStackTrace();
    60. return new JsonResponseBody<>("查询统计分析数据失败",false,0,null);
    61. }
    62. }
    63. @RequestMapping("/upload")
    64. @ResponseBody
    65. public JsonResponseBody upload(BookFileVo bookFileVo){
    66. try {
    67. MultipartFile bookFile = bookFileVo.getBookFile();
    68. System.out.println(bookFileVo);
    69. System.out.println(bookFile.getContentType());
    70. System.out.println(bookFile.getOriginalFilename());
    71. return new JsonResponseBody<>("上传成功",true,0,null);
    72. } catch (Exception e) {
    73. e.printStackTrace();
    74. return new JsonResponseBody<>("上传失败",false,0,null);
    75. }
    76. }
    77. @RequestMapping("/download")
    78. public void download(HttpServletRequest request, HttpServletResponse response){
    79. try {
    80. String relativePath = "uploads/1.jpg";
    81. String absolutePath = request.getRealPath(relativePath);
    82. InputStream is = new FileInputStream(new File(absolutePath));
    83. OutputStream out = response.getOutputStream();
    84. response.setContentType("application/octet-stream");
    85. response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("1.jpg", "UTF-8"));
    86. byte[] by = new byte[1024];
    87. int len = -1;
    88. while (-1 != (len = is.read(by))) {
    89. out.write(by);
    90. }
    91. is.close();
    92. out.close();
    93. } catch (Exception e) {
    94. e.printStackTrace();
    95. }
    96. }
    97. @RequestMapping("/downloadUrl")
    98. public void downloadUrl(HttpServletRequest request, HttpServletResponse response){
    99. String relativePath = "uploads/1.jpg";
    100. String absolutePath = request.getRealPath(relativePath);
    101. InputStream is = null;
    102. OutputStream out = null;
    103. try {
    104. is = new FileInputStream(new File(absolutePath));
    105. // 设置Content-Disposition
    106. response.setHeader("Content-Disposition",
    107. "attachment;filename=" + URLEncoder.encode("1.jpg", "UTF-8"));
    108. out = response.getOutputStream();
    109. IOUtils.copy(is, out);
    110. response.flushBuffer();
    111. System.out.println("完成");
    112. } catch (Exception e) {
    113. e.printStackTrace();
    114. } finally {
    115. IOUtils.closeQuietly(is);
    116. IOUtils.closeQuietly(out);
    117. }
    118. }
    119. }

    前端编写

    配置访问路径:

    在项目中的src文件下api中找到action.js进行配置数据访问的地址:

    1. 'BOOKMSG_BOOKINFO_REQ': '/book/queryBookPager', //获取书本信息
    2. 'BOOK_ADD': '/book/addBook', //新增书本
    3. 'BOOK_EDIT': '/book/editBook', //修改书本
    4. 'BOOK_DEL': '/book/delBook', //删除书本

    弹出框窗口编写

     增删改查实现

    新增演示:

    修改演示:

     删除演示:

    二、表单验证

    自定义验证规则
    1. //定义验证规则
    2. rules:{
    3. bookname: [{
    4. required: true,
    5. message: '请输入书籍名称',
    6. trigger: 'blur'
    7. },
    8. {
    9. min: 1,
    10. max: 10,
    11. message: '长度在 1 到 10 个字符',
    12. trigger: 'blur'
    13. }
    14. ],
    15. price: [{
    16. required: true,
    17. message: '请输入价格',
    18. trigger: 'blur'
    19. }],
    20. booktype: [{
    21. required: true,
    22. message: '请输入书籍类别',
    23. trigger: 'blur'
    24. }]
    25. },

    使用规则

    1. /* 新增书本 */
    2. saveBook() {
    3. this.$refs['bookForm'].validate((valid) => {
    4. if (valid) {
    5. //默认新增
    6. var url = this.axios.urls.BOOK_ADD;
    7. if (this.optiontype == 'update') {
    8. url = this.axios.urls.BOOK_EDIT;
    9. }
    10. console.log(url)
    11. this.axios.post(url, this.bookForm).then(d => {
    12. //关闭窗口
    13. this.closeBookForm();
    14. this.dialogFormVisible = false;
    15. this.query({});
    16. }).catch();
    17. } else {
    18. console.log('error submit!!');
    19. return false;
    20. }
    21. });
    22. },

    效果演示:

  • 相关阅读:
    k8s--基础--25.3--Helm--常用命令和常见错误
    Python获取时光网电影数据
    多模态大模型总结
    【数据结构】什么是线性表?
    数字IC笔试面试题之--时钟偏斜(skew)与抖动(jitter)
    科技云报道:不堪忍受英伟达霸权,微软、OpenAI纷纷自研AI芯片
    kubernetes(K8S)学习笔记P4:K8s核心概念1
    转置卷积详解(原理+实验)
    【分享】“简道云“ 在集简云平台集成应用的常见问题与解决方案
    perl列表创建、追加、删除
  • 原文地址:https://blog.csdn.net/liaozhixiangjava/article/details/133717135