Betty
Betty

Reputation: 67

JSF unnable to get parameter from URL

I try to get a parameter from the URL but I can't. I do this in my jsf

   <f:metadata>  
   <f:viewParam name="key" value="#{confirmationMB.key}"  required="true"   />  
   <f:viewAction action="#{confirmationMB.confirmer()}"/>  
   </f:metadata>

and this in my managed bean :

@ManagedBean
@ViewScoped
public class confirmationMB {
private String key;  public String getKey() {
    return key;
}

public void setKey(String key) {
    this.key = key;
}
public void confirmer(){

    System.out.println("the key is   "+key);

}

my URL is like this:

http://localhost:8080/exempler/validate_inscription.xhtml?key=124he

I get always null as value in System.out..can someone help me please ?

Edit: when I try <h:outputText value="#{param['key']}" /> in xhtml I can get the key but I don't know how I can insert it into my Bean !!

Upvotes: 1

Views: 821

Answers (2)

Betty
Betty

Reputation: 67

It s ok I find a solution to my problem : I ve add this lines into my managedBean and I can get now the key from the URL

HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();   
    String val = (String)req.getParameter("key");

I put this solution to help people who may have the same problem

Upvotes: 2

sQer
sQer

Reputation: 1151

Use:

<f:metadata>
    <f:viewParam name="key" value="#{confirmationMB.key}" />
    <f:event type="preRenderView"
        listener="#{confirmationMB.confirmer()}" />
</f:metadata>

Upvotes: 0

Related Questions