JSF---->自订转换器
除了使用标准的转换器之外,您还可以自行定制您的转换器,您可以实作javax.faces.convert.Converter接口,这个接口有两个要实作的方法:
public Object getAsObject(FacesContext context, UIComponent component, String str); public String getAsString(FacesContext context, UIComponent component,Object obj);
简单的说,第一个方法会接收从客户端经由HTTP传来的字符串数据,您在第一个方法中将之转换为您的自订对象,这个自订对象将会自动设定给您指定的Bean对象;第二个方法就是将从您的Bean对象得到的对象转换为字符串,如此才能藉由HTTP传回给客户端。
直接以一个简单的例子来作说明,假设您有一个User类别:
User.java
package wsz.ncepu; public class User { private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
这个User类别是我们转换器的目标对象,而您有一个GuestBean类别
GuestBean.java
package wsz.ncepu; public class GuestBean { private User user; public void setUser(User user) { this.user = user; } public User getUser() { return user; } }
这个Bean上的属性直接传回或接受User型态的参数,我们来实作一个简单的转换器,为HTTP字符串与User对象进行转换:一个简单的转换器,为HTTP字符串与User对象进行转换:
UserConverter.java
package wsz.ncepu; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.ConverterException; public class UserConverter implements Converter { public Object getAsObject(FacesContext context, UIComponent component, String str) throws ConverterException { String[] strs = str.split(","); User user = new User(); try { user.setFirstName(strs[0]); user.setLastName(strs[1]); } catch (Exception e) { throw new ConverterException(); } return user; } public String getAsString(FacesContext context, UIComponent component, Object obj) throws ConverterException { String firstName = ((User) obj).getFirstName(); String lastName = ((User) obj).getLastName(); return firstName + "," + lastName; } }
注册转换器时,需提供转换器识别(Converter ID)与转换器类别,
<?xml version="1.0"?> <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN" "http://java.sun.com/dtd/web-facesconfig_1_0.dtd"> <faces-config> <navigation-rule> <from-view-id>/*</from-view-id> <navigation-case> <from-outcome>show</from-outcome> <to-view-id>/pages/index.jsp</to-view-id> </navigation-case> </navigation-rule> <converter> <converter-id>userConverter</converter-id> <converter-class>wsz.ncepu.UserConverter</converter-class> </converter> <managed-bean> <managed-bean-name>guest</managed-bean-name> <managed-bean-class>wsz.ncepu.GuestBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> </faces-config>
接下来要在JSF页面中使用转换器的话,就是指定所要使用的转换器识别,
例如:index.jsp
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%> <%@page contentType="text/html;charset=utf-8"%> <f:view> <html> <head> <title>自订转换器</title> </head> <body> Guest名称是: <b> <h:outputText value="#{guest.user}" converter="userConverter" /> </b> <h:form> <h:inputText id="userField" value="#{guest.user}"> <f:converter converterId="userConverter" /> </h:inputText> <h:message for="userField" style="color:red" /> <br> <h:commandButton value="送出" action="show" /> </h:form> </body> </html> </f:view>
也可以<f:converter>卷标并使用converterId属性来指定转换器
结果如下:
除了向JSF注册转换器之外,还有一个方式可以不用注册,就是直接在Bean上提供一个取得转换器的方法,例如
package wsz.ncepu; import javax.faces.convert.Converter; public class GuestBean { private User user; private Converter converter = new UserConverter(); public void setUser(User user) { this.user = user; } public User getUser() { return user; } public Converter getConverter() { return converter; } }
之后可以直接结合 JSF Expression Language 来指定转换器:
<h:inputText id="userField" value="#{guest.user}" converter="#{guest.converter}"/>