How to attach a listener like oncomplete to p:button - facebook

I have the following code :
<h:form id="facebookForm">
<p:button value="Facebook Connect" href="#{loginPageCode.facebookUrlAuth}">
<p:ajax event="click" listener="#{loginPageCode.getUserFromSession}" update="growl confirmDialog" ></p:ajax>
</p:button>
<p:confirmDialog id="confirmDialog" message="Welcome"
header="Welcome to our website!" severity="alert" widgetVar="confirmation">
<h:panelGrid columns="1" cellpadding="10">
<h:outputText id="msg" value="Salutation : #{user.salutation}" />
<h:outputText id="msg1" value="Name and surname: #{user.name} #{user.surname}" />
<h:outputText id="msg2" value="Birthday : #{user.birthDate}" />
<h:outputText id="msg3" value="Email : #{user.email}" />
<h:outputText id="msg4" value="Postal code : #{user.postalCode}" />
<h:outputText id="msg5" value="Phone nr : #{user.phoneNumber}" />
</h:panelGrid>
<p:commandButton id="confirmation" value="Ok" onclick="confirmation.hide()" type="button" />
</p:confirmDialog>
<p:growl id="growl" life="3000" showDetail="true" />
</h:form>
I am trying to connect to facebook from my webpage, through a custom servlet which handles all the stuff regarding validation, parsing response, etc. The href inside above form contains the url to facebook authentification, which in the end redirects everything to the current page, which is index.xhtml.
What I want to do is to call a method after everything was done, something like onComplete attribute from <p:commandButton> component, because I want to display a dialog with updated managed bean, <p:confirmDialog>, and to welcome. So I need to first go to the first URL, wait until autenthification is finished, then call this method #{loginPageCode.getUserFromSession} which dinamically updates the managed bean under the hood by parsing JSON from facebook http response, and then to open the dialog which contains updated values.
I have tried also to use <p:commandButton> with type="button" to use action / actionListener / update capabilities, but unsuccessful.
Can you please advise?
Thank you.

You're overthinking/misgrasping how HTTP and ajax in general work. It's not possible to have an ajax oncomplete hook on a synchronous GET request.
Just do the job in the (post)constructor of the request/view scoped associated with index.xhtml which FB authentication is ultimately redirecting to, if necessary based on some specific request parameters which you've specified in index.xhtml callback URL.

Basically, you need to do most of your processing in the back. Try this
Define a preRenderView event in which you'll do most of the postprocessing of the facebook login. This event will be defined in index.xhtml. Your code will look something like this
<f:metadata>
<f:event type="preRenderView" listener="#{loginPageCode.postFBProcessing}" />
</f:metadata>
In postFBProcessing, you'll carry out whatever processing you need to do after return from FB (processing the JSON response, getting user from session etc).
Define a boolean dialogVisible in your backing bean and bind the <p:dialog/>'s visible property to the variable OR use RequestContext to update the dialog.
<p:confirmDialog id="confirmDialog" message="Welcome" visible="#{loginPageCode.isDialogVisible}" header="Welcome to our website!" severity="alert" widgetVar="confirmation">

Related

Spring MVC REST PUT is not working

