1. 程式人生 > 其它 >velocity的foreach遍歷資料

velocity的foreach遍歷資料

1、什麼是Velocity

  Velocity是一個簡單而強大的基於 Java 的模板引擎,可將資料從純 Java 物件呈現為文字、xml、電子郵件、SQL、Post Script、HTML 等。模板語法和呈現引擎既易於理解,又易於學習和實施.

功能遠遠超出了 Web 領域(例如 xdoclet、middlegen、Intellij 等),使程式設計師能夠專注於編寫功能程式碼,同時,模板設計人員可以直接修改模板以建立有吸引力的輸出

在 webapps 中,模型-檢視-控制 (MVC) 分離可以被嚴格執行,因為模板不包含“程式碼”。或者,由程式設計師決定,可以在模板中提供“工具”,以便更直接地訪問資料。

2、學習Velocity的參考網站

公司的專案使用的是velocity模板引擎,所以在日常的編碼中做個記錄,方便下次查詢使用:

附上我學習velocity這個技術的參考網站:

Java Velocity模板引擎詳解

3、需求以及實現

  遍歷資料庫中的使用者資料,並在前臺頁面進行一個展示:

DAO:

 1 /**
 2  * 對於使用者的一些查詢方法(dao層)
 3  *
 4  * @author zhangzhixi
 5  * @date 2021-6-25 10:43
 6  */
 7 public class InquireMemberDataManager {
 8     /**
9 * 單例建立物件 10 */ 11 private static InquireMemberDataManager singleton; 12 private static final Logger LOG = LoggerFactory.getLogger(InquireMemberDataManager.class); 13 14 public static InquireMemberDataManager instance() { 15 if (singleton == null) { 16 singleton = new
InquireMemberDataManager(); 17 } 18 return singleton; 19 } 20 21 private static CommonDatabaseAccess db() { 22 return CommonDatabaseAccess.instance(); 23 } 24 25 /** 26 * 通過name獲取這個使用者 27 * <p> 28 * // * @param loginname 使用者名稱稱 29 * 30 * @return 使用者實體 31 * @throws DbAccessException 32 */ 33 public List<Member> getMember(String name) throws DbAccessException { 34 35 String sql = "SELECT * FROM T_MEM_MEMBER WHERE F_LOGINNAME= ?"; 36 37 List<Member> member = new ArrayList<>(); 38 // 39 IDbacTransaction tx = db().beginTransaction(); 40 try { 41 member = db().listObjects(sql, new Object[]{name}, Member.class, 0, 0); 42 System.out.println("=======================>>" + member); 43 } catch (DbAccessException e) { 44 tx.rollback(); 45 throw e; 46 } finally { 47 db().endTransaction(); 48 } 49 return member; 50 } 51 }
View Code

Service:

 1 /**
 2  * service層
 3  *
 4  * @author zhangzhixi
 5  * @date 2021-6-25 10:57
 6  */
 7 public class InquireMemberDataService {
 8     private static InquireMemberDataService singleton;
 9     private static final Logger LOG = LoggerFactory.getLogger(InquireMemberDataService.class);
10 
11     public static InquireMemberDataService instance() {
12         if (singleton == null) {
13             singleton = new InquireMemberDataService();
14         }
15         return singleton;
16     }
17 
18 
19     public List<Member> getMember(String name) throws DbAccessException {
20         // 呼叫dao層,執行sql
21         InquireMemberDataManager instance = InquireMemberDataManager.instance();
22         List<Member> member = instance.getMember(name);
23 
24         // 一些不重要的資料輸出
25         for (Member member1 : member) {
26             System.out.println("Service===》" + member);
27         }
28         return member;
29     }
30 }
View Code

Controller:

 1 /**
 2      * 個人中心-張志喜測試
 3      *
 4      * @param name     使用者名稱
 5      * @param model    檢視層處理資料以及檢視頁面的跳轉
 6      * @param request  請求
 7      * @param response 響應
 8      * @return 使用者資料到前臺頁面進行一個展示
 9      */
10     @RequestMapping(value = "/myTest.htm", method = RequestMethod.POST)
11     public ModelAndView weeklyRecordByDate(String name, Model model, HttpServletRequest request, HttpServletResponse response) throws CmsException, JsonProcessingException, DbAccessException {
12         // 呼叫service層,得到使用者資料
13         List<Member> member = InquireMemberDataService.instance().getMember(name);
14 
15         for (Member mem : member) {
16             System.out.println(mem);
17         }
18 
19         // 將查詢到的使用者資料返回到前端頁面
20         if (member.size() > 0) {
21             // 返回檢視資料
22             model.addAttribute("msg", member);
23         } else {
24             model.addAttribute("msg", "查詢的使用者不存在或者資料為空,請重新輸入!");
25             return new ModelAndView("pc/cms/article/article/zzxView");
26         }
27         return new ModelAndView("pc/cms/article/article/zzxData");
28     }
29 
30     /**
31      * 使用者姓名資料填寫查詢頁面
32      *
33      * @return 跳轉到查詢使用者資料查詢表單頁面
34      */
35     @RequestMapping(value = "/myView.htm", method = RequestMethod.GET)
36     public ModelAndView getModelAndView() {
37         return new ModelAndView("pc/cms/article/article/zzxView");
38     }
View Code

前端程式碼:

  使用者輸入資料:

 1 ##註冊介面內容
 2 
 3 #if($!msg == "查詢的使用者不存在或者資料為空,請重新輸入!")
 4 <script>
 5     alert('$!msg');
 6 </script>
 7 #end
 8 <div class="container main-content2" style="text-align: center">
 9     <div class="row clearfix">
10         ## from表單進行跳轉到具體的查詢頁面
11         <form class="login-form" id="loginForm" method="post" action="#rootPath("myTest.htm")"
12               enctype="multipart/form-data">
13             <div class="form-group">
14                 <label class="form-label" for="name"><span class="star"></span>姓名</label>
15                 <input type="text" class="form-control input" id="name" name="name" placeholder="請輸入您的姓名"/>
16             </div>
17 
18             <div class="form-group">
19                 <button type="submit" class="btn btn-login">立即查詢</button>
20             </div>
21         </form>
22         <div class="marked-words">
23             <strong id="message-marked-words"></strong>
24         </div>
25     </div>
26 </div>
View Code

  資料展示:

 1 <div id="myTabContent" class="tab-content" style="text-align: center">
 2         <table border="1" cellpadding="3" cellspacing="0" style="width: 60%;margin:auto">
 3             <thead style="text-align:center">
 4             <tr>
 5                 <th>ID</th>
 6                 <th>登入名</th>
 7                 <th>使用者名稱</th>
 8                 <th>身份證號</th>
 9                 <th>手機號</th>
10                 <th>學歷</th>
11                 <th>職業</th>
12                 <th>郵箱</th>
13             </tr>
14             </thead>
15             <tbody style="text-align: center">
16                 #*#if($!msg == "查詢的使用者不存在或者資料為空")
17                 <script>
18                     alert('$!msg');
19                 </script>
20                 #else*#
21                 ## 能夠直接進來的說明使用者資料存在
22                 #foreach($!mem in $!msg)
23                 <tr>
24                     <td>$!mem.Id</td>
25                     <td>$!mem.F_loginname</td>
26                     <td>$!mem.f_username</td>
27                     <td>$!mem.f_id_num</td>
28                     <td>$!mem.f_mobile</td>
29                     <td>$!mem.f_education</td>
30                     <td>$!mem.f_major</td>
31                     <td>$!mem.f_email</td>
32                 </tr>
33                 #end
34                 ###end
35             </tbody>
36         </table>
37 </div>
View Code

測試:

查詢到使用者(使用者展示)

未查詢到使用者: