关键词搜索

源码搜索 ×
×

前端笔记-freemarker模板获取后端数据及提交数据

发布2019-12-09浏览12385次

详情内容

目录

 

基本概念

代码与实例


 

基本概念

这里有如下页面:

这里面表单的数据都是从后端获取的,点获取数据,会调用getRecord方法从数据库获取数据。

点击提交备注,是备注可以让用户填写。

提交后,更新数据库中的数据。

freemarker中使用${xxxx},这种方式获取单条的数据。

 

 

代码与实例

前端代码如下:

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8" />
  4. <link rel="stylesheet" href="https://cdn.jsdelivr.net/semantic-uihttps://cdn.jxasp.com:9143/image/2.2.4/semantic.min.css">
  5. <title>数据获取页面</title>
  6. </head>
  7. <body>
  8. <br>
  9. <br>
  10. <br>
  11. <br>
  12. <div class="m-container m-padded-tb-big doubling">
  13. <div class="ui container doubling">
  14. <div class="ui row">
  15. <div class="seven wide column doubling">
  16. <div class="ui top attached segment">
  17. <div class="ui header">获取数据</div>
  18. </div>
  19. <div class="ui attached segment ">
  20. <form id="webDataForm" name="webDataForm" method="post" action="/getRecord">
  21. <div class="ui labeled left icon input" style="width: 100%">
  22. <i class="computer icon"></i>
  23. <input id="userName" readonly="true" type="text" name="userName" placeholder="1568452****" value=${userName}>
  24. <a class="ui tag label">
  25. 用户名
  26. </a>
  27. </div>
  28. <br>
  29. <br>
  30. <div class="ui labeled left icon input" style="width: 100%">
  31. <i class="briefcase icon"></i>
  32. <input id="password" readonly="true" type="text" name="password" placeholder="1A2B3C4D5E6F7G!*(41@" value=${password}>
  33. <a class="ui tag label">
  34. 密码
  35. </a>
  36. </div>
  37. <br>
  38. <br>
  39. <div class="ui labeled left icon input" style="width: 100%">
  40. <i class="eyedropper icon"></i>
  41. <input id="createTime" readonly="true" type="text" name="createTime" placeholder="1A2B3C4D5E6F7G!*(41@" value=${createTime}>
  42. <a class="ui tag label">
  43. 创建时间
  44. </a>
  45. </div>
  46. <br>
  47. <br>
  48. <div class="ui labeled left icon input" style="width: 100%">
  49. <i class="calendar icon"></i>
  50. <input id="remarks" type="text" name="remarks" placeholder="如:可以使用,或者用户名错误,或者密码错误" value=${remarks}>
  51. <a class="ui tag label">
  52. 备注
  53. </a>
  54. </div>
  55. <br>
  56. <br>
  57. <div class="ui padded grid">
  58. <div class="white row">
  59. <div class="column">
  60. <h2 class="ui header">注意</h2>
  61. <p>成功获取帐号后,需要填写备注信息,随后点击提交备注按钮,避免下次获取同样的帐号</p>
  62. </div>
  63. </div>
  64. <div class="two column row">
  65. <div class="white column">
  66. <button class="fluid ui teal button" type="submit">点击获取数据</button>
  67. </div>
  68. <div class="two column column">
  69. <button class="fluid ui teal button" onclick="updateWebData()">点击提交备注</button>
  70. </div>
  71. </div>
  72. </div>
  73. </form>
  74. </div>
  75. </div>
  76. </div>
  77. </div>
  78. </div>
  79. <script>
  80. function updateWebData() {
  81. for(var i=0; i<document.webDataForm.elements.length - 2; i++)
  82. {
  83. if(document.webDataForm.elements[i].value=="")
  84. {
  85. console.log(i);
  86. alert("当前表单不能有空项");
  87. document.webDataForm.elements[i].focus();
  88. return;
  89. }
  90. }
  91. document.webDataForm.action = "/updateWebData";
  92. document.webDataForm.submit();
  93. }
  94. </script>
  95. </body>
  96. </html>

这里有几个重要的知识点:

表单中有两个按钮该如何发请求。

这里做一个原始的按钮使用

