JSF - 在ajax调用上传递参数 - 这段代码有什么问题?

时间:2020-12-01 20:08:04

I need to pass a parameter to a bean when i do an ajax call.

当我进行ajax调用时,我需要将参数传递给bean。

My bean is this :

我的豆是这样的:

@ManagedBean
@RequestScoped
public class Selector {
    @ManagedProperty(value="#{param.page}")
    private String page;

    @PostConstruct
    public void init() {
        if(page==null || page.trim().isEmpty()) {
            this.page="homepage";
        }

        System.out.println(this.page);
    }

    public String getPage() { return page; }
    public void setPage(String page) { this.page=page; }
}

And, when i do the ajax call, i need (due to the fact that i want to render a different context) the page parameter. So i've done this :

并且,当我执行ajax调用时,我需要(由于我想渲染不同的上下文的事实)页面参数。所以我做到了这一点:

// in this moment selector.page = articles
<h:inputHidden value="#{selector.page}" id="page" />

<h:commandLink>
    <f:setPropertyActionListener target="#{articlesSelector.order}" value="1" />
    <f:ajax event="click" render=":articlesContent"/>
    <h:graphicImage value="img/arrow_up.png" alt="Arrow Up"/>
</h:commandLink>

But, on the Apply request phase, the page still "homepage". It should get the page-parameter from the request, apply it to the Component tree and render the "articles" context. Why doesnt happens?

但是,在应用请求阶段,页面仍然是“主页”。它应该从请求中获取page-parameter,将其应用于Component树并呈现“articles”上下文。为什么不会发生?

Cheers

干杯

1 个解决方案

#1


5  

Because the value of <h:inputHidden> is only set during update model values phase. This is indeed an unintuitive behaviour which existed for long in JSF. I've ever reported an issue about this, but this was closed as "by design".

因为 的值仅在更新模型值阶段期间设置。这确实是一种在JSF中存在很长时间的不直观的行为。我曾经报道过一个关于这个的问题,但这个问题因“按设计”而被关闭。 :inputhidden>

There are several ways to fix this, among others the view scope. In your particular case, you can use <f:param> instead of <h:inputHidden>:

有几种方法可以解决这个问题,其中包括视图范围。在您的特定情况下,您可以使用 而不是 :inputhidden> :param>

<h:commandLink>
    <f:param name="page" value="#{selector.page}" />
    <f:setPropertyActionListener target="#{articlesSelector.order}" value="1" />
    <f:ajax event="click" render=":articlesContent"/>
    <h:graphicImage value="img/arrow_up.png" alt="Arrow Up"/>
</h:commandLink>

It will then be available as request parameter #{param.page} and in your request scoped bean thus be set as @ManagedProperty.

然后它将作为请求参数#{param.page}提供,并在您的请求范围内的bean中设置为@ManagedProperty。

#1


5  

Because the value of <h:inputHidden> is only set during update model values phase. This is indeed an unintuitive behaviour which existed for long in JSF. I've ever reported an issue about this, but this was closed as "by design".

因为 的值仅在更新模型值阶段期间设置。这确实是一种在JSF中存在很长时间的不直观的行为。我曾经报道过一个关于这个的问题,但这个问题因“按设计”而被关闭。 :inputhidden>

There are several ways to fix this, among others the view scope. In your particular case, you can use <f:param> instead of <h:inputHidden>:

有几种方法可以解决这个问题,其中包括视图范围。在您的特定情况下,您可以使用 而不是 :inputhidden> :param>

<h:commandLink>
    <f:param name="page" value="#{selector.page}" />
    <f:setPropertyActionListener target="#{articlesSelector.order}" value="1" />
    <f:ajax event="click" render=":articlesContent"/>
    <h:graphicImage value="img/arrow_up.png" alt="Arrow Up"/>
</h:commandLink>

It will then be available as request parameter #{param.page} and in your request scoped bean thus be set as @ManagedProperty.

然后它将作为请求参数#{param.page}提供,并在您的请求范围内的bean中设置为@ManagedProperty。