1

我想要做的是我正在获取timeMillis以毫秒为单位存储时间的属性(我得到了我的使用System.currentTimeMillis()),并在从当前时间减去它后将其转换为等效的天、小时、分钟和秒。主要问题是,每当调用转换器时,timeConvertergetAsString调用函数,getAsObject而不调用。

这是我的 xhtml 文件中导致转换器无法正常运行的部分。

<c:forEach var="p" items="#{statusBean.statusList}">
    <h:form>
        <div class="status">
            <h:commandLink action="#{friendBean.gotoFriendProfile(p.email)}">
                <img src="../images/profilePicture/#{p.picture}" style="height: 29px; width: 29px; "/>
                <h:outputText value="#{p.statusBy}:"/>
            </h:commandLink>
            <h:outputText value="#{p.statusmsg}"/>
            <h:outputText value="#{p.timeMillis}">
                <f:converter converterId="timeConverter"/>
            </h:outputText>
            <br/>
            <c:forEach var="q" items="#{statusBean.commentList(p.statusId)}">
            <div class="barcomment">
                <br/>
                <h:commandLink action="#{friendBean.gotoFriendProfile(q.email)}">
                    <img src="../images/profilePicture/#{q.picture}" style="height: 29px; width: 29px; "/>
                    <h:outputText value="#{q.commentBy}:"/>
                </h:commandLink>
                <h:outputText value=" #{q.comment}"/>
            </div>
        </c:forEach>
        <br/>
        <div class="comment">
            <p:inputText value="#{statusBean.comment.comment}" styleClass="box"  />
            <p:commandLink  value="Views" action="#{statusBean.update(p.statusId)}" ajax="false" styleClass="link"/>
        </div> 

这是我编写的 timeConverter 类。

package com.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

public class TimeConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
        System.out.println("inside getAsObject");
        long time=Integer.parseInt(arg2);
        long currentTime=System.currentTimeMillis();
        long eclapseTime=time-currentTime;
        long secs=eclapseTime/1000;
        long days=secs/(60*60*24);
        long hours=(secs%(60*60*24))/60*60;
        long mins=(secs%(60*60*24)%(60*60))/60;
        long secs2=(secs%(60*60*24)%(60*60)%(60));
        StringBuffer sb = new StringBuffer();
        sb.append(days).append("days").append(hours).append("hours").append(mins).append("mins").append(secs2).append("secs");
        String object1 = sb.toString();
        return object1;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component,
            Object value) {
        System.out.println("inside getAsString");
        String value1 = value.toString();
        return value1;

    }

}
4

1 回答 1

1

为什么这是一个问题?

您仅在UIOutput组件中使用转换器:

<h:outputText value="#{p.timeMillis}">
    <f:converter converterId="timeConverter"/>
</h:outputText>

getAsString()调用以将Object模型值转换为String可以嵌入到生成的 HTML 输出中的 a(您知道,您不能将 Java 对象简单地放在 HTML 字符串中)。

但是,您无法在UIInput类似的组件中使用它<h:inputText>,因此无法将提交的String值转换为模型中所需Object的值,因此getAsObject()显然永远不会调用。

一切都按设计工作。看起来您的具体问题是您实际上应该执行您在其中所做的getAsObject()工作getAsString()

我认为如果你给这些方法一个更合理的参数名称会有所帮助:

@Override
public String getAsString(FacesContext context, UIComponent component, Object modelValue) throws ConverterException {
    // Write code here which converts the model value to display value.
    // This method will be used when generating HTML output.
}

@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) throws ConverterException {
    // Write code here which converts the submitted value to model value.
    // This method will be used when processing submitted input values.
}
于 2012-09-27T19:32:29.287 回答