在JSF中处理表单时,所有这些都发生在一个线程中吗? [英] When a form is handled in JSF, does it all happen in one thread?

查看:114
本文介绍了在JSF中处理表单时,所有这些都发生在一个线程中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有这段代码

        <p:dataTable styleClass="scheduleTable" value="#{todaySchedule.hours}" var="hour">
            <p:column headerText="Hour" styleClass="hourColumn" >
                #{hour.time}
            </p:column>
        </p:dataTable>

在名为TodaySchedule的类中,有一个方法

and in a class called todaySchedule, have a method

public List<Hour> getHours() {
        final List<Hour> hours = IntStream.range(0, Hour.TIME.values().length)
                                          .mapToObj($ -> new Hour()).collect(Collectors.toList());
        for (int i = 0; i < 5; i++) {
             hours.get(i).setHour(1);
        }
        return hours;
}

这是小时课

public class Hour {
    private int time;

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }
}

现在,我不确定JSF在幕后做什么,以便在小时列表内使此动态dataTable数据迭代成为可能,但是我假设如果这一切都发生在一个线程中,那么就可以了.但是,如果在幕后将getHours用于实际上正在生成列的另一个线程中,并且看到Hour处于不良状态,该怎么办?如果getHours()方法是

Now, I'm not sure what JSF does behind the scenes to make this dynamic dataTable data iteration through the hours List possible, but I assume that if this is happening all in one thread, then it is okay. However, what if behind the scenes, the getHours is used in another thread that actually does the generating columns and see Hour in a bad state? This could be avoided if the getHours() method was

public List<Hour> getHours() {
        final List<Hour> hours = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            hours.add(new Hour(i + ""));
        }
        return hours;
}

with the corresponding Hour class being

public class Hour {
    private final int time;

    public Hour(int time) {
         this.time = time;
    }

    public int getTime() {
         return time;
    }
}

但是,我的问题是,如果不更改为后者的设计,由于发布此Hour实例时Java中的可见性问题,使用基本的动态JSF dataTable呈现时会出错吗?

However, my question is that, if it wasn't changed to the latter design, can things go wrong when using basic dynamic JSF dataTable rendering due to visibility issues in Java when publishing this Hour instances?

推荐答案

JSF在Servlet API之上运行,该Servlet API在一个线程中处理一个请求,因此,除非您自己引入更多线程,否则您可能希望代码在单个线程中运行线程.

JSF runs on top of Servlet API which serves one request in one thread, so unless you introduce more threads yourself you may expect your code to be run in a single thread.

另一方面,您应该了解JSF生命周期的基础知识以及如何访问Bean属性,因为如果您不了解它,可能会给您带来很多麻烦.

On the other hand, you should get to know the basics of JSF life-cycle and how the bean properties are being accessed as this may cause you a lot of troubles if you don't understand it.

例如,如果您的Java代码保持不变,则在JSF中添加对todaySchedule.hours的另一个引用将导致getter被调用两次,从而生成两次内容.这会很快变得非常混乱,因此进行一些缓存"是一件好事.我个人使用这种方法:

For example, should your Java code stay the same, adding another reference to todaySchedule.hours to your JSF would cause the getter to be called twice, thus generating the content twice. This can get really messy very quickly, so it is a good thing to do some "caching". I personally use this approach:

private List<Hour> hours = null;

private void initHours() {
    this.hours = new LinkedList<>();
    // Fill hours here
}

public List<Hour> getHours() {
    if (this.hours == null) {
        initHours();
    }
    return this.hours;
}

尽管必须在JSF生命周期的哪个阶段执行此操作,但仍要小心.例如,如果您以表单处理方法更改影响列表生成的数据,则列表可能已经从还原视图"阶段缓存"了,并且在呈现视图"阶段开始时该更改将不会得到反映.在这种情况下,您应该了解缓存并在需要重新加载列表时清除它.

You have to be careful though at what stage of the JSF lifecycle you do this. Should you for example in a form processing method change the data affecting the list generation, the list will probably be already "cached" from the restore-view phase and the change would not be reflected when render-view phase starts. In such case you should be aware of the caching and clear the list when you need to reload it.

public void saveHours() {
    // Save the data, do whatever you need to do

    // This will ensure re-initializing the list on the next call
    this.hours = null;
}

这篇关于在JSF中处理表单时,所有这些都发生在一个线程中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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