1

由于我在javaee编程方面没有太多经验并且没有人问,所以我想问你。我的代码中有一件事我不喜欢并且认为它以错误的方式实现。我的托管 bean 是请求范围的。在 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;
     }

....

}

我也有提交存储一些数据的按钮。

问题是,每次页面执行某些操作(例如单击提交按钮)时,每次都会调用此 get 方法并进入服务层,然后进入 dao 和数据库。这显然似乎不是正确的解决方案。如何避免?谢谢你的回答。

4

4 回答 4

0

If you're on JSF 2.0, you can use the new view scope by @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 (i.e. do NOT load data in the 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.

于 2011-04-29T12:23:05.850 回答
0

在web.xml中添加Spring RequestContextListener,Spring可以添加请求范围和会话范围。

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

参照:3.4.4。其他范围

于 2011-05-18T14:19:18.827 回答
0

每个人都有这个问题,因为没有“对话”范围。您有“会话”(只要用户登录)和“请求”(一个请求/响应周期)。

您需要的是一种表达“用户已开始对话”的方式,然后执行几个属于该对话的请求,最后将其结束。

由于 JavaEE 不支持此功能,因此您必须模拟它。当用户开始对话时,将 bean 放入会话范围并保持在那里。当用户完成对话时,手动删除 bean 或告诉它清理缓存。

于 2011-04-29T07:53:11.480 回答
0

Spring WebFlow 中实际上有一个“对话范围”:

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

于 2011-04-29T11:40:59.467 回答