一起寫架構-MVC架構-基礎功能-單一資料型別參數綁定(六),-mvc資料類型

來源:互聯網
上載者:User

一起寫架構-MVC架構-基礎功能-單一資料型別參數綁定(六),-mvc資料類型
功能實現

需求:根據表單的欄位名,如果和參數的變數名一致,並且類型相容,那麼將資料自動封裝到對應的自動上。

包括的支援的資料類型有:

1.基礎資料類型:long、int、double、float、char、byte、boolean、short

2.基礎資料類型的封裝類:Long、Integer、Double、Float、Character、Byte、Boolean、Short

3.字串類型:String

 

 這裡有一個痛點,由於低於JDK1.8的JDK是不支援直接獲得參數名的。即使高於JDK1.8.也需要在編譯時間,添加`-parameters`參數或者啟用偵錯模式`-g`。(麻煩)

JDK不支援,只能通過讀取class 位元組碼來獲得參數名。有興趣的朋友自己看看asm 這個架構。

實現思路

 

 

實現步驟

   

測試代碼

 

 1.測試代碼目錄

2.測試頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>    使用者登入     <form action="${pageContext.request.contextPath }/register.do" method="post">       使用者名稱:<input name="username" type="text" /><br/>       密碼:  <input name="password" type="text" /><br/>       年齡:    <input name="age" type="text" /><br/>         <input type="submit" value="註冊">     </form></body></html>

 

3UserController修改

 1     /** 2      * 註冊頁面 3      * @param username 4      * @param password 5      * @return 6      */ 7     @RequestMapping(value = "/register") 8     public String register(@Param("username") String username,@Param("password") String password ,@Param("age") long age,HttpServletRequest request){ 9         System.out.println("使用者名稱:"+username+",密碼:"+password+",年齡"+age);10         System.out.println(request.getRequestURI());11         return "redirect:/register.jsp";12     }

4.測試結果

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.