:::: MENU ::::
Browsing posts in: Java Server Faces

Cómo usar un convertidor en JSF 2.0

import java.util.Stack;

package escuela.convert;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
/** * * @author José Said Olano García */@FacesConverter(value="myDateConverter")
public class MyDateConverter implements Converter {
public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
        String pattern = "dd/MM/yyyy";        SimpleDateFormat sdf = new SimpleDateFormat(pattern);        Date nDate;        try {            nDate = sdf.parse(value);        } catch (ParseException ex) {            FacesMessage message = new FacesMessage();            message.setDetail("Date is missing or not valid");            message.setSummary("Date is missing or not valid");            message.setSeverity(FacesMessage.SEVERITY_ERROR);            throw new ConverterException(message);        }        if (nDate.getTime() > new Date().getTime()) {            FacesMessage message = new FacesMessage();            message.setDetail("Date is later than current date");            message.setSummary("Date is later than current date");            message.setSeverity(FacesMessage.SEVERITY_ERROR);            throw new ConverterException(message);        }        return nDate;    }
//aqui tengo que formatear para que se vea bonito..    public String getAsString(FacesContext facesContext, UIComponent uIComponent, Object value) {        //return value.toString();        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");        return sdf.format(value);    }}