1

I am learning JSF and when I send an object to a bean parameter the instance of the object is not correct.

I have a page that list a lot of movies, if I want to see the complete information about a movie, I click on a link that send me to a page with this code:

<h:form>
<h:commandLink action="#{sharedMovie.share(movie)}" value="Share">
    <div class="movieDetail">
    <div class="movieDetailTitle">"#{movie.title}"</div>
    <div class="movieDetailYear">"#{movie.year}"</div>
    <div class="movieDetailSynopsis">"#{movie.synopsis}"</div>
    </div>
</h:form>

The movie bean have a request scope.

The problem is that when I click on "Share" my class sharedMovie receives a new instance of Movie, instead of the displayed movie in the page. All the displayed data is correct, only when I call the other page that this problem happens.

I already tried sending just the movie ID (but it is always 0), using f:setPropertyActionListener, f:param, but nothing works. If I debug my project, when I click on share link it always hit the breakpoint inside the constructor of Movie class.

How I can pass this instance correctly?

Rafael
  • 1,655
  • 3
  • 17
  • 25
  • 2
    If you're using JSF 2, try to change your managed bean to `@ViewScoped` instead of `@RequestScoped` – Luiggi Mendoza Dec 29 '12 at 16:29
  • Please elaborate in detail why exactly is it a problem that a new instance is created and received. Please note that the action and all of its variables are not prepared during the HTTP request of displaying the form, but during the HTTP request of submitting the form. – BalusC Dec 29 '12 at 18:04
  • Thanks Luiggi, changing to @ViewScoped solved my problem. – Rafael Dec 29 '12 at 19:37

0 Answers0