Binding more than one input field to a backing bean property by using Java Server Faces?

Three ways: Back or intermediate it by java.util. Calendar with three getters and three setters. Make use of a Converter, this is however going to be a bit hacky.

Make use of a 3rd party component like rich:calendar. Edit: as per the comments, here's how option 2 would look like. Page.

Jsp: mypackage. MyBean: package mypackage; import java.util. ArrayList; import java.util.

Date; import java.util. List; import javax.faces.model. SelectItem; public class MyBean { private static List days = new ArrayList(); private static List months = new ArrayList(); private static List years = new ArrayList(); static { // Just do your thing to fill them.

Only ensure that those are Strings, // else you'll need to change the type in Converter accordingly. For (int I = 1; I ValueOf(i))); for (int I = 1; I Add(new SelectItem(String. ValueOf(i))); } private Date date; public void submit() { // Print submitted date to stdout.

System.out. Println("Submitted date: " + date); } public List getDays() { return days; } public List getMonths() { return months; } public List getYears() { return years; } public Date getDate() { return date; } public void setDate(Date date) { this. Date = date; } } mypackage.

DatePartConverter: package mypackage; import java.text. ParseException; import java.text. SimpleDateFormat; import java.util.

Date; import java.util. Map; import javax.faces.application. FacesMessage; import javax.faces.component.

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

Converter; import javax.faces.convert. ConverterException; public class DatePartConverter implements Converter { public Object getAsObject(FacesContext context, UIComponent component, String value) { String part = (String) component.getAttributes(). Get("part"); Date date = null; if (context.

GetRenderResponse()) { // Convert any default/selected date for display. Date selectedDate = (Date) ((UIInput) component).getValue(); if (selectedDate! = null) { if (("day".

Equals(part) && new SimpleDateFormat("d"). Format(selectedDate). Equals(value)) || ("month".

Equals(part) && new SimpleDateFormat("M"). Format(selectedDate). Equals(value)) || ("year".

Equals(part) && new SimpleDateFormat("yyyy"). Format(selectedDate). Equals(value))) { date = selectedDate; } } } else { // Convert submitted date after submit.

Map map = context. GetExternalContext().getRequestMap(); if ("day". Equals(part)) { map.

Put("DatePartConverter. Day", value); // Save until we have all parts. } else if ("month".

Equals(part)) { map. Put("DatePartConverter. Month", value); // Save until we have all parts.

} else if ("year". Equals(part)) { String day = (String) map. Get("DatePartConverter.

Day"); String month = (String) map. Get("DatePartConverter. Month"); String dateString = String.

Format("%s-%s-%s", day, month, value); try { date = new SimpleDateFormat("d-M-yyyy"). Parse(dateString); } catch (ParseException e) { throw new ConverterException(new FacesMessage(e.getMessage()), e); } } } return date; } } public String getAsString(FacesContext context, UIComponent component, Object value) { // Not relevant here. Just return SelectItem's value.

Return (String) value; } faces-config. Xml datePartConverter mypackage. DatePartConverter myBean mypackage.

MyBean request Note that there's no Validator and that SimpleDateFormat is by default lenient. Thus, selecting for example 31 november would produce 1 december. You may need to implement a DatePartValidator yourself if you want to warn about that.

Thanks for your reply, BalusC (+1) Can you show how do I get my goal bu using approach number 2: Make use of a Converter, this is however going to be a bit hacky – Arthur Ronald F D Garcia Dec 19 '09 at 17:53 I've edited my question with a working sample. – BalusC Dec 20 '09 at 4:01 Note: you really need to put the dropdowns in the order of day-month-year. If you for example want year-month-day, then you'll need to rearrange the if blocks in the converter so that the last part will finally create the date based on the input.

Talking about hacky :) – BalusC Dec 20 '09 at 4:05 Thank you very much. – Arthur Ronald F D Garcia Dec 20 '09 at 10:35.

When saying "binding" and "backing bean", you are supposed to refer to the following: and have private UIInput in your bean. If that is the case - no, you can't bind like that. Well, I'm not sure if you technically can - but it will surely have unexpected effect.

If, however, you want to target a managed bean property, then you can, for example.

Thanks for your reply. Any workaround? – Arthur Ronald F D Garcia Dec 18 '09 at 20:58 why would you need that?

What's the usecase? What's the actual component you want to bind to? – Bozho Dec 18 '09 at 21:01.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions