f:viewParam 具有多个值

发布于 2024-12-10 08:36:50 字数 857 浏览 0 评论 0原文

我有一个 驱动的搜索屏幕。我正在尝试实现它以获取单个 的多个值。

我相信正确的 URL 看起来像

<url>?state=COMPLETE&state=PENDING

XHTML 部分如下所示:

<f:metadata>
   <f:viewParam name="state"
      value="#{backingBean.state}"
      converter="#{stateNameConverter}" />
</f:metadata>

我在 backingBean 上尝试了以下 2 个方法签名:

public void setState(State... state)

希望 JSF 实现能够为值构建一个数组并在支持 bean 上设置。 JSF 实现失败,错误指出无法将枚举转换为枚举数组。

public void setState(State state)

我认为 JSF 实现可能会设置在 URL 中找到的转换后的值。仅设置了第一个值。

stateNameConverterStringenum 值之间进行转换。

JSF 2 中的 是否可以有多个值?

I have a <f:viewParam> driven search screen. I am trying to implement it to take multiple values for a single <f:viewParam>.

I believe the correct URL would look something like

<url>?state=COMPLETE&state=PENDING

The XHTML section is as follows:

<f:metadata>
   <f:viewParam name="state"
      value="#{backingBean.state}"
      converter="#{stateNameConverter}" />
</f:metadata>

I have tried the following 2 method signatures on backingBean:

public void setState(State... state)

Was hopeful that JSF implementation would build an array for the values and set on the backing bean. JSF implementation failed with error stating it cannot convert enum to array of enums.

public void setState(State state)

Thought that maybe the JSF implementation would set the converted values as it found them in the URL. Only the first value was set.

The stateNameConverter converts between String and enum value.

Is it possible to have multiple values for <f:viewParam> in JSF 2?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水般的淡然安静女子 2024-12-17 08:36:50

不,不幸的是,没有 这样的标签。 Mojarra 的 UIViewParameter#decode()实现(这是后面的代码 标记),这证实了:

@Override
public void decode(FacesContext context) {
    if (context == null) {
        throw new NullPointerException();
    }

    // QUESTION can we move forward and support an array? no different than UISelectMany; perhaps need to know
    // if the value expression is single or multi-valued
    // ANSWER: I'd rather not right now.
    String paramValue = context.getExternalContext().getRequestParameterMap().get(getName());

    // submitted value will stay as previous value (null on initial request) if a parameter is absent
    if (paramValue != null) {
        setSubmittedValue(paramValue);
    }

    rawValue = (String) getSubmittedValue();
    setValid(true);

}

现在解决此问题的最佳方法是自己从 ExternalContext#getRequestParameterValuesMap() 中的 中收集值>@PostConstruct 或者可能是 @ManagedProperty("#{paramValues.state}") String[] 属性。

您还可以创建一个自定义 标记来实现这一点,但这并不完全是微不足道的。

No, unfortunately, there's no such tag as <f:viewParams>. There's also a comment in Mojarra's UIViewParameter#decode() implementation (which is the code behind <f:viewParam> tag) which confirms that:

@Override
public void decode(FacesContext context) {
    if (context == null) {
        throw new NullPointerException();
    }

    // QUESTION can we move forward and support an array? no different than UISelectMany; perhaps need to know
    // if the value expression is single or multi-valued
    // ANSWER: I'd rather not right now.
    String paramValue = context.getExternalContext().getRequestParameterMap().get(getName());

    // submitted value will stay as previous value (null on initial request) if a parameter is absent
    if (paramValue != null) {
        setSubmittedValue(paramValue);
    }

    rawValue = (String) getSubmittedValue();
    setValid(true);

}

Right now your best bet to workaround this is by gathering the values yourself from ExternalContext#getRequestParameterValuesMap() in a @PostConstruct or <f:viewAction> or maybe a @ManagedProperty("#{paramValues.state}") on a String[] property.

You could also create a custom <my:viewParams> tag which achieves that, but this isn't exactly trivial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文