Updating an UI value from javascript is not getting reflected in javabean - jsf

I am trying to update an UI text field in primefaces 6.2 using java script method. Though I am able to update the value from UI side, it is not reflecting in backing managed bean
Xhtml:
<h:inputHidden id="test" value="#{mybean.fieldname}" valueChangeListener="#
{mybean.method}">
<f:ajax/>
</h:inputHidden>
Javascript:
function update(){
document.getElementByID('form:test').value="change";
alert(document.getElementByID('form:test').value);
}
I expect my value changehandler to get called since I updated my value but nothing occurs.can some one pls tell where I am getting wrong
Edit :Actually I am trying to submit the value changed from UI side alone using Js to the actual bean value, basically dom alone is changed and kind of trying to submit the same using any ajax calls. but still it is not working. Refered this link:When to use valueChangeListener or f:ajax listener? Can some one pls give some insights on how this can be achieved?

You have to trigger explicitly a change event by JavaScript if you set a value by JavaScript.

Related

JSF DocumentViewer fails to display PDF in #ViewScope

I am relatively new to JSF thus need some help.
Problem: I have webpage that displays a PDF. Thus I created a xhtml as follow
<p:panel rendered="#{mainAppView.getMessages().isEmpty()}" styleClass="preview-panel">
<div class="document-viewer-wrapper">
<pe:documentViewer
url="#{previewView.previewUrl}"
download="Document.pdf"
id="pdfPreview"/>
</div>
</p:panel>
URL is provided by a BackingBean previewView. Thus when the jsf tries to display the document, it makes a call to WebServlet. The WebServlet downloads the document and displays it and if it fails to download, an error message is shown on the DocumentViewer.
Is there a way to I can notify the ViewScope Bean about the failure? I want to disable a tab on the screen if the document download fails. I read that servlet can't make call to viewScope Bean.
Do not use ViewScoped with PDFViewer use RequestScoped/SessionScoped/ApplicationScoped instead.
See: https://github.com/primefaces-extensions/primefaces-extensions.github.com/issues/796
BalusC explains why ViewScoped can't be used as well: https://stackoverflow.com/a/18994746/502366
I ended up writing a JS to achieve the desire result. JS monitored the pdfviewer and if it fails to load the document, I updated the message on the pdfviewer.

PrimeFaces - Set a JSF component as mandatory

I am using PrimeFaces and JSF - I need to be able to set a component on the page as mandatory in response to an AJAX event. Is the best way to accomplish this using the following code or is there also a way to accomplish it using JQuery ?
Thanks
UIInput componentToChange = (UIInput) facesContext.getViewRoot().findComponent("ComponentId");
componentToChange.setRequired(true);
Thanks
Just set the component's required attribute with the desired EL expression.
E.g.
<h:inputText ... required="#{bean.required}" />
There are even EL ways without needing an additional bean property, but it's impossible to propose one based on the sparse information provided so far.
Use findComponent() with care. Think twice if it really can't be done just in the view (XHTML) side.

JSF render phase (why is my code being executed?)

I'm currently investigating the performance of a JSF application. I have noticed that code is being executed even though rendered is set to false. For example, take this piece of code:
<h:panelGroup rendered="#{bean.render}">
<my composite component here/>
</h:panelGroup>
Even though #{bean.render} returns false, I can clearly see from debug logs, that the code for my composite component is being executed during the render phase. It also looks like the code is being executed before #{bean.render} is even called. It isn't rendered in the HTML returned to the client, but it still appears that the server is executing the code.
Can anyone explain this?
Thanks.
Composite components are built during render response phase. JSF needs to populate the component tree first and then generate HTML based on the component tree. You're inside the composite component apparently referencing some bean properties which are mandatory to be evaluated during view build time.
If you'd like to conditionally control the building of the composite component instead of the rendering, then you need to use a conditional view build time tag instead of the rendered attribute. JSTL offers the <c:if> and <c:choose> for that.
<c:if test="#{bean.build}">
<my:composite />
</c:if>
See also:
JSTL in JSF2 Facelets... makes sense?
Jsf have to know if your components are rendered or disabled or whatever. Let say you say disabled="false" it is shown on clint side and client may change value and submit the form, even though javascript is disabled by the client, jsf checks it's disabled false or true on server side. if it was true it is not acceptable and never comes to your bean because of process validation phase of jsf, same as rendered="false"

