use multiple form instances jsf 2.0 - jsf

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

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...

Related

How to set a dynamic value for ui:param [duplicate]

I am new to JSF and I am struggling with dynamicaly rendering included pages. My code looks like this:
MenuBean
#ViewScoped
public class MenuBean implements Serializable {
private MenuItem[] menuItems = new MenuItem[] {
new MenuItem("page_1", "/page_1.xhtml"),
new MenuItem("page_2", "/page_2.xhtml"),
};
private String selectedItemLabel;
//...
}
MenuItem
public class MenuItem implements Serializable {
private String label;
private String page;
//...
}
index.xhtml
<ui:repeat var="menuItem" value="#{menuBean.menuItems}">
<h:panelGroup rendered="#{menuBean.selectedItemLabel eq menuItem.label}" layout="block">
<h:outputText value="#{menuBean.selectedItemLabel}" />
<ui:include src="#{menuItem.page}" />
</h:panelGroup>
</ui:repeat>
The result is 2 buttons rendered. Whenever I click any button the label inside conditionaly rendered panelGroup appears but included page doesn't. If I change 'menuItem1' var from first ui:repeat it works but it is really unpredictable. For example if I hardcode setSelectedItemLabel parameter to 'page_1' then when I click to button_1 page_1 is displayed but even if I click to button_2 page_2 (!?) is displayed...
You're facing a view build time vs view render time problem. This is essentially the same problem which is already answered in detail in JSTL in JSF2 Facelets... makes sense? In that answer, replace "JSTL" with "ui:include". To the point, the <ui:repeat> runs during view render time, while the <ui:include> has already run during view build time before.
You'll understand that the only solution is to replace the <ui:repeat> by another tag which runs during view build time, such as JSTL <c:forEach>.
Note that I don't guarantee that it will solve the concrete functional requirement which you've in mind. Using JSTL may have undesirable "side effects" (which are however explainable, understandable and workaroundable).
See also:
How to ajax-refresh dynamic include content by navigation menu? (JSF SPA)

Failing to populate Select element using RequestScoped bean? [duplicate]

