Pass backing bean field/variable on URL – JSF

Posted: November 28, 2012 in web

step1.xhtml

<p:commandButton action="#{bean.sendVIN(vin)}" value="Step2" />

bean

    private Car car;

    public String sendID(String vin) {
        return String.format("step2?faces-redirect=true&vin=%s", vin);
    }

step2.xthml

	<f:metadata>
		<f:viewParam name="vin" value="#{bean.car}"
			converter="#{carConverter}" />
	</f:metadata>

converter

@Named
@RequestScoped
@FacesConverter(forClass = Car.class)
public class CarConverter implements Converter {

    @Inject
    List<Car> cars;

    public CarConverter() {
        super();
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (cars == null) {
            return null;
        }
        for (Car car : cars) {
            if (car.getVIN().equals(value)) {
                return car;
            }
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object object) {
        if (object == null) {
            return null;
        } else {
            return ((Car) object).getVIN();
        }
    }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s