简体   繁体   English

在JSF中从URL获取参数的正确方法是什么

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

Here my JSF page: 这是我的JSF页面:

<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 当我传递这样的参数时:/getshipment.xhtml?id = 123
i get output 我得到输出

1= 1 =
2=123 2 = 123
3=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 ? 获取参数的正确方法是什么?

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. 尝试使用GlassFish 4至少使用2.2.2。之前的2.2.x Mojarra版本与新的http://xmlns.jcp.org/jsf/ JSF命名空间有着众所周知的兼容性问题。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM