Using forms on multiple tabs to edit a single entity - jsf-2

In my application I have implemented a page where the user can edit its account information. Since there are a lot of fields I have broken them up into p:tab elements inside a p:tabView. Like this:
<p:tabView dynamic="true" rendered="#{account.accessOK}">
<p:tab title="Basic Information">
<h:form id="formBasic">
(some fields here)
<p:commandButton value="Save"
action="#{account.doSave()}"
update="formBasic msgs"
/>
</h:form>
</p:tab>
<p:tab title="Address">
<h:form id="formAddress">
(fields related to address here)
<p:commandButton value="Save"
action="#{account.doSave()}"
update="formBasic msgs"
/>
</h:form>
</p:tab>
<p:tab title="E-Mail & Phone Numbers">
(then another form here with more field)
</p:tab>
<p:tab title="Password">
<h:form id="formPass">
<p:panelGrid columns="3">
(and this form handles the password)
<p:commandButton value="Change"
action="#{account.doPassword()}"
update="msgs"
/>
</h:form>
</p:tab>
</p:tabView>
This seems pretty straightforward but there is a problem. Each tab has its own form so that the fields that gets processed when the user hits the p:commandButton are only those in the form. However the following sequence will probably confuse the user:
Change some fields on one tab but do not click the Save button.
Go to another tab and fill in fields there, then click the Save button. The fields on that form will be processed.
Go back to the tab edited in step 1 above and the fields will look like they have been processed, but have not. It will be a filled out form but no visual way to tell that the fields have not been saved.
Note that I do not want to put all the tabs into one form and execute all the fields on all the tabs at once. This is because some field changes might have side effects, such as setting the password. Also you don't want a validation pass to happen on fields that the user isn't changing during this visit.
So is there any method to solve this user interface deficiency?

Related

security in Jsf2 guessing and modifying url should not be allowed

I have an application up and running. I have a page addstudent.xhtml page where the user adds the student details.When user finishes adding and submits he will be redirected displaystudent.xhtml page.
My question is when user directly types on browser
localHost:8081/Student/displaystudent.xhtml. Page is displayed with blank values.This should not happen. How do I prevent this even though he types on browser.
Both the files addstudent.xhtml displaystudent.xhtml pages are in web-inf folder. I am using Jsf2, primefaces 3.5
Make displaystudent.xhtml an include file which you render conditionally in addstudent.xhtml depending on whether the student is successfully added.
E.g., assuming standard JSF:
<h:panelGroup id="addStudent" layout="block">
<h:form rendered="#{empty bean.student.id}">
<h2>Add student</h2>
<h:inputText value="#{bean.student.name}" />
...
<h:commandButton value="Add" action="#{bean.add}">
<f:ajax execute="#form" render=":addStudent" />
</h:commandButton>
</h:form>
<h:panelGroup layout="block" rendered="#{not empty bean.student.id}">
<h2>Added student</h2>
<dl>
<dt>Student name</dt><dd>#{bean.student.name}</dd>
</dl>
</h:panelGroup>
</h:panelGroup>
(simplified; you could of course split off the add student to <ui:include src="/WEB-INF/displaystudent.xhtml"> and so on)
The button, whereby the add() method should return void or null, re-renders the <h:panelGroup id="addStudent">. If the student was successfully been added in the DB, then it should have a non-empty ID. This will cause the form to be hidden and the results panel to be shown.
See also:
Conditionally displaying JSF components
How to navigate in JSF? How to make URL reflect current page (and not previous one)

JSF2 Dynamic form is not being submitted

