@ViewScoped不起作用,为每个请求创建bean [英] @ViewScoped doesn't work, bean is created for every request

查看:115
本文介绍了@ViewScoped不起作用,为每个请求创建bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从JSF1.2迁移到2.1,我将faces-config.xml中bean的条目更改为注释。我尝试使用@ViewScoped而不是@RequestScoped和@ManagedProperties(对于很少的类中的许多参数),但每次我点击提交我的表单bean,注释为@ViewScoped被重新创建。对于@SessionScoped,一切都有效。

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.

我在这里读了几个Q& A,这篇文章,但我没有强迫它工作。

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

我将JSTL标记更改为呈现的属性或者c:如果使用ui:param渲染。

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

在我的web.xml中我设置了参数:

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>

我试过javax.faces.PARTIAL_STATE_SAVING = true,但也没有用。用javax.faces.STATE_SAVING_METHOD = SERVER同样的问题。

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.

在项目中使用: Mojarra 2.1.13,hibernate 3.6,spring 3.1(据我所知的更新形式2.x由我的前任),acegi-security-1.0.5,tomahawk20,urlrewrite-3.2.0。

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.

我使用tomcat 6

I use tomcat 6

编辑:

这个是我的bean:
package my.package;

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

}

bean的使用示例

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

更新
serviceLocalizator由Spring xml文件和JSF管理注释

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

}

更新2:
这是我的错。感谢@kolossus,他指出了方向。
我正在寻找答案,我发现并阅读 BalusC文章现在我现在,我不应该在支持bean动作中返回字符串。使用null代替字符串它可以工作。我很难理解一个视图的概念,我认为只要tab / windows是相同的ViewSoped bean id就可以了。现在我知道这就是JSF View。
我是个麻烦的人。

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.

也许是一种使用@ViewSoped重定向到新页面的方式?

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

推荐答案

注意:如果从2个不同的视图引用相同的(非会话作用域)bean,将创建该bean的两个实例

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

在支持JSF视图的任何类型的bean中,在JSF中导航都是非常基本和直接的。

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


  1. 返回您尝试导航到的页面的名称(视图ID)作为公共方法的返回值

  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     
   }


  • 返回 faces_config.xml 文件中指定的JSF导航案例结果

  • 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
       }    
    

    faces_config.xml 文件中,您将拥有此

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

    如果你想在一个动作之后保持在同一页面上,只需返回 null 而不是你方法中的字符串,你就不会被带到另一种观点。此外,根据您的支持bean的范围,如果返回空值,您可以确定将使用相同的bean实例

    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

    这篇关于@ViewScoped不起作用,为每个请求创建bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