1

I have found a few good replies to similar content so far, but never something that solves my issue. I am trying to accomplish this in the best manner possible.

Within my application (JSF 2.0 running on Glasshfish), I have a list of events (let's call this the EventPage). It is possible to click on each event to then show a page of "results" (ResultPage), showing a list of people who have attended this event.

On the EventPage, the link is made as such :

 <h:link value="#{event.eventName}" outcome="displayResults">
     <f:param name="eventCode" value="#{event.eventCode}"/>
 </h:link>

Then, on the outcome - displayResult, I have code such as this in my backing bean (inspiried by a similar):

@ManagedBean
@RequestScoped
public class DisplayResults {
    @ManagedProperty(value="#{param.eventCode}")
...

This works well. The results are displayed in a Datatable. Now I want the ability to sort them. So I've followed this example : http://www.mkyong.com/jsf2/jsf-2-datatable-sorting-example/.

But, once I change the scope of my backing bean to be something else the "request", I can't use the ManagedProperty anymore. And thus am thinking I have to refer to something less elegant such as :

public String getPassedParameter() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    this.passedParameter = (String) facesContext.getExternalContext().
        getRequestParameterMap().get("id");
    return this.passedParameter;
}

Aslo reading on this forum I share the opinion that if you have to dig down into the FacesContext, you are probably doing it wrong.

SO: 1. Is it possible to sort a Datatable without refreshing the view? Only the datatable in question? 2. Is there another good solution to get the url parameter (or use diffrent means)?

Thanks!

blo0p3r
  • 6,790
  • 8
  • 49
  • 68

1 Answers1

2

Use <f:viewParam> (and <f:event>) in the target view instead of @ManagedProperty (and @PostConstruct).

<f:metadata>
    <f:viewParam name="eventCode" value="#{displayResults.eventCode}" />
    <f:event type="preRenderView" listener="#{displayResults.init}" />
</f:metadata>

As a bonus, this also allows for more declarative conversion and validation without the need to do it in the @PostConstruct.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • This works fine for getting the url parameter. But, I am getting another problem. It is now during my init Function that I am doing my database access to fill values based on the parameter. The issue is, when I come to sort data from DataTable, every request calls the init function, and therefore always getting new data from database, when really, I don't need to and have all the information needed in session scope. Is there a way around this? – blo0p3r Jan 20 '12 at 16:29
  • Add a check on `FacesContext#isPostback()`. See also http://stackoverflow.com/questions/8506033/is-it-possible-to-disable-fevent-type-prerenderview-listener-on-postback By the way, do you really need it to be in the session scope? Wouldn't view scope make more sense? The backing bean name `DisplayResults` doesn't seem to indicate that it represents the logged-in user or any of its session-related settings/preferences. You need to realize that a bean in the session scope is shared between multiple browser windows/tabs throughout the session. – BalusC Jan 20 '12 at 16:36
  • Thanks! This is great help! I have made changes to the scope of my page as well. I had a session scope when things were not working - trying that, forgot to bring it back. Thanks for the heads up! – blo0p3r Jan 20 '12 at 17:07