I have a form that is defined in a separate jsf page. This is is included to the main page when i click a link. Now the form is being displayed correctly. But the problem is that the submit button is not calling the action function defined.
The code to include the page( As suggested in this question: JSF2 Dynamically loading pages by ajax
<h:panelGroup id="editdivparent" layout="block">
<h:panelGroup id="editdiv" rendered="#{formsBean.edituserdiv}" layout="block">
<h:form id="userform" class="form-horizontal">
<ui:include src="edituserdetails.xhtml">
</ui:include>
</h:form>
</h:panelGroup>
</h:panelGroup>
The included page contains just the form elements with submit button:
<h:commandButton action="#{userBean.register() }" value="Update">
</h:commandButton>
I am getting no errors. On submitting the form the current page is redisplayed. I have put some print statements in the action function. Also there is a query error is put. None of them are being generated.
Am i doing something wrong here?
The commandButton action property already expects a method expression, so just take the parenthesis out, like this:
<h:commandButton action="#{userBean.register}" value="Update" />
Also, make sure you don't have nested forms when making templates with includes.
I hope it helps.

Immediate="true" causes update="component" to fail

I have a command button (a Cancel button to be specific) that I want to bypass the validation of some components on my page. When I have immediate="true" set on my command button, the update attribute does not work. By "not work" I mean that the values on :centerForm:p_definition do not reset to what they should be. If I select an item from a dropdown or enter data into an input text, the information should disappear when clicking cancel. Setting immediate="false" or leaving it off completely does reset the fields as expected. Here is the definition of the commandbutton I am having trouble with.
<p:commandButton value="Cancel" style="float: right;"
immediate="true"
actionListener="#{manageBomPropHandler.doCancel()}"
update=":centerForm:p_definition"/>
Is that the expected behavior from immediate="true"? If so, how have you gotten around this for cancel buttons?
"Update" will be called/executed at Update Model Value phase. When you set "immediate" attribute to button command, this phase is ignored.See below images for more detail.
P1: Standard Lifecycle executes Update Model Values phase
P2: Immediate Lifecycle ignored Update Model Values phase
If may help, there's a already solution for clean input fields with primefaces:
<p:commandButton value="Reset Tag" update="panel" process="#this" style="margin-right:20px;" >
<p:resetInput target="panel" />
</p:commandButton>
OR
<h:commandButton value="Reset p:ajax" style="margin-right:20px;" >
<p:ajax update="panel" resetValues="true" />
</h:commandButton>
Source: http://www.primefaces.org/showcase/ui/misc/resetInput.xhtml
In my case, in order to update the fields, I have attached p:ajax against the required component.
That ensured that corresponding fields/variable in backing bean is updated as soon as user makes changes in any of those fields.
Another requirement I had is to refresh a data table after user clicks a immediate - true command button.
In order to do that, I have used code like:
RequestContext.getCurrentInstance().reset("Datatable ID");
Yup, displaying my ignorance of the JSF lifecycle. Found the answer here: JSF commandButton with immediate="true" and here: https://cwiki.apache.org/confluence/display/MYFACES/Clear+Input+Components

JSF not firing bean setters when using f:ajax

This is an update on a previous post I made regarding conditional rendering of page components. I can now conditionally render different components on the page based on selected user inputs using the f:ajax tag. The trouble is that when I click my commandbutton the inputs that are conditionally rendered using ajax aren't being read on submit. I am guessing it is because the whole page isn't rerendered when I add in the new components and the submit button can't see them, even though they are there. Here is the segment of my form page(it is wrapped in a h:form tag):
<h:outputLabel for="selectPersonType" value="Select Type: "/>
<h:selectOneMenu id="selectPersonType" value="#{addPersonBean.personType}" label="Person Type" required="true">
<f:selectItem itemValue=""/>
<f:selectItems value="#{listBean.personTypes}" />
<f:ajax render="results"/>
</h:selectOneMenu>
<h:message for="selectPersonType" style="color: red"/>
</h:panelGrid>
<h:panelGroup id="results">
<h:panelGroup rendered="#{addPersonBean.personType == 'STUDENT'}">
<h:outputLabel for="selectSchoolType" value="School Type: " />
<h:selectOneMenu id="selectSchoolType" value="#{addPersonBean.schoolType}">
<f:selectItems value="#{listBean.schoolTypes}" />
<f:ajax execute="selectSchoolType"/>
</h:selectOneMenu>
</h:panelGroup>
<h:panelGroup rendered="#{addPersonBean.personType == 'PATIENT'}">
<h:outputLabel for="smoker" value="Smoker? " />
<h:selectBooleanCheckbox id="smoker" value="#{addPersonBean.smoker}">
<f:ajax execute="smoker"/>
</h:selectBooleanCheckbox>
</h:panelGroup>
</h:panelGroup>
<h:commandButton value="Add Person" action="#{addPersonBean.action}"/>
The trouble here is that the components with ids 'selectSchoolType' and 'smoker's values don't get set when I click the commandButton because they are rendered conditionally using the selectPersonType select menu after the page has loaded. Is there a way to fire the components when their values change instead of when I click the submit button (therefore, their values should be processed before I even click submit). As you can see I have tried to use f:ajax with the execute attribute attached to the components in question but it didn't seem to work. (I believe the default events for these components are valueChange so haven't added them to the f:ajax tags, but I have tried 'change' and 'valueChange').
All your <f:ajax> things are inside a conditionally rendered component. This construct will fail when the condition behind rendered attribute evaluates to false during processing the form submit. This will happen when the #{addPersonBean} is in the request scope and doesn't preinitialize the condition behind the rendered attribute during (post)construction, or when it's performing business logic in getters/setters in a wrong way.
Placing the #{addPersonBean} in the view scope and ensuring that you aren't doing business logic in getters/setters should fix this problem.
See also:
commandButton/commandLink/ajax action/listener method not invoked or input value not updated - point 5 applies to you
How to choose the right bean scope?

Logout Link is not working

I am using JSF and Primefaces in my web application.
In this i am using one form which includes some some details to be filled by student.
So there is some validation on that fields
And, On this page there are two button one is Submit and Other is Logout
When I pressed submit button form is displaying validation error if details are not completed.
But Same things also shown while pressing LOGOUT link also
Logout is p:commadLink
<p:commandLink styleClass="logout" type="submit" action="#{userManagedBean.logout}">
<p:graphicImage value="/images/logout.png"></p:graphicImage>
</p:commandLink>
Submit is h:commandButton
<h:commandButton image="/images/submit_button.png" type="submit" action="#{referManagedBean.saveDetails}" value="submit"></h:commandButton>
I need Logout link skip this validation step and directly goes to managed bean called function.
How can i skip Validation Step ??
Since the log-out action seems that has nothing to do with the details form (being validated), you could just move the button to different form (h:form) so it wouldn't trigger the details form validation anyway.
I solved my problem i used immediate="true" by which it skips the validation phase.
<p:commandLink styleClass="logout" type="submit" immediate="true" action="#{userManagedBean.logout}">
<p:graphicImage value="/images/logout.png"></p:graphicImage>
</p:commandLink>

Resources