`
yingfang05
  • 浏览: 123442 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

JSF里自订转换器

    博客分类:
  • 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
User.java
package onlyfun.caterpillar;

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
GuestBean.java
package onlyfun.caterpillar;

public class GuestBean {
    private User user;

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

    public User getUser() {
        return user;
    }
}
  这个Bean上的属性直接传回或接受User型态的参数,我们来实作一个简单的转换器,为HTTP字符串与User对象进行转换:

UserConverter.java
UserConverter.java
package onlyfun.caterpillar;

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中完成注册:

faces-config.xml
faces-config.xml
<?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>onlyfun.caterpillar.User</converter-id>
        <converter-class>
            onlyfun.caterpillar.UserConverter
        </converter-class>
    </converter>

    <managed-bean>
        <managed-bean-name>guest</managed-bean-name>
        <managed-bean-class>
            onlyfun.caterpillar.GuestBean
        </managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
</faces-config>
  注册转换器时,需提供转换器识别(Converter ID)与转换器类别,接下来要在JSF页面中使用转换器的话,就是指定所要使用的转换器识别,例如:

index.jsp
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=Big5"%>

<f:view>

<html>
<head>
<title>自订转换器</title>
</head>
<body>

Guest名称是:<b>
           <h:outputText value="#{guest.user}"
                   converter="onlyfun.caterpillar.User"/>
           </b>

    <h:form>
        <h:inputText id="userField"
                     value="#{guest.user}"
                     converter="onlyfun.caterpillar.User"/>
        <h:message for="userField" style="color:red"/>
        <br>
        <h:commandButton value="送出" action="show"/>
    </h:form>
</body>
</html>

</f:view>
  您也可以<f:converter>卷标并使用converterId属性来指定转换器,例如:

<h:inputText id="userField" value="#{guest.user}">
     <f:converter converterId="onlyfun.caterpillar.User"/>
</h:inputText>
  除了向JSF注册转换器之外,还有一个方式可以不用注册,就是直接在Bean上提供一个取得转换器的方法,例如:

GuestBean.java
GuestBean.java
package onlyfun.caterpillar;

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}"/>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics