jquery and java communication - java

I have jquery pop form to upload a file, after on submit (the page refresh and the pop close) i check something about the file and then if there's something wrong i need to pop up that form again (from the java code?), how could i do that ?

You should use ( or must be using) ajax in jquery with a popup.
When the user hits "submit", control goes to server side code.
The code runs to upload the file.
Whatever the result of upload (success/failure), that message is sent to the popup with ajax automatically.
In case, there is problem in uploading the file then, along with the failure message, you can send in the div which contains your form.
I think rather than refreshing the page to close the popup, allow the user to close the popup with close button.

When "something is wrong" the server-side code (this applies to any language) should include within the HTML content Javascript that will trigger the "form" to be displayed again.

As I feel dizzy presently,can't write the code,but will try to break whole procedure in multiple steps:-
On Trigger(by some event) a pop up form will open from submission,which will have a button which will be calling a OnClick Event,which will be containing an Ajax call for client server communication
till the response don't close or fade out the Pop up box.
from server expect two tags SUCCESS or ERROR
a) On SUCCESS, remove form DIV and fade out/close the pop up
with a success message b)On Error,display a refreshed form DIV
And so on

Related

Javascript not executing on JSP forward action

Hi I have one jsp page and I have to update two html select boxes with the data of an inf file. On selecting the first combo(initially also not loading the second combo), the second will have to change accordingly. Since java script variable is not accessible from java, I had sent the value of first combo to an another jsp by ajax. And then return it to first jsp itself.Plaese help me.
Maybe I'm getting it wrong but looking at your code it seems that you are doing AJAX request on selectbox change event, but you are not doing anything with the response.
So basically what happens is you are doing an initial render to your jsp and everything is fine. Now you are changing selectbox option and AJAX request is sent to dummy.jsp. dummy.jsp is receiving it and redirecting the maker of the request to User.jsp. User.jsp-s content is sent back but nothing is done on the client side AJAX call with this response.
What you should do is have your logic at dummy.jsp(or even better at some servlet) which based on your AJAX call request parameters returns for example a JSON with data that you can process in jQuery.ajax success event. In there you can use this JSON to readjust your selectboxes and other data with javascript.
Check the examples at the end of jQuery.ajax manual: http://api.jquery.com/jquery.ajax/

Android, YUI, Moodle

