Update PrimeFaces dataTable from backing bean - jsf

I use schedule(Timer) in bean which update variable when employees enrolling which get data from fingerprint device. I need to update component in jsf from backing bean when variable in bean updated. I try to use primefaces poll component but it update component every time.

Assuming you'll be calling a Java method using Listener from Fingerprint device API, and from ManagedBean you can update any Primefaces Component using RequestContext.
RequestContext.getCurrentInstance().update("ID_OF_YOUR_DATATABLE")

There's already a component in Primefaces, as you can see : here.
RequestContext is a feature with various handy utilities.
Update component(s) programmatically. Execute javascript from beans.
Add ajax callback parameters. ScrollTo a specific component after ajax
update.
Have a look here for an example.

I resolved my problem with primefaces push component.
java code is:
PushContext pushContext = PushContextFactory.getDefault().getPushContext();
pushContext.push("/finger", "Pressed");
in jsf:
<p:socket channel="/finger" onMessage="clearCookies" />
"clearCookies" is remoteCommand function which update my dataTable.

It has changed from primefaces 7.0
PrimeFaces.current().ajax().update("elementJS");

Related

How to extend functionality for JSF primefaces component?

I'm using Primefaces 4.0.
The datatable has an issue that after a table update the filter values are away. There is a new attribute in Primefaces p:column "filterValue". The attribute binds the value of a filter to a backing bean property. The problem is that the value of this attribute is only used for backing bean propertie getter. The setter method is not called.
I have made a patch for this component. In classes DataTabe and DataTableRenderer, the setter for the method checks in param's what is the client setting for filter and set the backing bean propertie. The changes is made in DataTableRender decode and FilterFeature decode methods.
Links to original classes on grepcode:
DataTable decode
FilterFeature decode
So the question: if you want to call setter for a component in which JSF-Phase or which method is suitable for this. Is decode-Method place where you can call setters ? What is standard approach in this situation ?

Manipulating a disabled attribute in client side; does JSF test properly if component is disabled?

Primefaces 3.5.10, Mojarra 2.1.21, Omnifaces 1.5
I am thinking about security issues.
I set the component attribute with the component.getAttributes() method. This method returns a HashMap with attributes. Is it safe to set the ("disabled", true)-pair in this map to disable the component (for example p:inputText-component)? I use it from an actionListener, (Phase 5 or 4) of jsf pipeline. So possibly it has implications for render phase only. But I could manipulate the disabled attribute from input method on the client and then post the manipulated values. Does the server make test if the component is disabled and rejects the changes ?
What is the best way to go ?
all components in panelGrid will be disabled:
xhtml:
<p:panelGrid>
<my:component/>
<p:input value=#{mybean.value} />
</p:panelGrid>
Bean:
for (UIComponent component : l) {
component.getAttributes().put("disabled", true);
recursion(....);
}
But I could manipulate the disabled attribute from input method on the client and then post the manipulated values.
Yes, the enduser could.
Does the server make test if the component is disabled and rejects the changes ?
Yes, JSF does it based on component tree state, not on the submitted value. So that part is safe. It does that by the way also for readonly and rendered attribtues.
See also:
Why JSF saves the state of UI components on server?
Which properties in a JSF backing bean can be set by a user?
commandButton/commandLink/ajax action/listener method not invoked or input value not updated (point 5)

Update ui components from server event

How would I go about updating a UI component when a server process finishes or anytime else for that matter?
Can\Should I
extend the UI component and register some kind of EventListener on it?
bind the UI component in a backing bean?
use some of the new PrimePush features?
something else?
If anyone could guide me in the right direction I'd appreciate it.
Example Case One
Server process ends and updates the database.
I want to automatically
update a DataTable with new rows based on the new information.
I don't want to poll for the new data.
EDIT
I want to update the UI component (a primefaces datatable) from an EJB Stateless Bean
You didnt mention which version of primefaces you are using but you can update components from backed bean with addpartialupdatetarget.
RequestContext context = RequestContext.getCurrentInstance();
context.addPartialUpdateTarget("myForm:myComponent");
You can just use the update attribute of your commandButton or whatever else you're using.
Something like this should work
<p:dataTable value="#{backer.bean}" id="table">
// columns with data
</p:dataTable>
<p:commandButton actionListener="#{backer.updateDB}" update="table" />

JSF 2 : Accessing managed bean's instance in the phase listener object?

Is it possible to get a reference to the to-be-executed managedbean in the before-invokeApplication-phaselistener ?
Before the invoke application phase, it should be clear which managedBean that is going to execute the method.
For the sake of the example, assume there's 1 main manage bean to handle 1 jsf page.
So what i need is basically :
The user access the program from the menu
Because it's accessed from the menu, the main manage bean's init() method gets called to initialize stuffs like preparing data, doing authorization checks
Subsequent submits dont need to call the init() method anymore until it's reaccessed from the menu
To implement the point #2, im thinking of intercepting one of the phases
I've checked the API docs about getting the managed bean in the phases implementation, but i couldnt seem to find any.
After typing this question, i realize i could do this in #PostConstruct or the managed bean's constructor, but that would do only at the first time the bean is constructed, and my need is to call the method everytime the jsf is being accessed from the menu.
Any suggestions ?
Regards,
Albert Kam
You can access your managed beans via the ELContext/ELResolver. This is explained nicely in the MyFaces wiki (also works in vanilla JSF).
For example:
ELContext elContext = FacesContext.getCurrentInstance().getELContext();
NeededBean neededBean = (NeededBean) FacesContext.getCurrentInstance().getApplication()
.getELResolver().getValue(elContext, null, "neededBean");
See the MyFaces wiki entry for further explanation, and implementations for other JSF versions.
Your idea of using #PostConstruct is a good one. Consider changing your scope to something liked #ViewScoped, so the logic is executed everytime you navigate to that view.
Also, have a look at the PreRenderViewEvent (for JSF 2). This code is embedded in your facelet page:
<f:metadata>
<f:viewParam name="foo" value="#{bean.foo}"/>
<f:event type="preRenderView" listener="#{bean.doSomething}"/>
</f:metadata>
The f:event listener is executed before every page view.

JSF and richfaces: h:commandlink in richfaces table not working properly

When using h:commandlink(or commandbutton) inside a rich:dataTable, the action specified is never invoked, neither is the corresponding managed bean instantiated(whether it is at request or session scope)...
instead, the same request is performed.. (page reloads)..
have seen what appeared to be similar issue on forums, but is not actually the problem i am having..
the h:commandlink /button work ok outside of the rich:datatable..
Does anyone have any advice?
here is a code snippet:
<h:commandLink id="commLink" actionListener="#{hBean.test}" action="#{hBean.viewTranslation}">
<h:outputText value="#{trans.translationName}"/>
</h:commandLink>
</rich:column>
The bean is apparently request scoped and the datamodel is not been loaded during bean's construction (at least, during apply request values phase of the subsequent request). You need to preserve the same datamodel for the subsequent request, else JSF cannot locate the row item associated with the clicked link. The most straightforward way is to load the datamodel in the bean's constructor or #PostConstruct method.
A quick fix/test is to put bean in session scope. The datamodel will then be saved in the session scope and be available in the subsequent request. But this has more impact on user experience (e.g. unexpected results when having the same page opened in different browser windows/tabs in the same session). If you're already on JSF 2.0 (which is likely not the case since you're using RichFaces), then the new view scope would have been the solution.
Related questions:
h:commandLink is not been invoked - contains an overview of all possible causes of this behaviour.
If you are using RichFaces 3.0.0 through 3.3.3, use the tag a4j:keepAlive. It will works even with request scope.

Resources