binder.registerCustomEditor 方法的示例:springmvc之 特殊字段的注入

来源:互联网 发布:水果罐头品牌知乎 编辑:程序博客网 时间:2024/05/24 01:38
在学习BaseCommandController时,我们知道,当提交表单时,controller会把表单元素注入到command类里,但是系统注入的只能是基本类型,如int,char,String。但当我们在command类里需要复杂类型,如Integer,date,或自己定义的类时,controller就不会那么聪明了。这时,就需要我们帮助他们了。
一般的做法是在自己的controller里override initBinder()方法。这里我们修改一下学习SimpleFormController的那个例子。

先看一下command类:
public class User { private String account; private String phone; private Integer age; private String city; private Date birthday; private Date createTime;  public Date getBirthday() {  return birthday; } public void setBirthday(Date birthday) {  this.birthday = birthday; } public String getAccount() {  return account; } public void setAccount(String account) {  this.account = account; } public Integer getAge() {  return age; } public void setAge(Integer age) {  this.age = age; } public String getPhone() {  return phone; } public void setPhone(String phone) {  this.phone = phone; } public Date getCreateTime() {  return createTime; } public void setCreateTime(Date createTime) {  this.createTime = createTime; } public String getCity() {  return city; } public void setCity(String city) {  this.city = city; }}
可以看出,比以前多了一个birthday属性,这是需要用户填写的,它的类型是date。此外,我们还把age改成Integer型了。这两个属性系统是不会自动注入。

好了,在UserController里覆盖initBinder()方法:

import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.springframework.validation.BindException;import org.springframework.web.bind.ServletRequestDataBinder;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.SimpleFormController;import com.yangsq.domain.City;import com.yangsq.domain.User;import com.yangsq.validator.DateEditor;import com.yangsq.validator.IntegerEditor;public class UserController extends SimpleFormController {  protected void initBinder(HttpServletRequest req, ServletRequestDataBinder binder) throws Exception {  binder.registerCustomEditor(Integer.class, "age", new IntegerEditor());  binder.registerCustomEditor(Date.class, "birthday", new DateEditor()); } protected Map referenceData(HttpServletRequest req) throws Exception {  Map map = new HashMap();    List cityList = new ArrayList();  City city1 = new City();  city1.setCityName("BeiJing");  city1.setCityNo("010");  cityList.add(city1);  City city2 = new City();  city2.setCityName("ShangHai");  city2.setCityNo("020");  cityList.add(city2);    map.put("cityList", cityList);  return map; } protected void onBind(HttpServletRequest req, Object obj) throws Exception {  User user = (User) obj;  //format the infor     user.setAccount(user.getAccount().trim());     user.setPhone(user.getPhone().trim()); } protected void onBindAndValidate(HttpServletRequest req, Object obj, BindException err) throws Exception {  User user = (User) obj;  user.setCreateTime(new Date()); } protected ModelAndView onSubmit(Object obj) throws Exception {  User user = (User) obj;  return new ModelAndView(this.getSuccessView(), "user", user); }}
红色标出的是新加入的方法,其他不变。可以看出使用了binder.registerCustomEditor方法,它是用来注册的。所谓注册即告诉Spring,注册的属性由我来注入,不用你管了。可以看出我们注册了“age”和“birthday”属性。那么这两个属性就由我们自己注入了。那么怎么注入呢,就是使用IntegerEditor()DateEditor()方法。希望仔细思考一下registerCustomEditor方法的参数含义。

下面是IntegerEditor()方法:

import java.beans.PropertyEditorSupport;public class IntegerEditor extends PropertyEditorSupport { public String getAsText() {  Integer value = (Integer) getValue();  if(null == value){   value = new Integer(0);  }  return value.toString(); } public void setAsText(String text) throws IllegalArgumentException {  Integer value = null;  if(null != text && !text.equals("")){   value = Integer.valueOf(text);  }  setValue(value); }}

下面是DateEditor()方法:

import java.beans.PropertyEditorSupport;import java.text.SimpleDateFormat;import java.util.Date;public class DateEditor extends PropertyEditorSupport { public String getAsText() {  Date value = (Date) getValue();  if(null == value){   value = new Date();  }  SimpleDateFormat df =new SimpleDateFormat("yyyy-MM-dd");  return df.format(value); } public void setAsText(String text) throws IllegalArgumentException {  Date value = null;  if(null != text && !text.equals("")){   SimpleDateFormat df =new SimpleDateFormat("yyyy-MM-dd");   try{    value = df.parse(text);   }catch(Exception e){    e.printStackTrace();   }  }  setValue(value); }}
getAsTextsetAsText是要从新定义的。其中getAsText方法在get方法请求时会调用,而setAsText方法在post方法请求时会调用。



0 0
原创粉丝点击