<button class="fluid ui teal button" type="submit">点击获取数据</button>

另一个按钮使用JavaScript脚本:

<button class="fluid ui teal button" onclick="updateWebData()">点击提交备注</button>

其中对应的函数如下:

  1. <script>
  2. function updateWebData() {
  3. for(var i=0; i<document.webDataForm.elements.length - 2; i++)
  4. {
  5. if(document.webDataForm.elements[i].value=="")
  6. {
  7. console.log(i);
  8. alert("当前表单不能有空项");
  9. document.webDataForm.elements[i].focus();
  10. return;
  11. }
  12. }
  13. document.webDataForm.action = "/updateWebData";
  14. document.webDataForm.submit();
  15. }
  16. </script>

其中对应的后端如下:

  1. package com.example.demo.controller;
  2. import com.example.demo.model.DataForm;
  3. import com.example.demo.object.WebData;
  4. import com.example.demo.service.WebDataService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.ModelAttribute;
  9. import org.springframework.web.bind.annotation.PostMapping;
  10. import java.util.Map;
  11. @Controller
  12. public class IndexController {
  13. @Autowired
  14. WebDataService service;
  15. @GetMapping({"/", "/index", "index.html"})
  16. public String getIndex(Map<String, Object> map){
  17. map.put("userName", "131******1232");
  18. map.put("password", "abc*******123");
  19. map.put("createTime", "https://cdn.jxasp.com:9143/image/2019-12-08 20:36:32");
  20. map.put("remarks", "如:可以使用(禁止带空格)can_be_used");
  21. return "index";
  22. }
  23. @PostMapping("/getRecord")
  24. public String getRecord(Map<String, Object> map){
  25. WebData unUserdAccount = service.getUnUserdAccount();
  26. if(unUserdAccount != null){
  27. map.put("userName", unUserdAccount.getUserName());
  28. map.put("password", unUserdAccount.getPassword());
  29. map.put("createTime",unUserdAccount.getCreateTime());
  30. map.put("remarks", "");
  31. }
  32. else{
  33. //数据库中无数据的情况下:
  34. map.put("userName", "无数据,请联系站长补充!");
  35. map.put("password", "无数据,请联系站长补充!");
  36. map.put("createTime", "无数据,请联系站长补充!");
  37. map.put("remarks", "无数据,请联系站长补充!");
  38. }
  39. return "index";
  40. }
  41. @PostMapping("/updateWebData")
  42. public String updateWebData(@ModelAttribute("form") DataForm form){
  43. WebData webData = new WebData();
  44. webData.setUserName(form.getUserName());
  45. webData.setPassword(form.getPassword());
  46. webData.setRemarks(form.getRemarks());
  47. if(service.updateRemarksByWebData(webData)){
  48. return "redirect:/index";
  49. }
  50. //TODO
  51. System.out.println("有异常");
  52. return "redirect:/index";
  53. }
  54. }

这里接收前端数据用了DataForm,把DataForm里面的成员,设置为前端Input的name

  1. package com.example.demo.model;
  2. import lombok.Data;
  3. @Data
  4. public class DataForm {
  5. private String userName;
  6. private String password;
  7. private String createTime;
  8. private String remarks;
  9. }

通过这种方式来获取前端数据!

后端在请求中增加map,通过往map中增加数据,来给前端提供数据:

如下伪代码:

  1. @PostMapping("/getRecord")
  2. public String getRecord(Map<String, Object> map){
  3. WebData unUserdAccount = service.getUnUserdAccount();
  4. if(unUserdAccount != null){
  5. map.put("userName", unUserdAccount.getUserName());
  6. map.put("password", unUserdAccount.getPassword());
  7. map.put("createTime",unUserdAccount.getCreateTime());
  8. map.put("remarks", "");
  9. }
  10. else{
  11. //数据库中无数据的情况下:
  12. map.put("userName", "无数据,请联系站长补充!");
  13. map.put("password", "无数据,请联系站长补充!");
  14. map.put("createTime", "无数据,请联系站长补充!");
  15. map.put("remarks", "无数据,请联系站长补充!");
  16. }
  17. return "index";
  18. }

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载