f:param不适用于查询字符串上的p:commandLink或h:commandLink [英] f:param does not work with p:commandLink or h:commandLink on query string

查看:91
本文介绍了f:param不适用于查询字符串上的p:commandLink或h:commandLink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

f:param适用于h:link,但不适用于p:commandLinkh:commandLink.

f:param works great with h:link, but not with p:commandLink or h:commandLink.

例如,我有两个页面test_first.xhtmltest_second.xhtml,以及一个后备Java bean TestBean.java.

For example, I have two pages test_first.xhtml and test_second.xhtml, and a backing java bean TestBean.java.

我开始运行test_first.xhtml.

如果我单击link1(即h:link),则页面将重定向到test_second.xhtml.在f:param的帮助下,浏览器的地址栏将显示.../test_second.xhtml?id=1.在该页面上,testBean.userId被打印.

If I click link1, which is a h:link, the page will redirect to test_second.xhtml. With the help of f:param, the address bar of the browser will show .../test_second.xhtml?id=1. On that page, testBean.userId gets printed.

如果单击link2link3,页面将重定向到test_second.xhtml.但是,地址栏仅显示.../test_second.xhtml,没有?id=#!并且testBean.userId不会在该页面上打印.

If I click link2 or link3, the page redirects to test_second.xhtml. However, the address bar only shows .../test_second.xhtml, there is NO ?id=#! And testBean.userId does not get printed on that page.

如何使commandLinkf:param一起使用?有时,我希望链接不重定向到另一个页面,而是根据数据调用bean的某些方法.

How can I make commandLink work with f:param? Sometimes I want the link not to redirect to another page but to call some methods of bean depending on the data.

test_first.xhtml:

test_first.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head/>
<h:body>
<h:form>
    <h:link value="link1" outcome="test_second" >
        <f:param name="id" value="1"/>
    </h:link>
    <br/><br/>
    <h:commandLink value="link2" action="test_second?faces-redirect=true" >
        <f:param name="id" value="2" />
    </h:commandLink>
    <br/><br/>
    <p:commandLink value="link3" action="test_second?faces-redirect=true">
        <f:param name="id" value="3" />
    </p:commandLink>
    <br/><br/>
</h:form>
</h:body>
</html>

test_second.xhtml:

test_second.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<f:metadata>
    <f:viewParam name="id" value="#{testBean.userId}" />
</f:metadata>
<h:head/>
<h:body>
<h:form>
    This is the second page.
    <h:outputText value="Selected id is #{testBean.userId}" />
    <h:commandButton value="Print page id" action="#{testBean.print()}" />
</h:form>
</h:body>
</html>

TestBean.java

TestBean.java

@ManagedBean
@SessionScoped
public class TestBean implements Serializable{
    private Integer userId;

    public void print() {
        System.out.println(userId);
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }
}

推荐答案

您误解了这两个标签的含义,即

You misinterpreted the meaning of those two tags, namely <h:link> and <h:commandLink>, therefore, you also misinterpreted the meaning of <f:param> attached to either of the two. In anycase it is worthwhile to always read the documentation before asking the questions to get more insight.

<h:link>呈现HTML"a"锚元素.组件的值呈现为锚文本,并且组件的结果用于确定在"href"属性中呈现的目标URL. 任何子级UIParameter组件都将附加到String上,以在呈现之前作为查询参数输出为"href"属性的值 .

<h:link> renders an HTML "a" anchor element. The value of the component is rendered as the anchor text and the outcome of the component is used to determine the target URL rendered in the "href" attribute. Any child UIParameter components are appended to the String to be output as the value of the "href" attribute as query parameters before rendering...

<h:commandLink>呈现一个HTML"a"锚元素,如果单击该HTML锚元素,则其作用类似于表单提交按钮* ...如果不存在disable属性,或者其值为false.它将#"呈现为"href"属性的值,将组件的当前值呈现为链接文本(如果已指定),并且*将功能上等效于以下内容的JavaScript呈现为"onclick"属性的值:

<h:commandLink> render an HTML "a" anchor element that acts like a form submit button* when clicked ... if the disabled attribute is not present, or its value is false. It renders "#" as the value of the "href" attribute, renders the current value of the component as the link text if it is specified and *renders JavaScript that is functionally equivalent to the following as the value of the "onclick" attribute:

