使用多个表单实例 jsf 2.0
我的 jsf 应用程序被设计为通过单击菜单项,相应的页面将显示在新创建的选项卡中。选项卡上显示的页面有自己的表单元素和支持豆。 通常这工作得很好,但无法找到一种方法来解决同时使用同一表单的多个实例时的情况,例如。同时打开更多“添加新员工”选项卡。 我尝试将 @SessionScoped
托管 bean 与包含表单数据实例的 Map
一起使用(为了简单起见,目前只有一个字符串),如下所示:
@ManagedBean(name="employeeBean")
@SessionScoped
public class EmployeeBean extends BaseManagedBean implements Serializable{
private Map<String, String> fields = new HashMap<String, String>();
private String formId;
public void newForm(){
this.formId=RandomStringUtils.randomAlphabetic(10);
logger.debug("newForm: formId: " + formId);
this.fields.put(formId, new String());
}
//... etc
}
当我单击菜单项时,生成一个新的随机 formId
并将新的表单数据持有者添加到 fields
映射中。
jsf 页面是这样的:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:p="http://primefaces.prime.com.tr/ui">
<div class="dataContainer clearfix">
<h:form id = "#{employeeBean.formId}">
<h:inputText id="empField1" value="#{employeeBean.fields[employeeBean.formId]}" />
<p:commandLink action="#{employeeBean.action}" value="Action" id="actionLink">
</p:commandLink>
</h:form>
</div>
</html>
最后打开的选项卡实例工作正常,但旧的则不然。如果在旧选项卡上按下 commandLink
,则会提交正确的表单,并在恢复视图阶段调用一次 getFormId
方法,但在我的 中不会调用其他任何内容EmployeeBean
。 如果我使用固定的 formId 而不是随机的 #{employeebean.formId}
那么总是提交第一个打开的选项卡的数据。 (页面上具有相同 id 的多个表单)。
我想我的组件树有问题。 我正在使用 Primefaces 的 TabView 组件(基于 jquery),并在 jquery javascript 函数中向选项卡组件添加新选项卡。
谁能告诉我我做错了什么?或提出更好的方法来解决问题?处理这种情况的正确方法是什么?
预先非常感谢
my jsf appication is designed so that by clicking on a menuitem the corresponding page is displayed in a newly created tab. the pages displayed on tabs have their own form element & backing bean.
normally this works pretty well, but could not find a way how to solve the situation when multiple instances of the same form is used concurrently, eg. more 'add new employee' tabs are opened at the same time.
i try to use a @SessionScoped
managed bean with a Map
containing instances of the form data (currently only one string for simplicity) like this:
@ManagedBean(name="employeeBean")
@SessionScoped
public class EmployeeBean extends BaseManagedBean implements Serializable{
private Map<String, String> fields = new HashMap<String, String>();
private String formId;
public void newForm(){
this.formId=RandomStringUtils.randomAlphabetic(10);
logger.debug("newForm: formId: " + formId);
this.fields.put(formId, new String());
}
//... etc
}
when i click on a menuitem, a new random formId
is generated and new form data holder is added to the fields
map.
the jsf page is this:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:p="http://primefaces.prime.com.tr/ui">
<div class="dataContainer clearfix">
<h:form id = "#{employeeBean.formId}">
<h:inputText id="empField1" value="#{employeeBean.fields[employeeBean.formId]}" />
<p:commandLink action="#{employeeBean.action}" value="Action" id="actionLink">
</p:commandLink>
</h:form>
</div>
</html>
the last opened tab instance works perfectly but the old ones do not. if the commandLink
is pressed on older tabs the right form is submitted and the getFormId
method is called once in the restore view phase, but nothing else is called in my EmployeeBean
.
if i use fixed formId not the random #{employeebean.formId}
then always the first opened tab's data is submitted. (multiple forms with same id on the page).
i suppose something is wrong with my component tree.
i am using Primefaces
's TabView
component (based on jquery) and i add new tabs to the tab component in a jquery javascript function.
Can anyone tell me what i am doing wrong? or suggest a better way to solve problem? what is the right approach to handle situtations like this?
thanks a lot in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您这里需要一个会话范围的 bean 吗?例如,您想从其他页面获取 Bean 内容吗?如果没有,那么也许您可以简单地使您的 EmployeeBean 请求限定范围。比起您的
employeeBean
中不需要映射,您的valuebindings
也可以更简单......Do you need here a session scoped bean? E.g. do you want to reach bean-content from other pages? If not, than maybe you could simply make your
EmployeeBean
request scoped. Than you don't need a map inside youremployeeBean
, and youvaluebindings
could also be more simple...