SpringMVC初始化參數綁定--日期格式

來源:互聯網
上載者:User

標籤:control   map   技術分享   pat   sim   技術   editor   表示   java   

一、初始化參數綁定[一種日期格式]

配置步驟:

①:在applicationcontext.xml中只需要配置一個包掃描器即可

<!-- 包掃描器 -->     <context:component-scan base-package="cn.happy.controller"></context:component-scan>

②:在處理器類中配置Binder 方法  使用@InitBinder註解

在這裡首先註冊一個使用者編輯器 參數一為目標類型   propertyEditor為屬性編輯器,此處我們選用 CustomDateEditor屬性編輯器,

參數一為想轉換的日期格式,參數二表示是否允許為空白

@Controllerpublic class MyController {//匹配單個@InitBinderpublic void initData(WebDataBinder wdb){wdb.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));}@RequestMapping(value="/first.do")public String doFirst(Date birthday,int age){return "/welcome.jsp";}}

③ 定製jsp頁面:

 <form action="${pageContext.request.contextPath }/first.do" method="post">    <h1>參數綁定轉換器</h1>                 出生日期:<input name="birthday" value="${birthday}"/><br/><br/>                 年齡:<input name="age" value="${age }"/><br/><br/>       <input type="submit" value="註冊"/>    </form>

實現效果:

二、多日期的綁定

①自訂的屬性編輯器,需要我們繼承PropertiesEditor,重寫裡面的setAsText方法,使用setValue方法賦值

②在處理器類中使用我們自定的屬性編輯器

 

實現效果:

 

 

 

SpringMVC初始化參數綁定--日期格式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.