New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] default value for viewParam #118

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

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

This comment has been minimized.

Show comment
Hide comment
@arjantijms

arjantijms Mar 27, 2015

Member

Sounds like a great idea!

Member

arjantijms commented Mar 27, 2015

Sounds like a great idea!

@djmj

This comment has been minimized.

Show comment
Hide comment
@djmj

djmj 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.

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 closed this in dcf5421 Jul 14, 2015

@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC Jul 14, 2015

Member

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

Thank you for reporting!

Member

BalusC commented Jul 14, 2015

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

Thank you for reporting!

@djmj

This comment has been minimized.

Show comment
Hide comment
@djmj

djmj Aug 29, 2015

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

djmj commented Aug 29, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment