日期:2014-05-16  浏览次数:20310 次

JSF入门十(自定义转换器)

本文参考

http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=2630

?

除了使用标准的转换器之外,您还可以自行定制您的转换器,您可以实现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类:

package bruce.zhao.model;

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

package bruce.zhao.model;

public class GuestBean {
	private User user;

	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}
}

?这个Bean上的属性直接传回或接受User类型的参数,我们来实现一个简单的转换器,为HTTP字符串与User对象进行转换:

package bruce.zhao.model;

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

?实现完成这个转换器,我们要告诉JSF这件事,这是在faces-config.xml中完成注册:

<?xml version="1.0" encoding="UTF-8"?>
<!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>bruce.zhao.model.User</converter-id>
		<converter-class>
			bruce.zhao.model.UserConverter
	</converter-class>
	</converter>
	<managed-bean>
		<managed-bean-name>guest</managed-bean-name>
		<managed-bean-class>
			bruce.zhao.model.GuestBean
	</managed-bean-class>
		<managed-bean-scope>session</managed-bean-scope>
	</managed-bean>
</faces-config>

?注册转换器时,需提供转换器标识(Converter ID)与转换器类,接下来要在JSF页面中使用转换器的话,就是指定所要使用的转换器标识,例如:

<%@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=GB2312"%>
<f:view>
	<html>
	<head>
	<title>自定义转换器</title>
	</head>
	<body>
	Guest名称是:
	<b> <h:outputText value="#{guest.user}"
		converter="bruce.zhao.model.User" /> </b>
	<h:form>
		<h:inputText id="userField" value="#{guest.user}"
			converter="bruce.zhao.model.User" />
		<h:message for="userField" style="color:red" />
		<br>
		<h:commandButton value="送出" action="show" />
	</h:form>
	</body&