0
votes

I have a simple JSF-Page and want to pass a parameter from page1 to page2. Every page has its own controller which is in request scope.

The two controllers look like this: Controller1:

@Component("requestController1")
@Scope("request")
public class RequestController1 {

private List<Product> products;
private Product selectedProduct;

@PostConstruct
public void init() {
       //load products from db
}

public void setProducts(List<Product> products) {
    this.products = products;
}

public List<Product> getProducts() {
    return products;
}

public void setSelectedProduct(Product selectedProduct) {
    this.selectedProduct = selectedProduct;
}

public Product getSelectedProduct() {
    return selectedProduct;
}

Controller2:

@Component("requestController2")
@Scope("request")
public class RequestController2 {

private Long selectedProductId;

    @PostConstruct
public void init() {
selectedProductId = (Long) FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("selectedProductId");
}

    public void setSelectedProductId(Long selectedProductId) {
    this.selectedProductId = selectedProductId;
}

public Long getSelectedProductId() {
    return selectedProductId;
}
}

Now I want to navigate from page1 to page2 with passing the selectedProductId to controller2. Please note that both controllers are in request-scope and I want to use a commandButton to do the navigation.

<h:commandButton value="Continue" action="requestParam2.jsf?faces-redirect=true">
                <f:param name="selectedProductId" value="#{requestController1.selectedProduct.id}"></f:param>
            </h:commandButton>

It seems that f:param does not work here. How can I achieve this?

1

1 Answers

0
votes

Try this: <h:commandButton value="Continue" action="requestParam2.jsf?faces-redirect=true&selectedProductId=#{requestController1.selectedProduct.id}"/>

In JSF1 h:commandButton didn't supported f:param tags - if I remember correctly. You can always use a h:commandLink and use some CSS styling.

Also, you need to call getRequestParameterMap() to read request parameters. getRequestMap() gets entries from the servlet request scope - not exactly what you want.