开发者

JSF - variable set of params based on postback

开发者 https://www.devze.com 2023-04-12 21:27 出处:网络
In this post, the excellent BalusC pointed out the use of <c:forEach> to get the parameters at build time.

In this post, the excellent BalusC pointed out the use of <c:forEach> to get the parameters at build time.

The Code

I have a more complicated version of that with a variable amount of parameters (<f:param> elements) that are based on another part of the form. The top part of the form has a DataTable of all possible "Criteria". The bottom part has a PrimeFaces graphicImage whose content is based on the selections in those criteria, including only criteria that are used.

<p:dataTable var='criterion' value='开发者_开发知识库#{criteria}'
    binding="#{searchBean.criteriaList}" id="#{id}">
  ... each row shows a criteria, which has a parameter name, an operator, and a value ...
  ... each row may be set or not set ...
</p:dataTable>

<p:graphicImage value="#{searchBean.lineChartImage}">
  <c:forEach var="criteriaName" items="#{searchBean.usedCriteriaKeys}">
    <f:param name="#{criteriaName}" value="#{searchBean.criteriaMap[criteriaName]}"/>
  </c:forEach>
</p:graphicImage>

Background

I know the searchBean.lineChartImage method gives me the right chart as long as the parameters are set.

The SearchBean is a RequestScope bean.

I'm using JSF 2.1, Tomcat 7.0.22, and Primefaces 2.2.1.

The Problem

When I select/enable a criteria (e.g. param="eventDate", operator="after", value="2011-10-01"), the parameters of graphicImage are created early in the lifecycle at tree creation time in the RestoreView phase; the set of parameters that I actually want is not updated until the UpdateModelValues phase.

Is there a way to get a set of parameters for the p:graphicImage based on the actual results of the postback itself?


I see your problem. I however don't really see a clear solution for in the view side. Your best bet is to append those parameters yourself to the property behind #{searchBean.lineChartImage} in the bean's action method.

this.lineChartImage = imageURL + "?" + toQueryString(criteriaMap);

with this utility method

public static String toQueryString(Map<String, String> params) {
    StringBuilder queryString = new StringBuilder();

    for (Entry<String, String> param : params.entrySet()) {
        if (queryString.length() > 0) {
            queryString.append("&amp;");
        }

        queryString
            .append(URLEncoder.encode(param.getKey(), "UTF-8"))
            .append("=")
            .append(URLEncoder.encode(param.getValue(), "UTF-8"));
    }

    return queryString.toString();
}

It would have been possible if there exist a <f:params> which accepts a Map<String, String> or maybe even Map<String, String[]> as value.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号