@ViewScoped doesn't work, bean is created for every request

自闭症网瘾萝莉.ら 提交于 2019-12-10 12:07:45

问题


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?


回答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



来源:https://stackoverflow.com/questions/13085749/viewscoped-doesnt-work-bean-is-created-for-every-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!