1

So I have the following jsf page (working):

<ui:define name="content">
  <h:form>
    <p:dataTable id="projectList" value="#{studyController.studyList}" var="item">
      <p:column headerText="ID" >
        <h:outputText value="#{item.id}"/>
      </p:column>
      <p:column headerText="Name">
        <h:outputText value="#{item.name}"/>
      </p:column>
      <p:column headerText="Action">
        <h:commandLink action="#{projectController.openProject(item.id)}" value="Project">
        </h:commandLink>
      </p:column>
    </p:dataTable>
  </h:form>
</ui:define>

And this is the bean, that is called on link click:

@Named
public class ProjectController{

  private Integer projectId;

  public String openProject(Integer projectId){
    // this works, System.out.println shows correct setting
    this.projectId = projectId;

    // now redirect to correct jsf page via faces-config.xml
    return "show_project";
  }

  // this is called by the jsf page to be shown
  public Integer getProjectId(){
    // this is null :(
    return this.projectId;
  }  

}

My faces-config.xml is something like:

<navigation-rule>
  <from-view-id>*</from-view-id>
  <navigation-case>
    <from-outcome>show_project</from-outcome>
    <to-view-id>/project.xhtml</to-view-id>
    <redirect/>
  </navigation-case>
</navigation-rule>

It all works, except the projectId of the ProjectController is lost between the call of openProject - which is called and debug shows correct id in the console - and the rendering of the page, where the page does someting linke #{projectController.getProjectId} - because this returns null.

How can i retain the value in the bean until the page is rendered? I tried with some @SessionScope, @RequestScope and stuff, but frankly I don't understand the full Extend of the JFS lifecycle.

Just to provide some more info, I'm using JBoss AS 7.2 with PrimeFaces 3.4.2

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Kaffee
  • 1,503
  • 11
  • 21

1 Answers1

1

As to your concrete problem, that (old fashioned) navigation case is specifying a redirect. A redirect basically instructs the client to send a brand new HTTP request on the given resource. The initial request on which you've set the attribtue is been trashed for that.

I'm not sure what your concrete functional requirement is, so it's hard to propose the right solution to achieve that. One way would be to remove <redirect> entry from your navigation case. Another way would be to use a plain link with a <f:param> and the <f:viewParam> in the target page in order to set/convert it (this instantly also improves bookmarkability and thus SEO and UX). Again another way would be to use the flash scope.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Thank you for your anwer. Removing `` did not work. The projectId is still null (after is has been set). The purpose of the `` was to force the URL change in the browser window. I will try out the `` solution and comment later. – Kaffee Jun 17 '13 at 11:58
  • Then there's another cause of the problem not visible in the code/information provided so far. Perhaps you're managing the beans in a bad way? Just open up the 1st chapter of a sane JSF tutorial to create a "Hello World". Exactly this case is usually demonstrated in the average JSF Hello World. – BalusC Jun 17 '13 at 11:59
  • As you already said "old fashioned" - I tried several tutorials, but none seemed to be up2date with modern approaches :( – Kaffee Jun 17 '13 at 12:02
  • So I kind of found the solution. Additional to remove the ``, I also added `@RequestScoped`. Thanks. I think I will search again for better tutorials. – Kaffee Jun 17 '13 at 12:15
  • Just stop reading JSF 1.x resources and head to JSF 2.x ones. Pay attention to the version mentioned. JSF 1.x and 2.x are like day and night. – BalusC Jun 17 '13 at 15:10