What is the correct way to get parameter from URL in JSF

纵然是瞬间 提交于 2019-12-01 20:43:40

问题


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 ?


回答1:


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:

  • f:viewParam doesn't pass required parameter when new xmlns.jcp.org namespace is used


来源:https://stackoverflow.com/questions/22018409/what-is-the-correct-way-to-get-parameter-from-url-in-jsf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!