-
1. Re: Rich:Calendar - String backing bean version 4.1.0-FINAL
popo_joe Jan 27, 2012 11:10 AM (in response to popo_joe)Any kind of help, or tips could be appriciated thx.
-
2. Re: Rich:Calendar - String backing bean version 4.1.0-FINAL
giberius Jan 27, 2012 5:41 PM (in response to popo_joe)Indeed strange and I run into this kind of issue but different:
Perphaps a custom date converter might solve your problem. It is an atribute of the date field:
converter ="LaDateConverter" Then implement a Java Class:
@FacesConverter("LaDateConverter")
public class LaDateConverter implements Converter {
You have to override:
public Object getAsObject(FacesContext context, UIComponent component, String value) {
public String getAsString(FacesContext context, UIComponent component, Object value) {
As format mask I used: sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
It it no use to give you my converter while it is specific to solve my use case where I switch between query mode, where all fields are strings and normal mode. Only with the date field I got stuck while no trigger fires. I will raise an enhencement request later but the date converter solved the issue with the date format mask.
-
3. Re: Rich:Calendar - String backing bean version 4.1.0-FINAL
popo_joe Jan 30, 2012 4:53 AM (in response to giberius)hello Tom,
Thanks alot for your answer. It helped me alot,when I implemented your solution, I made the mistake to return a Date on the function getAsObject(FacesContext context, UIComponent component, String value); getAsObject and getAsString should both return a String, because my backing value is a String.
Is there a way to retrieve the userPattern from the calendar itself?, instead of using a hardcoded pattern, how can I use the one set in the calendar tag???
<rich:calendar converter="#{CalendarDateStringConverter}" datePattern="dd/MM/yyyy HH:mm" rendered="#{item.type.equals('Date')}" value="#{valueForType}" locale="#{access.calendarLocal}" inputSize="14"> </rich:calendar> I put my code here (for people who get cought in the same trap) @FacesConverter("CalendarDateStringConverter") @Named("CalendarDateStringConverter") public class CalendarDateStringConverter implements Converter { private String pattern = "dd/MM/yyyy HH:mm"; @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { String result = ""; if (value != null && value.length() > 0) { try { //SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"); SimpleDateFormat sdf = new SimpleDateFormat(pattern); Date date = sdf.parse(value); result = sdf.format(date); } catch (ParseException ex) { Logger.getLogger(CalendarDateStringConverter.class.getName()).log(Level.SEVERE, null, ex); } } return result; } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { String result = ""; String valueStr = (String) value; if (valueStr!= null && valueStr.length() > 0) { try { SimpleDateFormat sdf = new SimpleDateFormat(pattern); Date date = sdf.parse(valueStr); result = sdf.format(date); } catch (ParseException ex) { Logger.getLogger(CalendarDateStringConverter.class.getName()).log(Level.SEVERE, null, ex); } } return result; } }
-
4. Re: Rich:Calendar - String backing bean version 4.1.0-FINAL
giberius Jan 30, 2012 12:41 PM (in response to popo_joe)1 of 1 people found this helpfulHi John,
Good to hear it solved your problem. I have no idea how to get the date pattern from the calendar object. However I saw that you set the datePattern attribute in rich:calendar and that you could use this datepatternr in the converter (I did not know that). If this is the case you might try:
1) Set the datePattern in your bean, e.g. datePattern={myBean.datePattern}
2) Inject you bean in the converter with CDI, e.g
@Inject
protected MyBean myBean;
You can use CDI if you have defined your beans with @Named(value = "myBean")
If you can inject your bean you can have the datepattern in your converter: myBean.getDatePattern
-
5. Re: Rich:Calendar - String backing bean version 4.1.0-FINAL
popo_joe Jan 31, 2012 5:22 AM (in response to giberius)Hi Tom,
Thanks again for your answer, I wanted to give the freedom to anyone using a calendar object to set his pattern on the JSF page, then the converter will take care of everything by retrieving the pattern set by the user into the calendar object.
isn't it possible to retrieve that string pattern from the component ? could be very useful.