jsf-2 – 使用AJAX的CDI @ConversationScoped
我有一个非常典型的CRUD情况,我正在努力,所以我认为我必须误解一些东西.我已经整理了一个小的演示应用程序来更好地解释我的问题.感兴趣的两个文件如下所示:
PersonView – 支持JSF页面的CDI Managed Bean package example import java.io.Serializable; import java.util.List; import javax.enterprise.context.Conversation; import javax.enterprise.context.ConversationScoped; import javax.enterprise.inject.Produces; import javax.inject.Inject; import javax.inject.Named; @ConversationScoped @Named public class PersonView implements Serializable { private Person selectedPerson; @Inject private PersonService personService; @Inject private Conversation conversation; public PersonView() {} public List<Person> getPeople() { return personService.findAll(); } public void beginConversation() { if( conversation.isTransient() ) {conversation.begin();} } public void endConversation() { if( !conversation.isTransient() ) { conversation.end();} } public void createPerson() { beginConversation(); setSelectedPerson( new Person() ); } public void addPerson() { personService.addPerson( getSelectedPerson() ); endConversation(); } public void updatePerson() { personService.updatePerson( getSelectedPerson() ); } public Person getSelectedPerson() { return selectedPerson; } public void setSelectedPerson(Person selectedPerson) { this.selectedPerson = selectedPerson; } } index.xhtml – 用于操纵人的JSF页面 <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"> <h:head> <title>CRUD Example</title> </h:head> <h:body> <h:form prependId="false"> <p:dataTable var="p" value="#{personView.people}" id="person_table" rowKey="#{p.id}" selection="#{personView.selectedPerson}"> <p:column selectionMode="single"/> <p:column><f:facet name="header">ID</f:facet>#{p.id}<p:column> <p:column><f:facet name="header">Name</f:facet>#{p.name}</p:column> <f:facet name="footer"> <p:commandButton value="Create Person" onclick="create_dialog.show();" actionListener="#{personView.createPerson}"/> <p:commandButton value="Edit Person" onclick="edit_dialog.show();" update="edit_panel"/> </f:facet> </p:dataTable> </h:form> <p:dialog header="Create Person" id="create_dialog" widgetVar="create_dialog" modal="true" width="750" height="300"> <h:form prependId="false"> <p:panel id="create_panel"> <p>Name: <p:inputText value="#{personView.selectedPerson.name}" required="true"/></p> <p><p:commandButton value="Add" actionListener="#{personView.addPerson}" oncomplete="create_dialog.hide();" update="person_table" /></p> </p:panel> </h:form> </p:dialog> </h:body> 在索引页面上,向用户显示包含系统知道的所有人的数据表.然后,他们按下表格底部的“创建人员”按钮.我已经检查过这是否正确调用了createPerson方法,并且会话显然已经开始了.然后显示create_dialog,用户可以在其中输入名称.当用户单击“添加”按钮时出现问题. JSF尝试存储人员名称,但selectedPerson变量现在为null,因此它失败并出现NullPointerException. 我意识到这不是创建对象的常用方法,但在我当前的应用程序中是有意义的,因为我可以猜测一个新Person的一些值.它也非常适合我想要编辑的方式. 所以我的问题:为什么对话不会传播? PersonView bean似乎始终处于请求范围内.我在JSF2中读过@ViewScoped,但如果可能的话我宁愿坚持使用CDI.根据我的阅读,我认为问题是未能通过请求传递CID名称,但我不知道如何使用AJAX来做到这一点. 我提出的唯一解决方案是将PersonView移动到会话中,但这感觉就像一个巨大的kludge. 解决方法
我使用它的唯一方法是在
MyFaces CODI中使用@ViewAccessScoped.CDI允许扩展,因此您所要做的就是将CODI jar文件包含在您的应用程序中.即使您使用Mojarra而不是MyFaces,这也有效.
所以,如果你想使用CDI注释,这是我的建议.我尝试使用ConversationScoped注释一段时间,但我无法让它方便地工作.一旦我开始使用CODI,我的所有问题就消失了. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 扩展SQLite使其能从apk文件中读取db
- React Native: 把现代web科技带给移动开发者
- swift开发之Sequence
- ruby-on-rails – Rails ActionMailer“Pretty Name”防止发
- 关于跨域策略文件crossdomain.xml文件--配置实例
- cocos2dx基础篇(8)——定时器更新schedule/update
- objective-c – NSInvalidArgumentException reason接收方没
- cocos2dx bindings-generator readme 文档翻译
- objective-c – 我们不能在头文件中声明方法吗?
- SQLite学习1_Windows下安装配置SQLite和使用的教程(1)