Pressing enter to click on hidden button without using JS on a jsf page? - jsf

I have a jsf page where I have several 's. When I press enter I want a particular button to be clicked - and this button is hidden. Is there a way to do this without using JS?
I know if i place the button as the first button on the page, it will be clicked by default - but it does not seem to work.
Here is the sample code. I want the "hiddenButton" to be clicked on Enter.
<h:form>
<h:commandButton id="button1" action="#{bean.action1}" />
<h:commandButton id="button2" action="#{bean.action2}" />
<h:commandButton id="button3" action="#{bean.action3}" />
<h:commandButton id="hiddenButton" action="#{bean.hiddenAction}" style="display:none;"/>
<h:form>

Is there a way to do this without using JS?
Not if you want to support all browsers.
I know if i place the button as the first button on the page, it will be clicked by default - but it does not seem to work.
This works only in Firefox, not in Chrome (Webkit) or IE. If you replace display:none by visibility:hidden, then it'll also work in Chrome, but still not in IE.
So, if you want to support all browsers, JS is your best bet. There may be better solutions depending on the concrete functional requirement, which you didn't tell anything about in the question.

Related

PrimeFaces commandButton not work after submit

I have the following problem I have a CommandButton of primefaces that does well its function which is to save an accounting movement, but when you try to enter a second movement the button does not work to such an extent that it does not warn you if the required fields are empty.
Note that if I give the cancel button that only has a JS to hide and show some labels there is if the button works again.
I need that button to continue working without having to hit the cancel button.
I'm going to leave a partial code that includes the TAG of the form and the buttons warn if you need to also put the fields.
<h:form id="form_agregar_movimiento" prependId="false" >
...
<p:commandButton styleClass="Tamano-Texto-Datos Boton-Afirmativo"
value="Agregar"
action="#{movimientosController.agregarNuevoMovimiento()}"
update=":growl_mensaje_eventos :dataTable_listar_movimientos form_agregar_movimiento"
/>
<p:commandButton styleClass="Tamano-Texto-Boton Boton-Negativo"
value="Cancelar"
onclick="js_ocultar('agregar_movimiento_contenedor'); js_mostrar('movimiento_contenedor_botonAgregarMovimiento');"/>
</h:form>
594/5000
Hi guys, I have solved the problem, I noticed that other forms presented the same problem and testing by ending with #form as recommended and the "prependId = true" and additional add the attribute resetValues="true" and with And the buttons come back to work without problems.
Note: in the example that this function did not work for me but it was for something else, I had a RequestContext.getCurrentInstance().Execute() which calls a JavaScript and it was giving me an error in the browser, I corrected it and works correctly.
Thanks for the tips that have left me

JSF listener triggers buttons onclick

<h:form>
<h:selectOneMenu value="#{productBean.productName}">
<f:selectItem itemValue="" itemLabel="..." />
<f:selectItems value="#{productBean.pizza}" var="pizza" itemValue="#{pizza.name}" itemLabel="#{pizza.name}" />
<f:ajax listener="#{productBean.valueChanged(productBean.productName)}" render="pizzaResult pizzaButton" />
</h:selectOneMenu>
<h:commandButton value ="Dodaj do zamówienia" disabled="#{productBean.isDisabled}" id="pizzaButton" onclick="#{productBean.order}"/>
<h:outputText id="pizzaResult" value="#{productBean.message}" />
</h:form>
This is my JSF form. I used valueChanged listener to make button diabled in ome cases and it works good. But I don't get why it triggers also the buttons onclick. How to do something which enable me to use button ONLY after clicking it?
I noticed that when I delete the disabled option it works good:/ But why I cannot trigger the action when button is enabled in the moment?
onclick is a client side attribute so you shouldn't try to bind it to managed bean method calls and it looks as though you did onclick=#{productBean.order} (apart from the missing quotes). This may be the cause of your problems.
OK i did that. The problem is that the button may appear as enabled (disabled="false") but its client side change and server don't know about it and application thinks that the button is still disabled. Even it looks like enabled button it won't work with action="#{something}".
You have to let server know about the change. THe only thing i did is adding the #ViewScoped to the managed bean. Now the action of disabling and enabling the button is also being seen by the server and works perfectly.
However i have a question. Client side verifaction is a bad idea. Disabled button is the only thing which prevent user from sending empty itemValue or product(in my case) which is unavailable (is_available = 0 in DB). The question is: Is it enough to ensure that it will be safe?
edit: Unfortunately after clicking the button the buttons appear enabled even though the oneSelectMenu is turned to the first, empty value. After changing the list it works again as previously, and after clicking again the situation takes time again.

