简体   繁体   English

如何将参数从ManagedBean传输到jsf页面

[英]how to transfer parameter from managedbean to a jsf page

I want to transfer a parameter from a jsf page to another jsf page. 我想将参数从jsf页面传输到另一个jsf页面。 Like this: 像这样:

a.xhtml a.xhtml

<h:form>
    <h:commandLink class="navi" value="press"
               action="#{Bean.action}">
        <f:param name="id" value="5555" />
    </h:commandLink>
</h:form>

Bean.java Bean.java

public String action() {
    HttpServletRequest request = (HttpServletRequest) FacesContext
            .getCurrentInstance().getExternalContext().getRequest();
    String param = request.getParameter("id");
    return "b?id=" + param;
}

b.xhtml b.xhtml

<h:inputText value=#{param.id} />

By previous way, I transfer id from a.xhtml to b.xhtml, but I don't want to expose the parameter like "...b.xhtml?id=5555" outside because of this line: 通过以前的方式,我将id从a.xhtml传输到b.xhtml,但是由于以下原因,我不想在外部公开“ ... b.xhtml?id = 5555”之类的参数:

return "b?id=" + param;

And the scope of ManagedBean is request. 而ManagedBean的范围是请求。 How can I do to solve this problem? 如何解决这个问题? Thanks. 谢谢。

如果您使用的是JSF 2或EL 2.2,则可以将其作为参数传递给方法

<h:commandLink class="navi" value="press" action="#{Bean.action(5555)}" />

You could also try viewParam 您也可以尝试viewParam

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

It does basically the following: 它基本上执行以下操作:

  • Get the request parameter value by name id. 通过名称ID获取请求参数值。
  • Convert and validate it if necessary (you can use required, validator and converter attributes and nest a and in it like as with ) 如有必要,对其进行转换和验证(您可以使用必需的,验证器和转换器属性,并像一样在其中嵌套和)
  • If conversion and validation succeeds, then set it as a bean property represented by #{bean.id} 如果转换和验证成功,则将其设置为由#{bean.id}表示的bean属性。

You could pass the id on outcome link (b.xhtml?id=1, for example) and retrieve it on any Managed Bean. 您可以在结果链接上传递id(例如b.xhtml?id = 1),然后在任何Managed Bean上检索它。

If your bean is session scoped, this is easy. 如果您的bean是会话作用域的,这很容易。

Bean.java Bean.java

private String param;

public String action() {
    HttpServletRequest request = (HttpServletRequest) FacesContext
            .getCurrentInstance().getExternalContext().getRequest();
    param = request.getParameter("id");
    return "b?id=" + param;
}

public String getParam() {
    return param;
}

xhtml 的xhtml

<h:inputText value=#{bean.param} />

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

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