JSF: Reload request scope property after ajax-request - ajax

I have a hidden input field, which value is read from a property of the request scope:
<h:inputHidden id="myHiddenField" value="#{requestScope['myVar']}" />
I trigger an Ajax-Request where I change the value of myVar.
<p:commandButton value="submit" action="#{myController.doSomething}" update="myHiddenField">
But my input field still contains the old value.
Any idea how I can solve this?
UPDATE:
Maybe I have to explain it a little bit more.. myVar contains the IDs of all input fields with an error message (facesContext.getClientIdsWithMessages()).
When I first submit the form (with some validation errors) it works as expected. When I resubmit the form with some other validation errors the value of myVar doesn't get updated... (Still contains the IDs of the 'old' errors) When I resubmit the form with no validation errors myVar gets updated. (myVar is empty now)

If you want to access a bean after the page has been loaded it needs to be at least ViewScoped. RequestScoped beans are destroyed when the page is loaded (the request is handled and there is no need for it anymore).

Related

SelectMethod not called after form submission GET request

I have a form with method="get" set on the form. I have a control (DropDownList) with the SelectMethod attribute defined to populate the list's contents. On first load the SelectMethod is called and works (and I can verify this with a breakpoint). However, if I submit the form I can see the browser update and the query string get populated but SelectMethod is not called/I don't hit the breakpoint.
I'm more or less following the example from Microsoft here http://www.asp.net/web-forms/overview/presenting-and-managing-data/model-binding/using-query-string-values-to-retrieve-data
only I'm using a form submission with a GET instead of a link with a mutated query string.

METADATA parameters disappear after AJAX action

in template i'm defining a parameter
<f:metadata >
<f:viewParam name="pageKey" value="#{parameterProvider.pageKey}" />
</f:metadata>
All works fine except AJAX requests with makes this param disappear?
example ajax button in xhtml page:
Message value from server= [#{lockerBean.msg}]<br/>
<h:form>
<h:commandButton value="Ajax Action" styleClass="systemButton" >
<f:ajax execute="#all" render="#all" listener="#{lockerBean.ajaxButton(event)}"/>
</h:commandButton><br/>
</h:form>
ajax action is simply
public void ajaxButton(ActionEvent event){
msg="Ajax reload";
}
After ajax request pageKey is gone. If i set required="true" i get valudation error
j_idt3: Validation Error: Value is required. with clearly indicates, that parameter has been deleted. how can i preserve it?
I know about includeViewParams, but i don't understand this mechanism very well and it is not easy to add to all kinds of buttons and links... can i somehow force it to be passed or can i inject them somewhere in context?
EDIT:
this log might help to understand what is happening:
ParameterProvider, is sessionscoped CGI that holds pageKey. I modified getter & setter, so they log info about being called. In whole aplication getters and setters to this value is used only when applying request value in jsf lifecycle (my own code uses different methods)
14:04:06,400 OFF [ParameterProvider] (default task-12) Getting pageKey[-1] into viewParams.
14:04:06,401 OFF [ParameterProvider] (default task-12) Setting pageKey[2] from viewParams.
14:04:06,401 OFF [MultiActionLockListener] (default task-12) AJAX ACTION
14:04:06,414 OFF [ParameterProvider] (default task-12) Getting pageKey[3] into viewParams.
as you can see, before anything we have calling getter and setter for key (2 has been send from client 3 suppose to be sent to client in response)
calling another action will cause to log:
14:08:54,281 OFF [MultiActionLockListener] (default task-14) AJAX ACTION
14:08:54,294 OFF [ParameterProvider] (default task-14) Getting pageKey[7] into viewParams.
as you see, there is no getter and setter, which means pageKey is gone?

OmniFaces validateOrder disabling

I'm trying to use validateOrder component to validate two java.util.Date objects. It is similar to showcase example on this link (PrimeFaces example). Everything works perfect, but i have one question:
What if 2nd date field is not required?
In that case i'm getting nullpointer exception, and since validateOrder has "disabled" attribute, i was wondering is it worth/possible enabling/disabling it via ajax every time the 2nd date is inserted/removed. If not, i guess i'll stick to Balus' approach for JSF2.0 cross-field validation that you can read about on this link.
Let the disabled attribute check if the 2nd field is filled in. If it's not filled in, the request parameter value associated with field's client ID will be empty. Use exaclty that to let disabled attribute evaluate to true.
<p:calendar ... binding="#{endDate}" />
...
<o:validateOrder ... disabled="#{empty param[endDate.clientId]}" />
Code is as-is. No additional backing bean property necessary for binding.
See also:
How does the 'binding' attribute work in JSF? When and how should it be used?

Struts - binding form fields to bean

Lets say I have couple of filters in my deployment descriptor and each request to the app has to pass through those. There is form with a formbean attached to it. Once I fill the form and click submit, when does the html parameters in the form are binded to the form bean? Before the request is passed through the filters or after passing thorugh the filters?
The reason I am asking is.. I have a form with 20 fields. Once I click submit, 17 values are being binded to the bean properly but 3 values are missing by the team request reaches validate method. I am absolutely clueless has to where the values are missing. I tried to print an enumeration of all the request parameters in validate method and those three fields are there in request object. AFAIK, There is no issue with those three elements name because the app works perfecly in my local IDE but brakes up in test env which makes it all the more difficult to pin-point the issue :(.

How to know whether JSF bean setter is invoked from AJAX event or as part of form POST processing

I have a form that contains a Part Number inputText field and some other fields that represent the attributes of a part. If the user enters an existing part number, I want to populate the other fields with the part's existing attributes. The operator then has the option to change Part Number to something else (essentially creating a new part using the existing part as a template) and/or modify its attributes. I have an AJAX event defined to update the model when Part Number changes, so I can retrieve the attributes from the database:
<f:ajax event="valueChange" execute="#this" render="partlength" />
My question is this: How does the Part Number setter (e.g. setPartNumber() ) know whether it is being invoked as part of an AJAX event, in which case I want to fetch the attributes, or as part of the Update Model Values phase of the form being posted, in which case I don't? Or is there a better way to accomplish what I'm trying to do?
You can determine by PartialViewContext#isAjaxRequest() whether the current request is an ajax request or not. You can obtain the PartialViewContext by FacesContext#getPartialViewContext().
if (FacesContext.getCurrentInstance().getPartialViewContext().isAjaxRequest()) {
// The current request is an ajax request.
}
An alternative is to just do the job in the listener method of <f:ajax> instead of in the getter/setter (doing business job in getters/setters is a poor practice anyway):
<f:ajax listener="#{bean.listener}" render="partlength" />
(note that I omitted the event and execute attributes as you've used the default values already)

Resources