This question already has answers here:
How to populate options of h:selectOneMenu from database?
(5 answers)
What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
(2 answers)
Closed 5 years ago.
I've been struggling with a task that seemed to be very simple, but somehow I can't see a way to solve it without using some pretty hacky code - which I really want to avoid.
The problem itself has been reduced to a very simple task: I have a project with a single page containing a form, where I have to type a non-empty username and use a select element (h:selectOneMenu) to choose one "user group" for this user. This is submitted to a RequestScoped bean that I have (this is also the only bean I have in the project). Only validation rule (to simplify my explanation) is: the "username" field cannot be empty.
Here's my page's code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://xmlns.jcp.org/jsf/passthrough">
<h:head></h:head>
<h:body>
<f:metadata>
<f:event listener="#{userManagementBean.initializeGroupsList()}" type="preRenderView" />
</f:metadata>
<h:form>
<h:inputText id="fieldUsername" value="#{userManagementBean.user.username}" p:placeholder="Username" required="true" />
<h:message for="fieldUsername"/>
<h:selectOneMenu id="fieldGroup" value="#{userManagementBean.usergroup_id}">
<f:selectItem itemValue="#{null}" itemLabel="Please, select a group" />
<f:selectItems value="#{userManagementBean.groupsList}" var="curGroup" itemLabel="#{curGroup.groupName}" itemValue="#{curGroup.id}" />
</h:selectOneMenu>
<h:message for="fieldGroup"/>
<h:commandButton action="#{userManagementBean.submitValues()}" value="Submit data" />
</h:form>
</h:body>
</html>
And here's the bean's code, which initializes a list containing the elements of the select box:
import java.io.Serializable;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
....
#RequestScoped
#Named
public class UserManagementBean implements Serializable {
// PRIVATE FIELDS
private User user = new User();
private List<UserGroup> groupsList;
private Integer usergroup_id;
// BEAN'S PUBLIC METHODS
public void initializeGroupsList() {
groupsList = loadAllGroupsFromDatabase();
System.out.print(String.format("%d groups have been loaded\n", groupsList.size()));
}
// Getters, setters and database-loading code has been ommited
...
}
Question is: when/how should I initialize the list of user groups, which is used to populate the select element on the page?
First I tried to use an <f:event> element which calls initializeGroupsList() on the "preRenderView" event (as in the code I've wrote above). Everything is populated fine, but if I select a user group and do not type any username, the select element returns an error message after the form is submitted ("Validation Error: Value is not valid"), while it should not give me any validation errors, as the user has selected a valid user group.
Secondly, I have tried replacing the <f:event> by a <f:viewAction> which calls initializeGroupsList(). When I open the page for the first time, everything works fine. But when I submit the form without a username, the page is reloaded, showing me the validation error, and the select element is not populated with any user groups at all (the initializeGroupsList() isn't called at all when there are validation errors in this case).
Currently I'm solving this issue by removing both the <f:event> and <f:viewAction> elements, and annotationg initializeGroupsList() with #PostConstruct. Problem is: if I do so, I won't be abble to access any of the URL's query string parameters through the use of <f:viewParam> inside the initializeGroupsList(), which is something I want to be able to do in this page in a near future.
How should I call initializeGroupsList() then, to avoid these errors described in both the first and second cases, while allowing me to access URL's query string parameters inside initializeGroupsList()?
Thanks in advance.
Keep using #PostConstruct to initialize initializeGroupsList().
About the view params you could use #Param ( http://showcase.omnifaces.org/cdi/Param ) from ominifaces, which will inject them for you without the need to use view action or view event.

Why does my Object become null when going from bean to .xhtml page in jsf

I am building a JSF 2.0 Application using primfaces. I have a managed bean that has several objects for different scenarios.
In this particular portion of the application I move from a data table/form to a managed bean with an identification number.
I pull the information from the database and even have it in the log. However when I make it to the
JSF xhtml page the object values are null and I can't seem to figure out why, any help would be great as I am getting no errors,
exceptions, or warnings in the logs and all information seems to point to the loading of the data including the log.
JSF Page that I am coming from... jobs.xhtml (I am using jsf 2.2 and primefaces to build this application) From this page the
link specifies a job number to be retrieved and the next bean will retrieve that job based on the job number I have it down to just a button
that will take you to the primary method and get the information to the next page...
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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">
<body>
<div style="margin-bottom:350px;">
<p:column>
<h:form>
<h:commandButton id="editProject" action="#{editProjects.getProjectForm()}" value="Edit" />
</h:form>
</p:column>
</div>
</body>
</html>
EditProjects.java the backing bean for the editMailerJob.xhtml which will take in the medium id and the job number the medium id will be used
to direct the application to the next page and the job number will be used to retrieve a specific job
::::UPDATED::::
package beans;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.RossProjectManagement.rpm.objects.MailerProject;
#ManagedBean
#RequestScoped
public class EditProjects implements Serializable {
/**
*
*/
private static final long serialVersionUID = -6680733133634363295L;
private final Log LOG = LogFactory.getLog(this.getClass().getName());
// Editable projects
private MailerProject edit_mailer = new MailerProject();
//Changed this to be a static set value
private String mailerProject = "THIS IS THE MAILER STRING";
public int getProjectForm() {
loadEditMailer();
return 1;
}
public void loadEditMailer() {
this.edit_mailer = new MailerProject("REDJ15061005",
convertDate("2015-06-29"), false, "RickD",
"RickD and the Gang", "new", true, true, "SPEC", 1, 1, 1,
"REDJ15061005", "11X17", "SPOT_COLOR", "20,000",
convertDate("2015-06-30"), convertDate("2015-07-13"),
convertDate("2015-07-06"), convertDate("2015-07-06"),
convertDate("2015-07-15"), true, "BDC",
"someone#example.com", "STANDARD", 7500.00, "CONQUEST",
"NONE", "JS DIRECT", "NONE");
//Commented this out to keep the value static
//setMailerProject(edit_mailer.toString());
LOG.debug(":::MAILER PROJECT TO STRING::: " + this.mailerProject);
}
public Date convertDate(String date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null;
try {
date1 = df.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return date1;
}
}
JSF page that I am redirecting to editMailerJob.xhtml
<?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">
<body>
<div style="margin-bottom: 350px;">
<h:outputLabel>#{editProjects.edit_mailer}</h:outputLabel>
<h:outputLabel>Mailer String::: #{editProjects.mailerProject} </h:outputLabel>
</div>
</body>
</html>
The log shows that all of the information has been received and loaded but shows no where that the information has been cleaned or the object made anew. I have tried instantiating the object in different places at different times but that didn't work.
Before I leave the bean the object has value, when I get to the page the object has no value, I don't understand why
I then tried loading the information from different places. I have tried changing the scope of the bean from request to view back to request but no luck, (I thought about doing session but I didn't see the benefit as well as I just couldn't see reasoning to put all of that information into the session).
The problem seems to be revolving directly around the bean itself and the getProjectForm Method because at one point the object is loaded and then it just loses all of its information.
All of my values seem to be correct as far as I can tell but none of the information is loading.
I can't seem to wrap my head around the issue so any help would be greatly appreciated. if any more information is needed just let me know.
I hope all of this helps Let me know if I have forgotten anything.
EDITS:::
I have created just a string to see if at any point it would be delivered either, and so far I am still getting nothing, the .xhtml page is reading that it exists but not that it has value. Still working with this if anyone else has a solution it would be awesome, or if you know the solution and could at the least point me in the right direction that would be great also.
UPDATE #2
So I found that if I set the value outside a method it will properly be displayed so it seems to me like the methods are keeping the value from the xhtml page. What is the deal with that, I have getters and setters for the variables I have them declared and defined properly, but for what ever reason the values when loaded by the method are not kept outside the method, the values are not able to be accessed by the xhtml page.
10:00 pm update
Found out that if I instantiate the MailerProject object the same way I don't get the same results I still produce an object full of null attribute values.
Any ideas guys?
Start Here - It'll make the rest of this very trivial.
The Problem
You're using a #RequestScoped bean, and what you're experiencing is the expected behaviour for beans of that scope.
Between the first and the second page, you should understand by now, that you're dealing with two different instances of the editProjects bean : a field you set on the first page, backed by one instance of editProjects will not be available on the destination page, backed by a new instance of editBean.
To Solve
While it wasn't mentioned in the answer I linked to, what you need here is the FlashScope, that serves the express purpose of being transitory in nature: use it just to transport "stuff" between two pages/beans/scopes etc. Sorta. To use:
Declare your undying love for the flash scope around the point of origination of the data. In so doing, you're storing your data in the flash scope object, as implicitly provided by JSF, after which you can then navigate away:
Flash fScope = FacesContext.getCurrentInstance().getExternalContext().getFlash();
fScope.put("editMailer",edit_mailer);
You can then refer to the variable you stored in the flash scope, on the destination page:
<h:outputLabel>#{flash.edit_mailer}</h:outputLabel>
Like I mentioned earlier, the flash scope is a transitory scope: once the destination page has been rendered and the content of the scope displayed, you can consider the data as good as gone. If you would like to hang on to it for just one page redirect longer, you can specify the keep directive:
<h:outputLabel>#{flash.keep.edit_mailer}</h:outputLabel>
Unrelated to the problem
Avoid doing any heavy lifting in a getter/accessor method (like you're doing in getProjectForm). It's bad for business
Why JSF calls getters multiple times
Initialization of List in a JSF Managed bean
What's the significance of returning "1" from that navigation method? Why not "destination_page", or "edit_page.xhtml", y'know, something easier to read? That's just a style thing anyway, no evil can come of that.

Creating command links dynamically from a managed bean

I have a need to create command links dynamically based on content coming from elsewhere.
When a user clicks on a link it should call a method in a managed bean, and the method needs to know which link was clicked.
I can create the command links using the following code:
JSF:
<h:outputText value="#{myBean.dynamicLinks}" escape="false" />
Bean:
public String getDynamicLinks(){
// Return an html string that contains a set of <a> elements, based on the dynamic content
}
This works fine, but what I can't work out is how my <a> elements can call back into the bean.
This is not the right way to "dynamically" create markup. For that you should be using XHTML, and absolutely not Java and for sure not massage some plain HTML in the model and present it with escape="false". That's plain nonsense. You're basically mingling the view into the model. You need to make the model itself dynamic, not the view. The view must be static and dumb. The view must just present the model to the world. The model itself can be dynamic. You normally achieve that by using a flexible collection, such as List<String>, List<SomeEntity>, etc which you then present using an iterator in the view such as <ui:repeat> or <h:dataTable>.
E.g.
<ui:repeat value="#{bean.links}" var="link">
<h:commandLink value="link" action="#{bean.action(link)}" />
</ui:repeat>
public void action(Link link) {
// ...
}
You see, the action method can know about the pressed link by just inspecting the method argument.
See also:
How to create dynamic JSF form fields
How can I pass selected row to commandLink inside dataTable?

Dynamic <ui:include src> depending on <ui:repeat var> doesn't include anything

I am new to JSF and I am struggling with dynamicaly rendering included pages. My code looks like this:
MenuBean
#ViewScoped
public class MenuBean implements Serializable {
private MenuItem[] menuItems = new MenuItem[] {
new MenuItem("page_1", "/page_1.xhtml"),
new MenuItem("page_2", "/page_2.xhtml"),
};
private String selectedItemLabel;
//...
}
MenuItem
public class MenuItem implements Serializable {
private String label;
private String page;
//...
}
index.xhtml
<ui:repeat var="menuItem" value="#{menuBean.menuItems}">
<h:panelGroup rendered="#{menuBean.selectedItemLabel eq menuItem.label}" layout="block">
<h:outputText value="#{menuBean.selectedItemLabel}" />
<ui:include src="#{menuItem.page}" />
</h:panelGroup>
</ui:repeat>
The result is 2 buttons rendered. Whenever I click any button the label inside conditionaly rendered panelGroup appears but included page doesn't. If I change 'menuItem1' var from first ui:repeat it works but it is really unpredictable. For example if I hardcode setSelectedItemLabel parameter to 'page_1' then when I click to button_1 page_1 is displayed but even if I click to button_2 page_2 (!?) is displayed...
You're facing a view build time vs view render time problem. This is essentially the same problem which is already answered in detail in JSTL in JSF2 Facelets... makes sense? In that answer, replace "JSTL" with "ui:include". To the point, the <ui:repeat> runs during view render time, while the <ui:include> has already run during view build time before.
You'll understand that the only solution is to replace the <ui:repeat> by another tag which runs during view build time, such as JSTL <c:forEach>.
Note that I don't guarantee that it will solve the concrete functional requirement which you've in mind. Using JSTL may have undesirable "side effects" (which are however explainable, understandable and workaroundable).
See also:
How to ajax-refresh dynamic include content by navigation menu? (JSF SPA)

Resources