I'm very to to jsf and Primefaces. My problem is, if I do not comment currentLunch = new Lunch(...)
in *IndexBean.java` out, I'll get the error message which I've append below.
Without this line of code, the code will run.
Why I do that?
I want to add a new lunch instance in case of a 'save' click. That's why the 'add'-form has an empty instance of a lunch object. Is this way okay?
I checked the following Stackoverflow posts but I think, their solution would not solve my problem
Error log
1100: JSF1073: java.lang.IllegalArgumentException caught during processing of RENDER_RESPONSE 6 : UIComponent-ClientId=, Message=Cannot format given Object as a Date
Code
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);
}
}