have to press command button twice

I'm working on building a web page and notice now that I have to press the command button twice. Any command button has the same problem, so I figured I would add and action listener on one of them to see if I could see something.
<h:form id="formP">
<p:commandButton id="temp" value="photos" actionListener="#{viewBacking.debugBreakpoint()}" action="userPhoto" />
</h:form>
The backing bean has
public void debugBreakpoint() {
int i = 0;
i++;
}
Unfortunately, this does help. It hits my breakpoint only after the second press. I suspect that some field somewhere isn't passing validation but I would like some method of detecting what exactly is going wrong - why do I need the second push? Is there some option I can turn on in Glassfish, or something else where I can look at a dump of debug information? I can ignore the dump until everything is stable and then see what exactly is happening when I press the button for the first time.
Is there any such tool which I can use?
That can happen when a parent component of the given <h:form> has been rendered/updated by another command button/link with <f:ajax>. The given form will then lose its view state which it would only get back after submitting the form for the first time. Any subsequent submits will then work the usual way. This is caused by a bug in JSF JS API as descibred in JSF issue 790 which is fixed in the upcoming JSF 2.2.
You need to fix the another command button/link with <f:ajax> to explicitly include the client ID of the given <h:form> in the render.
<f:ajax render=":somePanel :formP" />
Another way is to replace this <f:ajax> by a PrimeFaces <p:commandLink> or <p:commandButton> so that you don't need to explicitly include the client ID of all the forms. PrimeFaces's own JS API has namely already incorporated this fix.
add event="onclick" in your p:commandbutton
I guess that will sort it out.
or you can add this ajax="false" property in your commandButton
<p:commandButton ajax="false" action="#{userController.create}" value="#{bundle.CreateUserSaveLink}"></p:commandButton>
I ran into the same issue. The solution was simple, instead of having both an actionListener and an action, just convert the actionListener method to return a string to where you want to navigate to and use it as the method for the action (and don't have an actionListener).
In simple terms: only use an action (do not use an actionListener on a commandButton that is submitting a form).
Please check your binding with bean.
bean fields should be String or non primitive.

JSF Required=Yes not working inside a datatable?

I searched everywhere but could not find a solution to this. I am trying to used
required=yes to validate whether a value is present or not. I am using it inside inputtext.
The problem is it does not work inside a datatable. If I put the text box outside the datatable it works. I am using JSF 1.7 so I don't have the validateRequired tag from JSF 2.0.
I even used a validator class but it is still not working. Does anyone know why does required=yes or validator='validationClass' inside a inputtext inside a datatable is not working.
I appreciate the help.
Thanks.
First of all, the proper attribute values of the required attribute are the boolean values true or false, not a string value of Yes. It's an attribute which accepts a boolean expression.
The following are proper usage examples:
<h:inputText required="true" />
<h:inputText required="#{bean.booleanValue}" />
<h:inputText required="#{bean.stringValue == 'Yes'}" />
As to the problem that it doesn't work inside a <h:dataTable>, that can happen when the datamodel is not been preserved properly (the datamodel is whatever the table retrieves in its value attribute). That can in turn happen when the managed bean is request scoped and doesn't prepare the datamodel during its (post)construction which causes that the datamodel is null or empty while JSF is about to gather, convert and validate the submitted values.
You need to ensure that the datamodel is exactly the same during the apply request values phase of the form submit request as it was during the render response phase of the initial request to display the form with the table. An easy quick test is to put the bean in the session scope. If that fixes the problem, then you definitely need to rewrite the datamodel preserving logic. You could also use Tomahawk's <t:saveState> or <t:dataTable preserveDataModel="true"> to store the datamodel in the view scope (like as JSF2's new view scope is doing).
Finally, JSF 1.7 doesn't exist. Perhaps you mean JSF 1.2?

Resources