1
votes

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

1
probably not the best solution but i managed to submit all of my tabs. the trick is that i use the same id for all forms and i replaced the PrimeFaces.ajax.AjaxRequest with my own AjaxRequest which always posts the current form (where the CommandLink is pressed) instead of the first one.gulygab

1 Answers

2
votes

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 your employeeBean, and you valuebindings could also be more simple...