document.forms['CLIENT_ID']['hiddenFieldName'].value='CLIENT_ID';    
document.forms['CLIENT_ID']['PARAM1_NAME'].value='PARAM1_VALUE'; 
document.forms['CLIENT_ID']['PARAM2_NAME'].value='PARAM2_VALUE'; return false;
document.forms['CLIENT_ID'].submit()" 

其中hiddenFieldName如上所述,CLIENT_ID是UICommand组件的clientId, PARAM _NAME和PARAM _VALUE分别是任何嵌套的UIParameter子项的名称和值. /em>.

where hiddenFieldName is as described above, CLIENT_ID is the clientId of the UICommand component, PARAM_NAME and PARAM_VALUE are the names and values, respectively, of any nested UIParameter children.

换句话说,嵌套在<h:link>标记内的<f:param>将最终作为所生成URL的查询参数,而嵌套在<h:commandLink>标记内的<f:param>将最终成为具有给定值的请求参数.

In other words, within <h:link> tag nested <f:param> will end up as a query parameter of the generated URL, while within <h:commandLink> tag nested <f:param> will end up as a request parameter with a given value.

虽然第一个清晰,但第二个值得更好地阐述.要了解它的作用,请考虑一下,如果我们从细节中摘录,则<h:commandLink>发送POST请求,并将所有嵌套的<f:param>标记附加为请求参数.但这完全取决于您如何处理它们,因为导航完全掌握在您的手中.

While the first one is clear, the second one deserves a better elaboration. To understand what it does, consider that if we abstract away from the details <h:commandLink> sends a POST request and attaches all nested <f:param> tags as request parameters. But it is up to you how you will handle them, as navigation is entirely in your hands.

因此,第一种选择是设置一个硬编码的action属性,用例是可疑的,就像在action="second-page"中那样,通过这种方式,您没有传递任何查询参数.将要做的是在不执行任何操作的情况下POST到同一视图并转发到第二个视图.相当愚蠢的动作.

So, the first option is to set a hardcoded action attribute, which use case is dubious, like in action="second-page", in which way you didn't pass any query parameter at all. What will be done is POSTing to the same view and forwarding to the second without undertaking any action. Quite a dumb action.

第二个选项是指定操作方法,如action="#{bean.action}"中所示.在这种情况下,您必须使用提供的操作方法处理导航,即从该方法返回null/void进行回发,或者将导航案例结果作为字符串返回以进行转发指定的视图.至于您通过<f:param>传递的请求参数,它们将以标准JSF方式提供,例如在请求范围内的bean上通过@ManagedProperty("#{param.name}")或通过调用中一样.因此,现在您有了可以在操作方法中使用的参数,可以随意使用自己喜欢的参数,只需遵守URL中存在的一组规则即可.

The second option is to specify an action method, like in action="#{bean.action}". In this case you must handle navigation in the provided action method, i.e. return null/void from the method for a postback, or return a navigation case outcome as a string to make a forward to the specified view. As for the request parameters that you passed with <f:param> they will be available with standard JSF means like @ManagedProperty("#{param.name}") on a request-scoped bean, or by calling ExternalContext#getRequestParameterMap() in any-scoped bean, for example, in action method, like in String param = externalContext.getRequestParameterMap().get("name"). So now you have your parameter in action method that you're free to use how you like, just adhere to a set of rules that exist for URLs.

还有两件事值得一提.请记住,通过调用命令链接传递的请求参数将仅在同一请求中可用,因为您可能希望它在faces-redirect=true中仍然有效,而该faces-redirect=true本质上会引发另一个请求.另一个选择是指定includeviewparams=true以通过当前视图的参数,如果需要的话,如另一个答案中所述.

Two things left worth mentioning. Remember that request parameters passed with calling the command link will be available only within that same request, as you might expect it to survive a faces-redirect=true that basically fires another request. The other option is to specify includeviewparams=true to pass through the paramaters of the current view, if that's desired, as mentioned in the other answer.

这篇关于f:param不适用于查询字符串上的p:commandLink或h:commandLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