I have the following problem: Whenever I try to edit the following data, I get an exception saying that HTTP PUT is not supported by this URL. This is the JSP form side of things where a button is used to edit that row:
<c:url var="formAction" value="/circuits/${circuit.circuitId}" />
<form:form method="PUT" action="${formAction}">
<input type="hidden" name="circuitId" value="${circuit.circuitId}" />
<input type="submit" value="Edit" class="btn btn-primary" />
</form:form>
And the following is my controller method that retrieves the circuitId:
#RequestMapping(value = "/{circuitId}", method = RequestMethod.PUT)
public String showEditCircuitForm(#PathVariable Integer circuitId, ModelMap model) throws NoSuchRequestHandlingMethodException
However, when I use the normal GET method everything works fine, it's just this PUT method that is causing the problem. I have got all the dependencies that I need.
Only getand post are officialy supported with forms. I'm not aware of any browser that supports put with forms. It is supported with AJAX, though.
Apart from that the support of the Java Servlet API for PUT is not ideal. You might need to include a filter in your web.xml:
<filter>
<filter-name>httpPutFormContentFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
Before I forget: You can send a post request and use another filter to fake a put request:
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
To get this working you have to include a hidden field with the name _method and the value PUT in your form.

NoBotState is always InvalidBadResponse (AjaxContorlToolKit)

ASPX Code:
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<cc1:ToolkitScriptManager ID="ToolkitScriptManager2" runat="server">
</cc1:ToolkitScriptManager>
<asp:TextBox ID="txtLastNm" runat="server" CssClass="ETextField1"></asp:TextBox>
<asp:ImageButton ID="btnSend" runat="server" ValidationGroup="Send" BorderWidth="0" BackColor="Transparent" ImageUrl="~/images/Buttons/submit.png" Height="34" Width="100" onclick="btnSend_Click" />
<cc1:NoBot ID="NoBot1" runat="server" OnGenerateChallengeAndResponse="NoBot1_GenerateChallengeAndResponse" ResponseMinimumDelaySeconds="3"
CutoffMaximumInstances="5" CutoffWindowSeconds="60" />
It works locally. But when I upload it on server it shows every time InvalidBadResponse
I enter data for login and click on asp button. NoBot state is InvalidBadResponse and it fails. But, then I click on browser's refresh button it asks me to resend request I say ok and now state is valid! Why?
Same thing same problem like : AjaxControlToolkit NoBotState is always InvalidBadResponse
Check out my answer here: AjaxControlToolkit NoBotState is always InvalidBadResponse
Basically, my url re-writing was messing up the ASP.NET ajax framework libraries' urls, causing them not to load - meaning the Javascript challenge would always fail because it wasn't being executed.

generate querystring

I have created one jsp form which contains the username textfield. On the click of submit button it pass on to servlet and read parameter and display on the screen.
I want to generate a querystring of username with the url.
Can anyone tell me how can i do that??
The following example will add the username field in the querystring of the request URL.
<form action="servletURL">
<input type="text" name="username" />
<input type="submit" />
</form>
Note that there's no method. It defaults to GET already, which means that all form data is passed by URL.
If you still don't see the querystring in the request URL, then it means that your servlet is performing a redirect after submit.
response.sendRedirect("result.jsp");
The enduser will then see the redirected URL in browser address bar instead. If you don't include the querystring in the redirect URL, then the enduser will indeed not see it at all.
You should either be doing a forward() instead,
request.getRequestDispatcher("/WEB-INF/result.jsp").forward(request, response);
or append the query string yourself:
response.sendRedirect("result.jsp?" + request.getQueryString());

grails form redirect gives 404, but url mapping works

I feel like I've entered the Twilight Zone. I have a Grails form that redirects to a URL, but gives a 404. If I go directly to that exact URL, everything works fine (that is, the URL mappings are correct).
This is an example generated form tag:
<form action="/alm/contactRefresh/itemRefreshProcess/7070" method="post">
On submit, it redirects to:
http://localhost:8080/alm/contactRefresh/itemRefreshProcess/7070
But gives this error:
HTTP ERROR 404
Problem accessing /alm/contactRefresh/itemRefreshProcess/7070. Reason:
NOT_FOUND
Powered by Jetty://
But then if I just go directly to that same URL (by focusing the browser Location bar and pressing enter), the page renders just fine, though the form params are lost because it's just a GET now. I've also tried changing the form method to GET, and that doesn't work either (throws a 404).
I've done similar forms a zillion times before with no problems. I'm sure this is some stupid user error, but I seriously can't figure out what's wrong.
Thanks for any ideas!
So, I finally started ripping parts out of the form and found out that for some reason you can’t name a Grails checkbox starting with the word "action". It must be something related to the default params["action"] entry. Though my checkbox names were a concatenation of "action_" + an id.
Anyway, there was some kind pre-processing of the checkbox form params that was blowing up before making it to the controller, and somehow that translated to a 404 instead of an actual Exception.
Originally I had this:
<g:checkBox name="action_${serviceRefreshAction.id}" value="${true}" />
Which renders this:
<input type="hidden" name="_action_7196" /><input type="checkbox" name="action_7196" checked="checked" id="action_7196" />
I changed "action" to "myAction", like this:
<g:checkBox name="myAction_${serviceRefreshAction.id}" value="${true}" />
Which renders this:
<input type="hidden" name="_myAction_7206" /><input type="checkbox" name="myAction_7206" checked="checked" id="myAction_7206" />
And now everything works fine.
Five hours of my life down the drain.
But I guess I have to forgive Grails, for the all time it saves me on a daily basis normally. :o)

Sinatra execute code before uploading

I'm having a simple HTML form on my page that looks like this:
<form action="/" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="input" name="pin" />
<input type="submit" value="Upload" />
</form>
Now what I am trying to accomplish (with Sinatra) is to check if the PIN entered into the form field is correct:
post "/" do
if params[:pin] == "1234"
start_upload()
else
print_error_message()
end
end
Of course, I want the PIN to be checked before the file starts uploading. But that's my problem. Immediately after clicking the "Upload" button, the file upload starts until it is finished. Then the script checks to see if the PIN is valid.
Is there a way to do stuff before the file upload starts? And if not, what other ways of doing this are there?
Unless you use some Ajax and split up your request this won't work. You could have two forms, one that holds the pin and that authorizes the user. Once you enter a correct pin you send an asyn request to the server which will then reply with a positive or a negative answer. Depending on the response some javascript will then enable your file upload button so you can start uploading the file. What you should also do is setting a session for the user so that only an authorized user (via the pin) is allowed to send a form. If you check the Sinatra Readme you can find some information on how to do that.
That would be my solution.