问题:
演示下怎么定制Converter , FacesConverter
解决:
User.java + Test1.xhmtl + Test1Controller.java + UserConverter.java;
class User:
class User
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
/* * 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
/* * 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); } } |
class UserConverter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
/* * 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(); } } |