0

I've a @ViewScoped bean

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class MyBean implements Serializable

It is reconstructed on every postback (with all obvious consequences such as losing initial properties). How is this caused and how can I solve it? If it is not solveable are there any workarounds?

I cannot change the scope of the bean to for example @SessionScoped due to non technical reasons.

Following State Saving Method is configured:

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
</context-param>

All JSF Related files out of my pom.xml

        <!-- JSF Files -->
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>mojarra-jsf-impl</artifactId>
        <version>2.0.0-b04</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.12</version>
    </dependency>

    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.0.2-FCS</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>javax.faces-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Danny.
  • 363
  • 1
  • 4
  • 23
  • What does the postback-method you're calling look like? – Dawn Sep 15 '15 at 08:58
  • 1
    Danny, nope, you have never shown it. @Dawn: OP's problem already occurs before the method is hit, otherwise the problem would only occur "every second click" only. So that part is already irrelevant and would only add distracting noise to the question. – BalusC Sep 15 '15 at 09:09
  • 1
    have you tried posting the title of this question in Google and tried finding a cause of your problem by reading the results? Or even reading the 'related' questions on the right in Stackoverflow – Kukeltje Sep 15 '15 at 09:18
  • Well, the [ask] page (and also any [how-to-ask-smart-questions](http://www.catb.org/esr/faqs/smart-questions.html#before)) state to describe what you did/tried and why it was no solution. So instead of asking me what I found (several possible causes), please improve your question from your side. – Kukeltje Sep 15 '15 at 09:52
  • There just too many possible causes and you didn't give any little clue to exclude one and other (which is a signal that you know little about HTTP and HTML, which is worrying given that JSF would then basically be a deep hole for you). I was yesterday not in a mood to take you with your hand through all possible causes. As first step, to exclude one and other, does `@SessionScoped` work or not? – BalusC Sep 16 '15 at 07:23
  • `@SessionScoped` is doing the job with all consequences – Danny. Sep 16 '15 at 07:25
  • As second step, press F12 in browser to open the webdeveloper's toolset and open the *Network* tab and perform some postbacks on the same form (with log preserved). Pay attention to the `javax.faces.ViewState` request parameter in the form data payload. Is its value exactly the same across subsequent postbacks? – BalusC Sep 16 '15 at 07:28
  • The value is changing `javax.faces.ViewState:5009729732090143156:-8654254684322543083 javax.faces.ViewState:5009729732090143156:-1247411320451084082 javax.faces.ViewState:5009729732090143156:-6939382571533866801` – Danny. Sep 16 '15 at 07:39
  • Does this happen with this [Hello World Example](http://stackoverflow.com/tags/jsf/info) too? (by the way, if more than one other users have commented on a post, no one would get notifications anymore as long as you don't target one with `@nickname`; you're otherwise dependent on whether one would look back later if there are any new comments on the post) – BalusC Sep 16 '15 at 08:01
  • @BalusC yes also on this page the viewstate parameter changes – Danny. Sep 16 '15 at 08:12
  • Which server make/version are you using and which JARs do you have all in `/WEB-INF/lib` folder of the deployed WAR file? – BalusC Sep 16 '15 at 08:12
  • @BalusC Tomcat 7.0.47, I pasted my pom.xml into the question. – Danny. Sep 16 '15 at 08:16
  • The JSF dependencies are a mess. They don't match all four. Get rid of all those four JSF dependencies and use the single [`org.glassfish:javax.faces`](https://maven.java.net/content/repositories/releases/org/glassfish/javax.faces/) dependency of [latest version](http://javaserverfaces.java.net/download.html) (2.2.12 currently). – BalusC Sep 16 '15 at 08:30
  • @BalusC I just updated to 2.2.12. Unfortunately I now facing following error message `SEVERE: JSF1094: Could not decode flash data from incoming cookie value Invalid characters in decrypted value. Processing will continue, but the flash is unavailable for this request.` first I've to solve it. I'll come back soon to you, as soon this is solved. thanks a lot so far – Danny. Sep 16 '15 at 08:42
  • 1
    Your browser cookie store is messed up too due all those old and mixed dependencies which had previously [flash scope bugs](http://stackoverflow.com/questions/13685633/how-to-show-faces-message-in-the-redirected-page/13685722#13685722). Just clear all cookies or spawn an incognito window. – BalusC Sep 16 '15 at 08:43

1 Answers1

2

This doesn't make sense.

<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>mojarra-jsf-impl</artifactId>
    <version>2.0.0-b04</version>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-api</artifactId>
    <version>2.2.12</version>
</dependency>
<dependency>
    <groupId>javax.faces</groupId>
    <artifactId>jsf-impl</artifactId>
    <version>2.0.2-FCS</version>
</dependency>
<dependency>
    <groupId>javax.faces</groupId>
    <artifactId>javax.faces-api</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
</dependency>

Get rid of them all. They're incompatible with each other. This can have many consequences, of which a broken view scope is indeed one.

To use Mojarra on Tomcat, just grab the single org.glassfish:javax.faces dependency.

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.faces</artifactId>
    <version><!-- Check javaserverfaces.java.net for latest version --></version>
</dependency>

Latest stable 2.2.x version is currently 2.2.12.

Related potential causes:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555