[Enhancement] default value for viewParam #118

Closed
djmj opened this Issue Mar 27, 2015 · 4 comments

Projects

None yet

3 participants

@djmj
djmj commented Mar 27, 2015

This is related to the open ticket at JSF spec issue tracker.

https://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1341

What about allowing the definition of a default value for a view-parameter?

Example

Defining number of displayed items (pagination feature) as a view-parameter but still keeping default value within xhtml and not java bean.

Following example is a very clean approach provinding a default value.

<f:viewParam name="pageSize" value="#{bean.pageSize}" default="10"/>
<p:dataTable rows="#{bean.pageSize}"/>

This could also be extended to el expression providing java objects as default values.

Currently there are two workarounds which have no clean separation between markup and java code:

Workaround 1

Passing the default value to bean.

<f:viewParam name="pageSize" value="#{bean.pageSize}"/>
<f:event type="preRenderView" listener="#{bean.initPreRenderView(10)"/>
<p:dataTable rows="#{bean.pageSize}"/>

private int pageSize;

public void initPreRenderView(int pageSize)
{
if (this.pageSize == PageSizeDefault)
    this.pageSize == pageSize;
}

Workaround 2

Hardcoding the default value in bean. But markup should be responsible and not bean.

<f:viewParam name="pageSize" value="#{bean.pageSize}"/>
<p:dataTable rows="#{bean.pageSize}"/>

private int pageSize = 10; 

Workaround 3

If conversion is invoked it may also be possible to use a converter that returns the default value if null or empty string is passed. But this needs new java converter classes for such simple things.

@arjantijms
Member

Sounds like a great idea!

@djmj
djmj commented Jun 19, 2015

This can be more enhanced solving the problem of irrelevant parameters appended to link components, creating more friendly URL's and making it easier to develop.

How often one wrote something like <f:param name="pageSize" value="#{bean.pageSize} disable="#{bean.pageSize eq 10}"/>

When using ìncludeViewParams=true on a link or command or using programmatically getBookmarkableURL the view parameters who's value equal their default value can be ignored.

This behavior maybe is a good idea to be optional or optionally be deactivated using a param like index.xhtml?appendDefaultValues=true

A <f:param name="pageSize" value="10"> in the first post example can be ignored in a <h:link> component the final URL.

@BalusC BalusC added a commit that closed this issue Jul 14, 2015
@BalusC BalusC Fix #118: Set default value for o:viewParam.
Took opportinity to cleanup and rearrange class structure.
dcf5421
@BalusC BalusC closed this in dcf5421 Jul 14, 2015
@BalusC
Member
BalusC commented Jul 14, 2015

It's available as per today's 2.2 snapshot.

Thank you for reporting!

@djmj
djmj commented Aug 29, 2015

Thank you :), often the small features do it!
Already used this in my thesis project :)

@BalusC BalusC added the enhancement label Oct 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment