问题
I want to transfer a parameter from a jsf page to another jsf page. Like this:
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
public String action() {
HttpServletRequest request = (HttpServletRequest) FacesContext
.getCurrentInstance().getExternalContext().getRequest();
String param = request.getParameter("id");
return "b?id=" + param;
}
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:
return "b?id=" + param;
And the scope of ManagedBean is request. How can I do to solve this problem? Thanks.
回答1:
You could also try 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.
- 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}
You could pass the id on outcome link (b.xhtml?id=1, for example) and retrieve it on any Managed Bean.
回答2:
If you are using JSF 2 or EL 2.2 you can pass it as a parameter to the method
<h:commandLink class="navi" value="press" action="#{Bean.action(5555)}" />
回答3:
If your bean is session scoped, this is easy.
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
<h:inputText value=#{bean.param} />
来源:https://stackoverflow.com/questions/16622281/how-to-transfer-parameter-from-managedbean-to-a-jsf-page