简体   繁体   English

Jsf / Spring 请求作用域bean

[英]Jsf / Spring request scoped bean

As I don't have much experience in javaee programming and hasve noone to ask, i would like ask you.由于我在javaee编程方面没有太多经验并且没有人问,所以我想问你。 I have one thing in my code that i don't like and think it's implemented in wrong way.我的代码中有一件事我不喜欢并且认为它以错误的方式实现。 My managed bean are request scoped.我的托管 bean 是请求范围的。 in jsf i use rich:pickList which takes data from list.在 jsf 我使用 rich:pickList 从列表中获取数据。

@Scope("request")
public class MyBean{
     private List<String> sourceList;
     ....     

     public List<String> getsourceList() {
            //if (sourceList == null)    <--- Has no sence in request scoped bean
             { sourceList = service.loadList();
             }
             return sourceList;
     }

....

}

I have also submit button which store some data.我也有提交存储一些数据的按钮。

The problem is, that each time when the page do some actions (eg clicking submit button) this get method is invoked every time and goes to service layer, then to dao and to database.问题是,每次页面执行某些操作(例如单击提交按钮)时,每次都会调用此 get 方法并进入服务层,然后进入 dao 和数据库。 It obviously seems to be not correct solution.这显然似乎不是正确的解决方案。 How to avoid it?如何避免? Thanks' for you answers.谢谢你的回答。

Everyone has this problem because there is no "conversation" scope.每个人都有这个问题,因为没有“对话”scope。 You have "session" (as long as the user is logged in) and "request" (one request/response cycle).您有“会话”(只要用户登录)和“请求”(一个请求/响应周期)。

What you'd need is a way to say "user has started a conversation", then do a couple of requests that belong to the conversation and finally wrap it up.您需要的是一种表达“用户已开始对话”的方式,然后执行几个属于该对话的请求,最后将其结束。

Since this isn't supported by JavaEE, you have to emulate it.由于 JavaEE 不支持此功能,因此您必须模拟它。 When the user starts the conversation, put the bean in the session scope and keep it there.当用户开始对话时,将 bean 放入 session scope 并保留在那里。 When the user finishes the conversation, delete the bean manually or tell it to clean it's caches.当用户完成对话时,手动删除 bean 或告诉它清理缓存。

There is actually a "conversation scope" in Spring WebFlow: Spring WebFlow中其实有一个“对话范围”:

http://static.springsource.org/spring-webflow/docs/2.0.x/reference/html/ch12s06.html http://static.springsource.org/spring-webflow/docs/2.0.x/reference/html/ch12s06.html

If you're on JSF 2.0, you can use the new view scope by @ViewScoped .如果您使用的是 JSF 2.0,则可以使用 @ViewScoped 的新视图@ViewScoped

@ManagedBean
@ViewScoped
public class Bean {

    private List<Foo> foos;

    @EJB
    private FooService fooService;

    @PostConstruct
    public void init() {
        foos = fooService.list();
    }

    public List<Foo> getFoos() {
        return foos;
    }

}

When you're still on JSF 1.x, it's good to know that the RichFaces' <a4j:keepAlive> and Tomahawk's <t:saveState> have exactly the same effect on a request scoped bean with the above code design (ie do NOT load data in the getter):当您仍在使用 JSF 1.x 时,很高兴知道RichFaces 的<a4j:keepAlive>Tomahawk 的<t:saveState>对具有上述代码设计的请求范围 bean 具有完全相同的效果(即不要在 getter 中加载数据):

<a4j:keepAlive beanName="#{bean}" />

and

<t:saveState beanName="#{bean}" />

I haven't used the new RichFaces' @KeepAlive annotation, but concerning the docs, it should behave the same as well.我没有使用新的 RichFaces 的@KeepAlive注释,但关于文档,它的行为也应该相同。

Add the Spring RequestContextListener in web.xml that Spring can add the request scope and session scope. Add the Spring RequestContextListener in web.xml that Spring can add the request scope and session scope.

<listener>
     <listener-class>
          org.springframework.web.context.request.RequestContextListener
     </listener-class>  
</listener>

Cf: 3.4.4.参照: 3.4.4。 The other scopes 其他范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM