【问题标题】:getAsObject not called未调用 getAsObject
【发布时间】:2012-09-28 04:29:44
【问题描述】:

我想要做的是我正在使用timeMillis 以毫秒为单位存储时间的属性(我使用System.currentTimeMillis())并在从当前时间。主要问题是每当转换器timeConverter 只调用了getAsString函数,没有调用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;

    }

}

【问题讨论】:

    标签: jsf jsf-2 converter


    【解决方案1】:

    为什么会出现这样的问题?

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

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

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

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

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

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

    @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.
    }
    

    【讨论】:

      猜你喜欢
      • 2013-05-02
      • 2013-12-18
      • 1970-01-01
      • 2011-11-28
      • 2011-12-05
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多