includeViewParams = true将null模型值转换为查询字符串中的空字符串

时间:2014-05-18 18:50:10

标签: jsf jsf-2.2 viewparams

给出<p:selectOneMenu>如下。

<f:metadata>
    <f:viewParam name="id" value="#{testManagedBean.id}" converter="javax.faces.Long"/>
</f:metadata>

<p:selectOneMenu value="#{localeBean.language}" onchange="changeLanguage();">
    <f:selectItem itemValue="en" itemLabel="English" />
    <f:selectItem itemValue="hi" itemLabel="Hindi" />
</p:selectOneMenu>

<p:remoteCommand action="#{testManagedBean.submitAction}"
                 name="changeLanguage"
                 process="@this"
                 update="@none"/>

相应的托管bean:

@ManagedBean
@RequestScoped
public final class TestManagedBean {

    private Long id; //Getter and setter.

    public TestManagedBean() {}

    public String submitAction() {
        return FacesContext.getCurrentInstance().getViewRoot().getViewId() + "?faces-redirect=true&includeViewParams=true";
    }
}

<f:viewParam>指示的参数是可选的。例如,使用URL访问页面,如下所示。

https://localhost:8181/Project-war/private_resources/Test.jsf

由于id是一个可选参数,因此如果未按以下方式提供语言,则会在URL中附加一个空参数(当语言从<p:selectOneMenu>更改时)。

https://localhost:8181/Project-war/private_resources/Test.jsf?id=

这不应该发生。如果未提供空参数,则不应附加空参数,并且URL应与第一个相同。

有没有办法阻止空参数在未传递时附加到URL?


这仅与<f:viewParam> - javax.faces.Long指定的转换器相关联。

如果删除此转换器,则参数不会附加到URL,以防没有提供参数。

虽然完全没有必要指定转换器,但我有如下所示的转换器将通过URL传递的id转换为查询字符串参数到JPA实体。

@ManagedBean
@RequestScoped
public final class ZoneConverter implements Converter {

    @EJB
    private final SharableBeanLocal sharableService = null;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        try {
            long parsedValue = Long.parseLong(value);

            if (parsedValue <= 0) {
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Summary", "Message"));
            }

            ZoneTable entity = sharableService.findZoneById(parsedValue);
            if (entity == null) {
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_WARN, "Message Summary", "Message"));
            }

            return entity;
        } catch (NumberFormatException e) {
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Summary", "Message"), e);
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return value instanceof ZoneTable ? ((ZoneTable) value).getZoneId().toString() : "";
    }
}

现在需要使用<f:viewParam>明确指定此转换器,如下所示。

<f:viewParam name="id" 
             value="#{testManagedBean.id}"
             converter="#{zoneConverter}"
             rendered="#{not empty param.id}"/>

关联的托管bean需要按如下方式进行更改。

@ManagedBean
@RequestScoped
public final class TestManagedBean {

    private ZoneTable id;  //Getter and setter.

    public TestManagedBean() {}

    public String submitAction() {
        return FacesContext.getCurrentInstance().getViewRoot().getViewId() + "?faces-redirect=true&includeViewParams=true";
    }
}

1 个答案:

答案 0 :(得分:7)

这可能是Mojarra's default implementation UIViewParameter#getStringValueFromModel()的疏忽,其来源可供参考,如下所示:

384    public String getStringValueFromModel(FacesContext context)
385        throws ConverterException {
386        ValueExpression ve = getValueExpression("value");
387        if (ve == null) {
388            return null;
389        }
390
391        Object currentValue = ve.getValue(context.getELContext());
392
393        // If there is a converter attribute, use it to to ask application
394        // instance for a converter with this identifer.
395        Converter c = getConverter();
396
397        if (c == null) {
398            // if value is null and no converter attribute is specified, then
399            // return null (null has meaning for a view parameters; it means remove it).
400            if (currentValue == null) {
401                return null;
402            }
403            // Do not look for "by-type" converters for Strings
404            if (currentValue instanceof String) {
405                return (String) currentValue;
406            }
407
408            // if converter attribute set, try to acquire a converter
409            // using its class type.
410
411            Class converterType = currentValue.getClass();
412            c = context.getApplication().createConverter(converterType);
413
414            // if there is no default converter available for this identifier,
415            // assume the model type to be String.
416            if (c == null) {
417                return currentValue.toString();
418            }
419        }
420
421        return c.getAsString(context, this, currentValue);
422    }

在为UIViewParameter构建查询字符串期间,会为每个<f:viewParam>includeViewParams=true后面的UI组件)调用此方法。我们在源代码中看到,无论currentValue是否为null,它都会调用转换器。换句话说,即使模型值为null,它仍会使用它调用转换器。

根据javadoc of Converter#getAsString()转换器,如果值为null,则返回零长度字符串所需的规范:

  

符getAsString

     

...

     

返回:如果值为null,则为零长度字符串,否则为转换结果

因此,转换器实际上应该永远不会在null上返回getAsString()。然后他们返回一个空字符串。在查询字符串中的视图参数的情况下,这是非常不期望的。空字符串值与查询字符串中完全缺失之间的差异非常重要。

我已经向Mojarra家伙报告了issue 3288。然后他们应该解决这个问题如下:

391        Object currentValue = ve.getValue(context.getELContext());
392
393        if (currentValue == null) {
394            return null;
395        }

与此同时,我[{3}}为OmniFaces提供了解决方案。此修复已扩展committed。它按照今天的<o:viewParam>提供。

<f:metadata>
    <o:viewParam name="id" value="#{testManagedBean.id}" converter="javax.faces.Long"/>
</f:metadata>

更新:他们决定不修复它。无论如何,有OmniFaces。