JSF,Primefaces - '无法将给定对象格式化为日期'如果是教练电话

时间:2014-05-23 19:22:58

标签: java jsf primefaces

我非常喜欢jsf和Primefaces。我的问题是,如果我不在{IndexBean.java`中注释currentLunch = new Lunch(...),我将收到我在下面附加的错误消息。

如果没有这行代码,代码就会运行。

为什么我这样做?

我想添加一个新的午餐实例以防止'保存'单击。这就是为什么'添加' -form有一个午餐对象的空实例。这样可以吗?

我检查了以下Stackoverflow帖子,但我认为,他们的解决方案无法解决我的问题

错误日志

1100: JSF1073: java.lang.IllegalArgumentException caught during processing of RENDER_RESPONSE 6 : UIComponent-ClientId=, Message=Cannot format given Object as a Date

代码

Index.htmlx

<h:form id="newLunch">
        <h:panelGrid columns="2" style="width: 300px;">
            <h:outputLabel for="initiator" value="Initiator" />
            <p:inputText id="initiator" value="#{indexBean.currentLunch.initiator}"/>

            <h:outputLabel for="date" value="Date" />
            <p:calendar id="date" minHour="11" maxHour="13" stepMinute="15" value="#{indexBean.currentLunch.date}"/>

            <p:commandButton value="save" action="#{indexBean.onSaveLunch}" update=":lunches" />
        </h:panelGrid>
</h:form>

IndexBean.java

@ManagedBean
@SessionScoped
public class IndexBean
{
private List<Lunch> lunches;
private Lunch currentLunch;
public List<Lunch> getLunches() ...
public void setLunches(List<Lunch> lunches) ...
public Lunch getCurrentLunch() ...
public void setCurrentLunch(Lunch currentLunch) ...

public IndexBean()
{
    // Set attributes to a value whichs should work (as you can see one line below)
    currentLunch = new Lunch("Tobi", "Starbucks", new DateTime(2014, 5, 30, 12, 0, 0 ,0));

    // This works without any problems.
    lunches = new ArrayList<Lunch>();
    lunches.add(new Lunch("Tobi", "Starbucks", new DateTime(2014, 5, 30, 12, 0, 0 ,0)));
}

public void onSaveLunch() ...

}

Lunch.java

public class Lunch
{
    private String initiator;
    private String location;
    private DateTime date;

    public Lunch(String initiator, String location, DateTime date)
    {
        this.initiator = initiator != null ? initiator : "";
        this.location = location != null ? location : "";
        this.date = date != null ? date : DateTime.now();
    }

    // default getter / setter


    public String getFormattedDate()
    {
        if(date == null) return "";
        DateTimeFormatter fmt = DateTimeFormat.forPattern("hh:MM (dd.MM.yyyy)");
        return date.toString(fmt);
    }
}

1 个答案:

答案 0 :(得分:1)

当您不使用java.util.Date时会出现此问题。检查Lunch班级中的相应字段是否有java.util.Date类型,而不是java.sql.Date或其他类型。

您似乎正在使用DateTime来获取Joda API。要解决此问题而不是修改当前设计,您可能希望在视图中为date字段设置自定义getter / setter。只需将此代码添加到IndexBean

即可
public class IndexBean {
    //current code...
    public Date getDate() {
        if (currentLunch.getDate() != null) {
            return currentLunch.getDate().toDate();
        }
        return null;
    }
    public void setDate(Date date) {
        return currentLunch.setDate(new DateTime(date));
    }
}

将视图更改为:

<!-- Note the expression language in value attribute -->
<p:calendar id="date" minHour="11" maxHour="13" stepMinute="15"
    value="#{indexBean.date}"/>

与您当前的问题无关,但可能@SessionScoped的范围比您需要的范围更宽,可能该bean应为@ViewScoped

此外,最好在@PostConstruct带注释的方法中将任何业务逻辑与bean初始化相关联:

public class IndexBean {
    public IntexBean() {
    }
    @PostConstruct
    public void init() {
        // Set attributes to a value whichs should work (as you can see one line below)
        currentLunch = new Lunch("Tobi", "Starbucks", new DateTime(2014, 5, 30, 12, 0, 0 ,0));
        // This works without any problems.
        lunches = new ArrayList<Lunch>();
        lunches.add(new Lunch("Tobi", "Starbucks", new DateTime(2014, 5, 30, 12, 0, 0 ,0)));
    }
    //rest of code...
}