2015年11月30日 | Leave a comment 问题: 演示下怎么定制Converter , FacesConverter 解决: User.java + Test1.xhmtl + Test1Controller.java + UserConverter.java; class User: class User /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package me.yek.testjsinsertele; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Objects; import java.util.logging.Logger; /** * * @author sihai */ public class User implements java.io.Serializable { private static final Logger LOG = Logger.getLogger(User.class.getName()); String name; Integer id; Date birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User{" + "name=" + name + ", id=" + id + ", birthday=" + birthday + '}'; } @Override public int hashCode() { int hash = 3; hash = 59 * hash + Objects.hashCode(this.id); return hash; } @Override public boolean equals(Object obj) { LOG.info("User.equals enter..."); if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final User other = (User) obj; if (!Objects.equals(this.id, other.id)) { return false; } return true; } public static List<User> list(){ List<User> ret=new ArrayList<>(); for (int i = 0; i < 5; ++i) { User u = new User(); u.setId(i); u.setName("user " + i); ret.add(u); } return ret; } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package me.yek.testjsinsertele; import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Objects;import java.util.logging.Logger; /** * * @author sihai */public class User implements java.io.Serializable { private static final Logger LOG = Logger.getLogger(User.class.getName()); String name; Integer id; Date birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User{" + "name=" + name + ", id=" + id + ", birthday=" + birthday + '}'; } @Override public int hashCode() { int hash = 3; hash = 59 * hash + Objects.hashCode(this.id); return hash; } @Override public boolean equals(Object obj) { LOG.info("User.equals enter..."); if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final User other = (User) obj; if (!Objects.equals(this.id, other.id)) { return false; } return true; } public static List<User> list(){ List<User> ret=new ArrayList<>(); for (int i = 0; i < 5; ++i) { User u = new User(); u.setId(i); u.setName("user " + i); ret.add(u); } return ret; }} Test1.xhtml <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core"> <h:head> <title>Facelet Title</title> </h:head> <h:body> <h:form> <h:selectOneMenu value="#{test1.selectedUser}"> <f:selectItems value="#{test1.userItemList}" /> </h:selectOneMenu> <h:commandButton action="#{test1.save()}" value="save" /> </h:form> </h:body> </html> 1234567891011121314151617 <?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core"> <h:head> <title>Facelet Title</title> </h:head> <h:body> <h:form> <h:selectOneMenu value="#{test1.selectedUser}"> <f:selectItems value="#{test1.userItemList}" /> </h:selectOneMenu> <h:commandButton action="#{test1.save()}" value="save" /> </h:form> </h:body></html> Test1Controller.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package me.yek.testjsinsertele; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; import javax.faces.convert.Converter; import javax.faces.model.SelectItem; import javax.inject.Named; import javax.faces.view.ViewScoped; /** * * @author sihai */ @Named(value = "test1") @ViewScoped public class Test1Controller implements java.io.Serializable { private static final Logger LOG = Logger.getLogger(Test1Controller.class.getName()); /** * Creates a new instance of Test1Controller */ public Test1Controller() { } public Converter getUserConvert() { return new UserConverter(); } private User selectedUser; public User getSelectedUser() { return selectedUser; } public void setSelectedUser(User selectedUser) { LOG.info("setSelectedUser enter " + selectedUser); this.selectedUser = selectedUser; } public List<SelectItem> getUserItemList() { List<User> userList=User.list(); List<SelectItem> ret = new ArrayList<>(); for (User u : userList) { ret.add(new SelectItem(u, u.getName())); } return ret; } public void save() { LOG.info("save() enter..." + selectedUser); } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package me.yek.testjsinsertele; import java.util.ArrayList;import java.util.List;import java.util.logging.Logger;import javax.faces.convert.Converter;import javax.faces.model.SelectItem;import javax.inject.Named;import javax.faces.view.ViewScoped; /** * * @author sihai */@Named(value = "test1")@ViewScopedpublic class Test1Controller implements java.io.Serializable { private static final Logger LOG = Logger.getLogger(Test1Controller.class.getName()); /** * Creates a new instance of Test1Controller */ public Test1Controller() { } public Converter getUserConvert() { return new UserConverter(); } private User selectedUser; public User getSelectedUser() { return selectedUser; } public void setSelectedUser(User selectedUser) { LOG.info("setSelectedUser enter " + selectedUser); this.selectedUser = selectedUser; } public List<SelectItem> getUserItemList() { List<User> userList=User.list(); List<SelectItem> ret = new ArrayList<>(); for (User u : userList) { ret.add(new SelectItem(u, u.getName())); } return ret; } public void save() { LOG.info("save() enter..." + selectedUser); }} class UserConverter /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package me.yek.testjsinsertele; import java.util.List; import java.util.Objects; import java.util.logging.Logger; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.FacesConverter; /** * * @author sihai */ @FacesConverter(forClass=User.class) public class UserConverter implements Converter { private static final Logger LOG = Logger.getLogger(UserConverter.class.getName()); @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { LOG.info("getAsObject enter ..." + value); List<User> us=User.list(); User ret=null; int userId=Integer.parseInt(value); for(User u : us){ if( Objects.equals(userId, u.getId()) ){ ret=u; break; } } return ret; } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { LOG.info("getAsString enter ..." + value); User u=(User)value; return u.getId().toString(); } } 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package me.yek.testjsinsertele; import java.util.List;import java.util.Objects;import java.util.logging.Logger;import javax.faces.component.UIComponent;import javax.faces.context.FacesContext;import javax.faces.convert.Converter;import javax.faces.convert.FacesConverter; /** * * @author sihai */@FacesConverter(forClass=User.class)public class UserConverter implements Converter { private static final Logger LOG = Logger.getLogger(UserConverter.class.getName()); @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { LOG.info("getAsObject enter ..." + value); List<User> us=User.list(); User ret=null; int userId=Integer.parseInt(value); for(User u : us){ if( Objects.equals(userId, u.getId()) ){ ret=u; break; } } return ret; } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { LOG.info("getAsString enter ..." + value); User u=(User)value; return u.getId().toString(); } }