JSF commandButton 'onclick' event not persistent - jsf-2

i want to have a click-able images, which change on roll-over/out/click. needless to say, i want that after the 'onclick' the page won't reload.
i'm trying to accomplish this with either h:image or h:commandButton - both don't work as expected.
the most annoying this is this:
i have in my code-bean the following simple function:
public String getClick()
{
return "alert('abc')";
}
and in the jsp file, the following code:
<td><h:commandButton id="a" type="button" value="click" onclick="#{CodeBean.click}" /></td>
<td><h:commandButton id="b" type="button" image="/resources/empty.jpg" onclick="#{CodeBean.click}" />
upon click, both raise the alert box, however, only the second one reloads the page, while the first one doesn't. don't know what i has to do with it, but when i look at the compiled page's source, i see that the first button's type stays 'button', but the second one is turned into 'image'.
any suggestions?
cheers,
eRez

The default behaviour of the <h:commandButton> (and <h:commandLink>) is to submit the parent POST form which is specified by <h:form>.
The onclick attribute just allows the developer to execute some piece of JavaScript code right before the form is been submitted. It does by default not block the form submit. If you'd like to block the button's default behaviour (submitting the form), then you'd need to add return false; to the onclick.
In case of <h:commandButton>, the component's type attribute defaults to submit and it will generate a HTML <input type="submit">. When you change it to button, then it will be turned in a "dead" button which does basically nothing, expect of executing any JavaScript handlers attached to it, and it will generate a HTML <input type="button">. When using the component's image attribute, then it will be turned in an image map which allows you to submit the X,Y position of the cursor relative to the image (however, in your case you seem to want just a background image; in that case you should actually be using CSS background-image instead) and the component's type attribute will be ignored and it will generate a HTML <input type="image">.
This is all clearly documented in Encode Behavior section of the tag documentation.
If your sole requirement is to block the <h:commandButton type="image"> from submitting the parent form, then you need to add return false; to the onclick, as said before:
<h:commandButton image="/resources/empty.jpg" onclick="#{CodeBean.click}; return false;" />
An alternative is to use type="button" in combination with a CSS background image (so that you aren't abusing the image attribute):
<h:commandButton type="button" styleClass="emptyButton" onclick="#{CodeBean.click}" />
with the following in a CSS file which is loaded by <h:outputStylesheet>:
.emptyButton {
background-image: url(#{resource['empty.jpg']});
}

Related

focus input text inside template-if in Polymer Dart

Let's assume I've got Polymer custom control like:
<template if="{{editMode}}">
<input id="myInput" type="text">
</template>
<div on-doubleclick="{{setEditModeToTrue}}">
some stuff here
</div>
Now I want #myInput to be text focused everytime editMode is true. The problem is that in the setEditModeToTrue handler #myInput is not yet in DOM tree so I can't focus it by:
root.QuerySelector('#myInput').focus();
(And yes I tried setting autofocus attribute on input but it works only for the first double click)
Is there any event or something else letting me know that template-if content is in DOM tree ?
You can wrap or extend the input element and put your code in the attached or ready lifecycle method or use MutationObserver.

SharePoint Designer 2007 - custom submit button committing every content type to default content type

When I submit a new item to a list with 5 content types, everything is being saved as the default content type.
Applications: SharePoint 2007 & SharePoint Designer 2007
I have a list with 5 content types. I have created Custom List Forms for each one to replace the Newform.aspx page.
I have commented out the code for the default "Save" buttons on these forms because I want them to redirect the user to a page other than the default list view upon submittal. I tried using the "Redirect URL" command on the default buttons, but it appears to have no effect on the functionality:
<SharePoint:SaveButton runat="server" ControlMode="New" id="savebutton1" RedirectUrl="http://www.destinationpageforuser.com"/>
Then I did a little internet research and swapped out the default buttons for this chunk of code:
<input type="submit" value="OK" name="btnFormActionSave1" onclick="javascript: {ddwrt:GenFireServerEvent('__commit;__redirect={http://www.destinationpageforuser.com}')}" />
This allows the redirect to happen, but causes every new item to save as the default content type.
Then I tried adding a Form Action Button from the Toolbox in Designer:
<input type="submit" value="Submit" name="Submit1" onclick="javascript: {ddwrt:GenFireServerEvent('__commit;__redirect={http://www.destinationpageforuser.com}')}" />
When I press this, it 1) causes the item to submit as the default content type, 2) clears the NewForm.aspx page of data, and 3) does not redirect but rather sits there on the now empty NewForm.aspx page blinking stupidly at me.
Do I need to pass the content type id somewhere in the button code to ensure the content type is correct? I'm not experienced enough with coding to know where this might go.
So to sum up - I need to both redirect my users to a specific page upon submittal of a new item to the list, and I need the different content type to be respected. Any insight would be appreciated.

p:commandlink inside ui:repeat

