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

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

配置步驟:
app

①:在applicationcontext.xml中只須要配置一個包掃描器便可jsp

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

②:在處理器類中配置綁定方法  使用@InitBinder註解編輯器

在這裏首先註冊一個用戶編輯器 參數一爲目標類型   propertyEditor爲屬性編輯器,此處咱們選用 CustomDateEditor屬性編輯器,post

參數一爲想轉換的日期格式,參數二表示是否容許爲空spa

@Controller
public class MyController {

	//匹配單個
	@InitBinder
	public 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頁面:component

 <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>

實現效果:orm


2、多日期的綁定xml

①自定義的屬性編輯器,須要咱們繼承PropertiesEditor,重寫裏面的setAsText方法,使用setValue方法賦值blog

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

 

實現效果:

 

 

相關文章
相關標籤/搜索