The problem I'm having is the following:
I have an app with two separate modes: A WebView for browsing and a custom Canvas. The custom Canvas captures handwriting samples for language placement exams. Here's how it works. A user logs in to Moodle via the WebView. After they log in, they navigate to a Quiz inside Moodle. They click a link on one of the Quiz's questions and this launches an Intent which hides the WebView and shows the Canvas. The user then writes (using a stylus) on the Canvas. When a user is finished writing their essay (or whatever), they press a button that uploads an image file to Moodle. I am able to upload images to a point, it's getting them to show up in the HTML page that the user clicked the link in originally (see above) and to get Moodle to commit them to permanent storage that is the problem. Normally this is all accomplished through AJAX (really AJAJ since it's JavaScript and JSON) and when the user drops a file on this one component, the component refreshes and uploads the file.
Here is the problem: I need the WebView so that students can log in to Moodle through Shibboleth. But because the underlying JavaScript in the browser makes AJAX calls to the Moodle server and since the Java side of Android doesn't have access to the DOM, I have use the Apache HTTP components library to make some of the connections below basically to preserve the state of the HTML page in WebView.
In a desktop browser on, say, Windows, I use WebScarab to monitor the browser's requests and this is what I see: the browser uploads a file to Moodle via five successive calls to the following scripts:
POST https://[moodle website]/repository/repository_ajax.php [posts multipart form data]
POST https://[moodle website]/repository/draftfiles_ajax.php [posts some params]
GET https://[moodle website]/draftfile.php/[some_id]/user/draft/[some_id]/[somefilename.png] [returns an icon of the image for a filepicker from YUI]
POST https://[moodle website]/mod/quiz/processattempt.php [returns HTML page]
GET https://[moodle website]/mod/quiz/summary.php [returns HTML page]
Some of these scripts return, as you'd expect, JSON data since they're AJAX and not HTML. The final two calls (4 & 5) return HTML. Now, I can make all of those calls in succession in either the WebView or the Apache HTTP library, but if I do so with WebView, only JSON data is returned to the WebView in calls 1-3 (WebView treats the JSON data as a page and displays it wiping out whatever HTML page was displayed in it). If I capture and process the JSON data using the Apache HTTP library in Java, then the JavaScript components internal to the page do not get updated. If I split the calls so that I send only calls 4 & 5 to the WebView, the HTML merely returns WebView to the first question of the exam and Moodle acts as if I haven't uploaded anything.
I can verify that files are uploading if I manually refresh (press a link) the JavaScript UI elements in the page. I can't expect students to do this, though, because the link to do so is very tiny and it's not obvious that it does a refresh. I need a way to programmatically refresh this one element (it's part of YUI) or to get Android and the Java side to play more nicely with the JavaScript/DOM side.
My question is: does anyone know a way to 1) fire off a drag and drop event using YUI to an element inside an HTML page or 2) a way to consume the JSON data and pass it to an element inside the HTML page.
I'm banging my head against a wall trying to figure this out.
OK, so I figured out that: javascript:document.getElementsByClassName(\"[name of link here]\")[0].click() works in Chrome on the desktop but doesn't work if I pass it to WebView.loadURL(). I just need to be able to simulate that click event reliably in WebView. It appears not to support click(). Anyone have any ideas?
The winning code is:
el = document.getElementsByClassName("[some element]")[0];
var event = document.createEvent("MouseEvents");
event.initEvent("click", true, true);
el.dispatchEvent(event);
This selects the link at [some element] and thereby fires an AJAX request that refreshes the FilePicker. For those working with Moodle, I had to add the above code to the same quiz question that handles so it is invoked by putting that code in its own function and calling it with WebView.loadURL("javascript:myRefreshFunction()").

Reading website content via MATLAB

I need to read the content of a website via MATLAB but the problem is when I load the website as:
URL = java.net.URL(UrlNew);
urlConnection = URL.openConnection();
urlConnection.connect();
str = regexprep(fileread(filename),'\s*',' ');
The website redirects me to a page which I have first to press "I agree" button and then it goes to the requested page.
How can I handle this situation in MATLAB. I need to somehow click the "Agree" button in MATLAB to have access to the original webpage.
Thx
First you need to find out the value of the action attribute of the form on your original page. You can do this by examining the source code of the page, looking for the form tag containing the Agree button, or by manually clicking on the button and looking at the Network activity tab in the Chrome Developer Tools or Firebug in Firefox.
Once you find this, find out how to send POST requests in Java and simulate the clicking of the button by sending a POST request to the action URL of the form. If you collect the output from the socket after that, it should correspond to the page you want.

Spring MVC redirect: new page is opened in div

Here is a problem I have encountered.
I have jsp page which has a javascript openForm function. This function fetches data from server URL that relates to dialog.jsp and inserts that code into the body of my page. I.e. it looks like
function showDialog(url){
$.get(url,function(data){
$("body").append(data);
})
}
OK, but after user deals with that dialog I want to redirect him to some better place. I use return createRedirectModelAndView(URL_CONSTANT) in the controller, responsible for handling POST from that dialog.
It works, but here is a problem: a new page (redirect target) is opened INSIDE that dialog div. So the parent page (on which user clicks showDialog) still exists and it looks like the redirect is redirecting user inside dialog. This effect can be cool in some situations, but not in mine. I want total redirect - close that page and open another.
Where I went wrong?
If you want to redirect from an Ajax call then you should return the redirect URL from the server and do the redirect/location change from JavaScript.

How can I direct a user to a new page dynamically based on a dropdown list with plain Wicket?

Here is my situation: the user selects a section (for example from a dropdown) such as "Section1," "Section2" or "Section3." Then he clicks the OK button (or some link).
What I need to happen: after he clicks on that button/link, he will be redirected to the selected section, e.g. www.homepage.com/docs#section2.
So far, I have not been able to process the form from Link's onClick method, nor have I been able to call some clickLink on Link from the Button method onSubmit().
I would prefer not to use AJAX or JavaScript. How can I do this?
That's because a Link doesn't submit the form. It just acts as a link to somewhere. To access your formdata you'll need to submit the form first. Try using a SubmitLink instead of a Link and call
getRequestCycle().setRequestTarget
(new RedirectRequestTarget("www.homepage.com/docs#section2"));
from the onSubmit function of the SubmitLink.
Judging from the Javadoc this should work but I can't test it right now.
A RequestTarget that will send a redirect url to the browser. Use this if you
want to direct the browser to some external URL, like Google etc, immediately.
Or if you want to redirect to a Wicket page. If you want to redirect with a
delay the RedirectPage will do a meta tag redirect with a delay.
Did you try Link.setAnchor(Component)?

Categories