Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 同步会话范围的托管bean_Jsf_Jsf 2 - Fatal编程技术网

Jsf 同步会话范围的托管bean

Jsf 同步会话范围的托管bean,jsf,jsf-2,Jsf,Jsf 2,我的JSF2.0应用程序中有两个屏幕。屏幕1搜索学生并在表格中列出学生。结果表中的学生姓名是指向学生详细信息页面(屏幕2)的链接 我的托管bean: @SessionScoped public class TestController { private Student studentOnUI; // Student being viewed on the UI // synchronized public getters and setters public String viewStude

我的JSF2.0应用程序中有两个屏幕。屏幕1搜索学生并在表格中列出学生。结果表中的学生姓名是指向学生详细信息页面(屏幕2)的链接

我的托管bean:

@SessionScoped
public class TestController {
private Student studentOnUI; // Student being viewed on the UI 
// synchronized public getters and setters
public String viewStudentAction(String studentId) {
    this.studentOnUI = getStudentFromDB( studentId );   
    return "studentDetailedPage";
}
public synchronized String clearSearchAction() {
    this.studentOnUI = null;
    return "studentSearchPage";
}
屏幕1 xhtml代码段

<!-- search fields -->
<h:commandButton  
    value="Clear Search"
    action="#{testController.clearSearchAction()}"/>
<!-- Search button -->
<!-- search results table -->
<h:outputText value="#{testController.studentOnUI.name}" />
<h:dataTable 
  value="#{testController.studentOnUI.subjects}"
  var="subject">
  <h:outputText value="#{subject.score}"/>
</h:dataTable>

屏幕2 xhtml代码段

<!-- search fields -->
<h:commandButton  
    value="Clear Search"
    action="#{testController.clearSearchAction()}"/>
<!-- Search button -->
<!-- search results table -->
<h:outputText value="#{testController.studentOnUI.name}" />
<h:dataTable 
  value="#{testController.studentOnUI.subjects}"
  var="subject">
  <h:outputText value="#{subject.score}"/>
</h:dataTable>

我面临以下问题:

  • 运行搜索后,用户单击 学生姓名(移动到屏幕中) (二)
  • 当的渲染响应阶段 屏幕2正在进行中(已完成) EL参考 测试控制员,studentOnUI),他 单击清除按钮(无需等待第一个请求完成)。现在 处理清除请求的线程 将testController.studentOnUI设置为null并 渲染中的第一个线程 响应阶段抛出 当它 评估 {testController.studentOnUI.*}
  • 尽管托管bean处理同步(正确吗?),但它不能解决这里的并发问题,因为可能会发生以下情况

  • 线程1(处理导航到屏幕2的请求)-计算#{testController.studentOnUI.name}并愉快地呈现该值。然后退出testController.getStudentOnUI()同步方法。因此线程1不再拥有控制器实例上的锁(在会话范围内)。发生上下文切换
  • 线程2(清除的处理请求 搜索结果)-有吗 testController.studentOnUI=null clearSearchAction()。上下文切换 发生了
  • 线程1-计算页面中的下一个EL(#{testController.studentOnUI.subjects})并抛出NullPointerException(因为testController.studentOnUI现在为null)

  • 感谢大家指点我使用这种方法的错误之处,或者是否需要在这里采用不同的方法。

    这确实是一种奇怪的行为,但我宁愿使用JavaScript禁用第一页上的链接,使其“不可链接”。 尝试使用jQuery:

    1按钮:$(“#按钮”).attr(“已禁用”,true)

    2链接:$('a.something')。单击(函数(e){ e、 预防默认值(); });


    我不确定支持bean中的同步,因为JSF控制器必须锁定学生对象,而不是clear方法。我错了吗?

    什么是屏幕?您在浏览器中使用了两个选项卡?或者它更像一个向导?我说的屏幕是指一个页面。没有选项卡或向导。用户单击第1页上的链接1(在学生表中导航到学生详细信息页面),在响应完成之前,单击第1页上的链接2。