In our application we are displaying the menus dynamically. We have the menu object(menuitems in below code) populated with all the menu items (read from an xml). The home page then generates the menus by usinng ui:repeat. Insdie ui:repeat there are p:commandlink.
Below is the code
<h:form id="mainMenu">
<h:panelGroup id="MMPanel" layout="block" styleClass="left_menu">
<ui:repeat var="node" value="#{menuitems.level1menus}">
<p:commandLink immediate="true" styleClass="menu"
action="#{menuitems.onLevel1MenuChange(node.id)}"
update=":pageTitle :menuLevel2 :menuLevel3" title="#{node.name}"
rendered="#{menuitems.selectedLevel1.id!=node.id}" onclick="menuSelect(this)">
<span class="icon icon_#{node.name}"></span>
<span class="text">#{node.name}</span>
</p:commandLink>
<p:commandLink immediate="true" styleClass="menu activelink"
action="#{menuitems.onLevel1MenuChange(node.id)}"
update=":pageTitle :menuLevel2 :menuLevel3" title="#{node.name}"
rendered="#{menuitems.selectedLevel1.id==node.id}" onclick="menuSelect(this)">
<span class="icon icon_#{node.name}"></span>
<span class="text">#{node.name}</span>
</p:commandLink>
</ui:repeat>
</h:panelGroup>
</h:form>
The menuitems bean is at Session level.
There are two diffeent p:commandlink inside ui:repeat. The only difference between the 2 is in the styleclass and rendered attribute. This is done to identify the default menu item when the user logs for the first time and give it an extra css of "activeLink".
The java script called on onclick is given below (in case it is required)
function menuSelect(selectOne){
$(".left_menu>a").removeClass("activelink");
$(selectOne).addClass("activelink");
removeAllChannels();
}
function removeAllChannels(){
$.atmosphere.unsubscribe();
}
The removeAllChannels is to remove primepush autorefresh channels we have.
The issue i am facing is this.
All the links are getting rendered correctly and in Firebug i see all of them have the same html.
But the one which is generated with the extra css "activeLink" (through rendered condition -- menuitems.selectedLevel1.id==node.id) is not working. Nothing happens when i click this default link. All other links work fine.
So when i click on a different link, it takes me to the required page. But when i click back on the menu which was default, nothing happens
I added a Custom phase listener and saw that all the lifecyle stages are called when this link is clicked but the action method is not.
I went through the links this and this but could not figure out the issue.
Please help.
Do tell me if something is not clear
As part of safeguard against tampered/hacked requests, the JSF component's rendered attribute is re-evaluated during processing the form submit. In case of a command link/button, if it evaluates false, then its action won't be queued/invoked. This matches the symptoms you're seeing.
This can in turn happen if the managed bean #{menuitems} is request scoped and/or when the properties behind #{menuitems.level1menus} or #{menuitems.selectedLevel1} are incompatibly changed during the postback request.
Putting the bean in the view scope and ensuring that the getters do not do any business job should fix this problem.
See also:
How to choose the right bean scope?
commandButton/commandLink/ajax action/listener method not invoked or input value not updated - point 5 applies to you

Grails g:Button name coming from params and no form to use

Is there any possibility of having a button which will execute and action in a controller without a form ?. Also, i would like do do something like this, but i see that's not possible:
<g:form action="addFavourite">
<td>
<g:submitButton name="${it.area.name}" value="Add" class="button small blue"/><br><br>
</td>
</g:form>
To name the button with a value that comes from a controller isnt working. Any possible alternative for that? It gives me a null-error-code. And i'm 100% sure the value isnt null..
You can create a button outside a form that executes a controller action when it's clicked using the remoteFunction tag
<button type="button" name="myButton"
onclick="${remoteFunction(action:'bookByName', controller: 'book'
params:'\'bookName=\' + this.value')}">Click Here</button>
It kind of depends. If you want a button to submit to a server via a standard POST then no. HTML doesn't even have a button that works without a form. You can fake this with an image link that looks like a button, but really it just submits via a standard anchor tag. And this would perform a GET, not a POST.
However, if you want to use ajax, you could skip the Grails tags (as I often do) and use the HTML BUTTON element. Could even use the remoteFunction to make the ajax call if you want.
UPDATE: Doh! 2 of the same answers. :)
What does "it" stands here for? I think that is the culprit..
<g:submitButton name="${it.area.name}" value="Add" class="button small blue"/>

submit button behaviour in IE

I have a problem in IE. Hitting enter when the focus is on the last input control sends the focus to the "Next" button. This submits the form. So far, so good.
The code in my base class WizardController looks to see if the Next submit button is null, as follows:
protected string NextButton
{
get
{
return ControllerContext.HttpContext.Request.Params["NextButton"];Nex
}
}
However, despite the form submitting, this property returns null unless the user explicitly clicks on the button with his mouse.
This is blatantly wrong, but I have no idea why it is happening.
EDITED TO SPECIFY THE PRECISE PROBLEM:
The problem only occurs IF there is ONLY one TEXT input control in the HTML form that gets rendered to the browser.
END EDIT
Andrew
I have finally found an explanation for my problem:
It seems to be a bug in IE, whereby if there is a single text input in the rendered HTML form, then IE will not submit the form properly. The issue is described (briefly) at:
Form Submit via Enter Key when using IE
In the above link, no description is given as to why the bug occurs, or since what version of IE, so a blanket solution is better.
The workaround suggested in the article is to add a css hidden text input (with conditionals for IE):
<!--[if IE]>
<input type="text" style="display: none;" disabled="disabled" size="1" />
<![endif]-->
This worked for me, so issue solved.
The following is included to document the issue as I experienced it:
Unlike the problem described in the article, my form did submit. However, when I tried to check which button had been accessed by hitting tab or enter key, no submit button was in the HttpContext.Request.Params collection. So the behaviour I saw was slightly different.
What the above article did identify is that this behaviour is only seen WHEN there is ONLY one text input control. A single check box, for example, does not cause the problem.
I hope that this documents the problem adequately... and that MS will one day correct the bug.
A simple work around might be to use a hidden form element and depend on that rather than the button.
<input type='hidden' name='action' value='next' />
If you have multiple buttons you can always use JavaScript to change the value of the action element just before submitting.

Resources