4

Here my JSF page:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">

    <f:metadata>  
        <f:viewParam name="id" value="#{productDetailBean.id}" />  
    </f:metadata> 

    <h:head>
        <title>Facelet Title</title>
    </h:head>

    <h:body>
        <h:outputText value="1=#{productDetailBean.id}" />
        <br/>
        <h:outputText value="2=#{param['id']}" />
        <br/>
        <h:outputText value="3=#{productDetailBean.param}" />
    </h:body>
</html>

And bean

import java.io.Serializable;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@ViewScoped
public class ProductDetailBean implements Serializable{  

    private String id;  

    public String getParam(){
        FacesContext context = FacesContext.getCurrentInstance();
        Map<String, String> paramMap = context.getExternalContext().getRequestParameterMap();
        String projectId = paramMap.get("id");
        return projectId;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
} 

When i passing parameters like this: /getshipment.xhtml?id=123
i get output

1=
2=123
3=123

Second and third way are working fine. Why first one is not working ? And what is the correct way to get parameter ?

7
  • So where are you actually populating the id property of the bean? Commented Feb 25, 2014 at 15:32
  • ?id=123 in URL should set "123" value to ProductDetailBean.id And <h:outputText value="1=#{productDetailBean.id}" /> print this value, but nothing. Commented Feb 25, 2014 at 15:46
  • Let me rephrase: what part of your code actually puts the request parameter physically in the ID object property? The answer is: not any code is doing that. No wonder you get an empty value if you don't properly initialize it. Commented Feb 25, 2014 at 15:55
  • <f:viewParam name="id" value="#{productDetailBean.id}"/> shoud do all work , like shown here Commented Feb 25, 2014 at 16:03
  • ... wow, I need to have my eyes examined, my apologies. So the question is: why is that f:viewParam not doing what it is supposed to be doing. I'm afraid I have no answer. What happens when you put breakpoints or System.out.println() statements inside your getId() / setId() methods? EDIT: this may be relevant: stackoverflow.com/questions/18025508/… Commented Feb 25, 2014 at 16:12

1 Answer 1

3

Try going with at least 2.2.2 with GlassFish 4. Prior 2.2.x Mojarra releases have well known compatibility issues with the new http://xmlns.jcp.org/jsf/ JSF namespaces.

See also:

Sign up to request clarification or add additional context in comments.

1 Comment

Updated Mojarra to 2.2.5 and everything is forking fine. Thank you. I am very appreciate your help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.