0

I am migrating from JSF1.2 to 2.1, I changed entries for beans in faces-config.xml to annotations. I try use @ViewScoped instead @RequestScoped and @ManagedProperties(To many params in few classes), but every time i click submit for my form bean with annotated as @ViewScoped is recreated. For @SessionScoped everything works correctyl.

I read few Q&A here, and This article, but i didn't force it to work.

I change JSTL tags to rendered atribute, or c:if with ui:param rendered.

in my web.xml i set params:

<context-param>
  <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
  <param-value>false</param-value>
</context-param>
<context-param>
  <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
  <param-value>CLIENT</param-value>
</context-param>

I tried javax.faces.PARTIAL_STATE_SAVING = true, but didn't work too. With javax.faces.STATE_SAVING_METHOD = SERVER the same problem.

I removed tags handler for test, but it didn't help too.

In project is used: Mojarra 2.1.13, hibernate 3.6, spring 3.1(far as i know updated form 2.x by my predecessor), acegi-security-1.0.5, tomahawk20, urlrewrite-3.2.0.

I use tomcat 6

EDIT:

This is my bean: package my.package;

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

import my.package.MyOtherBean;

@ManagedBean(name="someNameBean")
@ViewScoped
//@SessionScoped
public class MyBean extends MyOtherBean {

public MyBean(){
    super();
    //XXX
    System.out.println("-->> someNameBean is being created");       
}
}



package my.package;

@ManagedBean(name="someNameMyOtherBean")
//@ViewScoped
@SessionScoped
public class MyOtherBean extends BaseBean {

private ClassWithFormFields dataIn; //getter & setter exist
//a lot of code here

}

Example use of bean

<h:selectOneMenu value="#{someNameBean.dataIn.currencyId}" id="currencyId" tabindex="2" >
<f:selectItems value="#{someNameBean.dataIn..availableCurrencies}"/>
</h:selectOneMenu>

Update serviceLocalizator is managed by Spring xml files ans JSF annotation

@ManagedBean
public class BaseBean implements Serializable {

private static final long serialVersionUID = 1L;

protected transient Logger log = Logger.getLogger(this.getClass());

@ManagedProperty(value="#{serviceLocalizator}")
protected transient ServiceLocalizator serviceLocalizator;

    //few more lines

}

Update 2: It's My fault. Thanks for @kolossus that he the indicated direction. I i was looking for answer and I found and read BalusC article And now i now, i shouldn't return string in backing bean action. With null instead string it works. I badly understood concept of a view, I thought that ViewSoped bean id live as long as tab/windows is the same. Now i know that is it JSF View. I'am sory for a trouble.

Maybe is a way to use @ViewSoped with redirect to new page?

k.nieszporek
  • 135
  • 2
  • 12
  • Did you put the @ManagedBean annotation? Do you have correct import for your @ManagedBean? Can you show us slimed down version of the bean and how you are accessing it? – bjedrzejewski Oct 26 '12 at 12:08
  • Did you solve the serialization problem as described in your previous question? http://stackoverflow.com/questions/13073546/java-io-notserializableexception-with-spring-and-viewscoped-bean If the view scoped bean can't be serialized, then it can't be retained for the next request. A new one would then indeed be created. You'd need to fix the serialization problem first. – BalusC Oct 26 '12 at 12:40
  • @BalusC I think is solved, javax.faces.STATE_SAVING_METHOD = SERVER solve serialization problem, i added transient word to one filed too. I don't have exception now, but ViewScoped still doesn't work. – k.nieszporek Oct 26 '12 at 13:00
  • To me it looks like there may be something wrong with the serialization still. This could be because you are duplicating the @ManagedBean a few times in your hierarchy? There are some related questions here you may check: http://stackoverflow.com/questions/6661499/jsf-2-managedbean-not-detected and http://stackoverflow.com/questions/6627384/jsf-2-is-it-possible-to-inherit-managedbean – bjedrzejewski Oct 26 '12 at 13:10
  • Code looks fine so far. But you're also using Spring. Is this bean *also* managed by Spring? (i.e. registered in some Spring XML file or so). Then chance is that you're actually using a Spring-managed bean instead of a JSF-managed one. – BalusC Oct 26 '12 at 13:10
  • @BalusC Yes, I use Spring, but this bean is only managed by JSF annotation, I didn't found any entries for it in spring xml filse. Spring is used for database communication, but i added transient for field related to Spring. – k.nieszporek Oct 26 '12 at 13:42
  • @jedrus07 Annotation are not inherit in Mojarra, I checked and i have Serializable interface or transient word use in place where could be serialization problem. – k.nieszporek Oct 26 '12 at 13:45
  • I added BaseBean code, serviceLocalizator is managed as spring bean and jsf bean – k.nieszporek Oct 26 '12 at 14:31
  • @k.nieszporek, where is the method expression that you're calling in the backing bean? Does it return a String? – kolossus Oct 26 '12 at 15:50
  • @kolossus Which method? if you thinking about currencyId, it is Long field, and anailableCurrencyId return Map – k.nieszporek Oct 26 '12 at 16:41
  • @k.nieszporek what I mean is, is there a command component on the page like a command button/link that calls a method in your backing bean? If there is and you're returning a string in there and for any reason, the view doesn't navigate away, it may cause what you're experiencing – kolossus Oct 27 '12 at 03:59
  • @kolossus Yes, it is, and it returns String. I have navigation case for that, i checked with xhtml file name as string without navigation case, but it didn't work too. With SessionScoped it works in both cases. – k.nieszporek Oct 27 '12 at 19:59
  • I found something [here](http://balusc.blogspot.com/2010/06/benefits-and-pitfalls-of-viewscoped.html), i should return null or void, but how can I replace navigation case and use other page for results? @kolossus Sory, my understand of english is still really bad – k.nieszporek Oct 27 '12 at 20:15
  • @k.nieszporek we might need to move this away from here to chat or something. But it appears the problem is simple. Paste the code of the method where you want to carry out your navigation here – kolossus Oct 27 '12 at 20:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18669/discussion-between-k-nieszporek-and-kolossus) – k.nieszporek Oct 27 '12 at 20:45

1 Answers1

1

NB: If you reference the same (non-session scoped) bean from 2 different views, two instances of that bean will be created

Navigating in JSF is very basic and straightforward from whatever kind of a bean that is backing a JSF view.

  1. return the name (view id) of the page you're trying to navigate to as the return value of a public method

      public String navigateAway(){
       //Do whatever processing you want here
       return "page2"; //where page2 is the name of an actual .xhtml file in your application     
       }
    
  2. Return a JSF navigation case outcome as specified in a faces_config.xml file

      public String navigateAway(){
        //Prior processing
        return "go somewhere else" ;  //where go somewhere else is a navigation outcome you've specified in your faces_config.xml file
       }    
    

    In your faces_config.xml file, you'll have this

      <navigation-rule>
    <from-view-id>/register.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>go somewhere else</from-outcome>
        <to-view-id>/review_registration.xhtml</to-view-id>
        <redirect/>
    </navigation-case>
    

If you want to remain on the same page after an action however, just return null instead of a string in your method and you will not be taken to another view. Also, depending on the scope of your backing bean, you can be sure you will be using the same instance of the bean if you return a null value

For more detail look here

kolossus
  • 20,559
  • 3
  • 52
  • 104