JSF Form Submit works in FF but not in IE

I have a simple Form with an inputText and a commandButton.
<h:form>
<h:inputText id="xyz" value="#{viewImpl.field}" onfocus="clearText(this)" onblur="setDefaultText(this)" />
<h:commandButton action="#{viewImpl.method}"/>
</h:form>
If I press on the commandButton the form is submitted correctly.
But if I hit enter, still being in the inputText-Field the page reloads, but the method is not called (server is in debug mode with activated breakpoints).
Important: This only happens in IE 8. It works perfectly fine in Firefox.
Any clue on how I can fix this?
Thanks alot!
This is a known problem with one-field forms in IE. Pressing the enter key won't trigger the first-next button of the form and hence its name=value pair won't appear in the request parameter map and hence JSF can't identify the button in order to queue the action event.
One of the ways to fix this is to add a second but invisible input field to the very same form.
<input type="text" name="dummy" style="display: none;" />
This will cause IE to send the name=value pair of the first-next button as well.

JSF jumping to anchor right away

When a JSF commandLink is clicked, the scrollposition of the new page will be 0 - which is fine.
I noticed a strange behaviour with one of my jsf pages - but its acuallty quite hard to explain:
Clicking on a link in order to navigate "somewhere" usually freezes the page and when the new response is received, you'll end up on another page and the scrollposition gets reseted. This is fine.
In that particular case the "current" view is immediately scrolling to the top, once a commandlink is clicked, and THEN forwards to the desired navigation outcome.
Its like clicking on a plain link, that has just its anchor set to the top. <a href='#'>
What could be a possible reason for this? It feels very strange, if the page scrolls up the second you click on a link.
All pages have basically the same usage of commandlinks within datatables. No other page encounters that problems. The Controllers are different - but since the scrolling up is done within milliseconds, I dont think, that this is a server-side issue.
I noticed that jsf appends the # to all links, that are generated.
so, when I click on the link, I can see the url changing to .../page.xhtml# for a split second (the split second, the page scrolls up), and then getting re-jsft like .../page.xhtml?cid=4
edit:
for all my pages, the link looks like this:
SVA
However on that said page the return false; at the end is missing... which then makes the link behave like an anchor...
edit2:
The working-as-expected-links are generated, using <h:commandLink> - the ones that are making the page scroll up, are <p:commandLink>s (Primefaces). Indeed, replacing it, solves the issue, but still wonder what's different for the primefaces link.
edit3: Example
working as expected:
<h:form style="display:inline;">
<h:commandLink
action="#{processManagementController.unpinProcess(process)}">
<h:graphicImage
style="display:inline-block; height:16px; width:16px;"
value="/resources/img/pinned.png"
title="Click to unpin this process" />
</h:commandLink>
</h:form>
causes immediate scrolling:
<h:form style="display:inline;">
<p:commandLink ajax="false"
action="#{processManagementController.unpinProcess(process)}">
<h:graphicImage
style="display:inline-block; height:16px; width:16px;"
value="/resources/img/pinned.png"
title="Click to unpin this process" />
</p:commandLink>
</h:form>
It's not scrolled to top due to href="#", but because of the synchronous postback (which results in a complete reload of the page!). The both examples in your "edit3" still scrolls to top for me.
Nest <f:ajax> in the <h:commandLink>, or set ajax="true" on the <p:commandLink> to make it an asynchronous postback and thus get them to behave as intented.

Pop up Window in spring web flow

i am still new to spring.I need to make a popup window when the button is clicked in spring web flow.
Let say in html file
<h:commandLink value="Sick Leave" action="sickleave" id="sickleave" /></div>
When the action command link is clicked I have to populate the page with some value on-entry itself.
In my flow.xml
<transition on="sickleave" to="sickLeave"></transition>
<view-state id="sickLeave" view="sickLeave.xhtml" popup="true"></view-state>
i have added popup=true.But still i am not able to get a popup window.
I think you need a to use a sf:commandLink component instead of the h:commandLink setting the reRender tag as follows:
<sf:commandLink value="Sick Leave" action="sickleave" reRender="#{flowRenderFragments}" processIds="*" id="sickleave" />
I'm not sure if the "processIds" tag is needed, so try it with and without it.

Resources