I am currently working on a web app using Wicket and started using jQuery core (UI also) in it today. I ran into an issue I troubleshooted as a jQuery function (show(), slideDown()) colliding with some of the javascript Wicket auto-generated for an external form link.
Before I get deeper into this project I am wondering if anyone has had similar experiences. Are there a lot of places where the jQuery effects/actions collide with Wicket generated javascript/HTML? Are there clean ways to avoid this, or mitigate it?
I have seen Wicketext as possibly a cleaner "integrator" of the two, but mostly it seems to just be a way of doing more of the jQuery-ish code in the Java instead of JS.
This is an interesting problem since jQuery occupies it's own namespace. I'm guessing that there is a conflict with the $ jQuery variable. To get around this you can have jQuery occupy a different namespace like so:
var $j = jQuery.noConflict();
You can do this after you've loaded both sets of code and the $ will be returned to its original value. A bit of a headache but it can fix things in a pinch. See using jQuery with other libraries for more info.
Related
I have some java class files that I would like to run from javascript. They take in a string and spit out a string. I am trying to figure out the best/easiest way to execute them the class files from javascript so I can get/use its response. So as far as I know now here are my options.
Javascript ajax request to php then maybe exec() the jar
Javascript ajax request to my class files re implemented as servlet
Rewriting the class files as javascript and call it a day
1 is not ideal because I don't avoid using php, if I can. 2 sucks cause I don't want to run Apache Tomcat. 3 sucks naturally.
Long story short, I wrote a beautiful implementation of A* in java and I want to use it in a game I wrote in js without rewriting it. What would you do?
It isn't possible to run Java through Javascript completely independently without using HTML. However, it isn't impossible.
You will first need to convert your Java game (.JRE file) into an applet. To do that, you might take the public static main void and replace it with an Applet initialization (how to use applets). It might seem tedious, but when you actually do it, it won't be that difficult.
Then, using Javascript's HTML Document Object Model (learn more), you can easily do something like this:
document.write('<embed src="foo.class">');
Also, keep in mind that the <applet> tag is NOT supported in HTML5. Like shown above, you can use the <embed> tag or also the <object> tag.
Sincerely, PD.
I have 3 questions:
Each and every action in Vaadin makes a call to the server. is there a way to avoid calls to server for every actions? like having a code at client side for particular actions that is used many times? Like in CSValidation add-on.
I want to know how to add Javascript/JQuery in Vaadin 7. It seems easy in Vaadin 6. But, I couldn't get it working in Vaadin 7. I hope they would have made it more easy now. Can anyone show me some examples regarding this. If it is JQuery, It will help me a lot.
And also will
Javascript.getCurrent().execute("");
'execute the javascript' or 'add specified script' in to the code. Will this help me to solve my 2nd question?
1) Each and every action in Vaadin makes a call to the server. Is there a way to avoid calls to server for every actions? like having a code at client side for particular actions that is used many times? Like in CSValidation add-on.
This depends on the client-side code. Vaadin is built with a server side programming model, but if you need to restrict the amount of server calls, you need to do it yourself. Vaadin 7 made it relatively easier to include third party libraries as it was in Vaadin 6.
2) I want to know how to add Javascript/JQuery in Vaadin 7. It seems easy
in Vaadin 6. But, I couldn't get it working in Vaadin 7. I hope they
would have made it more easy now. Can anyone show me some examples
regarding this. If it is JQuery, It will help me a lot.
Here you have a nice tutorial on how to integrate jQuery with Vaadin 7: http://java.dzone.com/articles/integrating-html-and-0
It basically goes about creating a JavascriptExtension class, this is the main part of the solution:
#JavaScript({ "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js" })
public class JavascriptJQueryExtension extends AbstractJavaScriptExtension {
... // Please see the link above for an example of implementation
}
The path can either be an URL or an internal path to the jQuery library.
3) 'execute the javascript' or 'add specified script' in to the code.
The following code snippet will be executed, as stated in the Book of Vaadin 7 (https://vaadin.com/book/vaadin7/-/page/advanced.javascript.html)
// Shorthand
JavaScript.getCurrent().execute("alert('Hello')");
The JavaScript is executed after the server request that is currently
processed returns. (...)
I would suggest for you to take a good look at the Book of Vaadin. It contains a lot of important information that is usually helpful to solve most of the problems that arise when working with Vaadin.
I am not expert of Vaadin Framework...
I can tell you that your Question No.3 is to run JavaScript commands through that..
You can also run jQuery command through that..
But for that you must have jQuery included in the page..
for Question 1: I can say it is possible, as Vaadin have the functionality that overrides function..
JavaScript.getCurrent().addFunction("com.example.foo.myfunc",
new JavaScriptFunction() {
#Override
public void call(JSONArray arguments) throws JSONException {
Notification.show("Received call");
}
});
Link link = new Link("Send Message", new ExternalResource(
"javascript:com.example.foo.myfunc()"));
Now in absence of supporting code, you must identify the actual plugin's function that is making call to server on each action.
Make sure if you override the function.. you will require that functionality at some point.. so do not override the actually required function....
Question 2,
yes the jQuery is available with vaadin, refer forum
it says you can call jQuery directly like this $wnd.JQuery
I hope this will help...
I already have a simple application, to experiment I am implementing the same in Struts 2. In an old app I've used jQuery, now can I use directly the same jquery.js here or shall I opt for Struts 2 jQuery Plugin?
I tried with jquery.js, I am getting null pointer exception. So I am confused now. Some one tell the difference between two. Both are jQuery only wright?
Try setting loadAtOnce in <sj:header> tag to true to disable on demand script loading. So the jQuery script will be loaded at page load. You should be able to use your own library scripts without manually loading your own copy of jquery.js.
Ref. Here
If you have got NullPointerException, it doesn't mean that JQuery or plugin has bugs. You are getting errors because you are doing something wrong in the code, misunderstanding or something.
Struts 2 JQuery is a tag library that you can use with Struts 2 framework. It contains the JQuery inside it. JQuery is only a JavaScript library. Using the plugin you have Ajax support with Struts 2 framework.
I am migrating my java web-application project from JSP to thymeleaf.
I want temporarily to have ability to reuse some simple custom JSP tags in thymeleaf pages. It seems not a problem with custom tags defined in old fashion, as java files. I just instantiate tag, set fake PageContext, request and response, attribtes - and call doStartTag / doEndTag.
However I could not find a way how to instantiate object representing JSP 2.0 tag (i.e. defined in a ".tag" file. How can I achieve this?
Thanks in advance for any ideas!
It looks there are two ways:
Use jspc-maven-plugin to get precompiled classes from jsp and tag files - I checked that works - however latest version of this plugin is 2.0-alpha-3 and it is 4 years old now.
Access servlet-container jsp compiler (in most cases jasper) and do the same thing as in first variant, but on demand, in execution time. However, this makes application container-dependent.
(My personal opinion now is that it would better to avoid the idea at whole - all solutions looks too unreliable to use in production)
An existing Java site is designed to run under "/" on tomcat and there are many specific references to fixed absolute paths like "/dir/dir/page".
Want to migrate this to Java EE packaging, where the site will need to run under a context-root e.g. "/dir/dir/page" becomes "/my-context-root/dir/dir/page"
Now, the context-root can be easily with ServletRequest.getContextPath(), but that still means a lot of code changes to migrate a large code base. Most of these references are in literal HTML.
I've experimented with using servlet filters to do rewrites on the oubound HTML, and that seems to work fine. But it does introduce some overhead, and I wouldn't see it as a permanent solution. (see EnforceContextRootFilter-1.0-src.zip for the servlet filter approach).
Are there any better approaches to solving this problem? Anything obvious I'm missing? All comments appreciated!
Check out a related question
Also consider URLRewriteFilter
Another thing (I keep editing this darn post). If you're using JSP (versus static HTML or something else) you could also create a Tag File to replace the common html tags with links (notably a, img, form). So link can become <t:a href="/root/path">link</t:a>. Then the tag can do the translation for you.
This change can be easily done "en masse", using something like sed.
sed -e 's/<a/<t:a/g' -e 's/<\/a>/<\/t:a>/g' old/x.jsp > new/x.jsp
Form actions may be a bit trickier than sed, but you get the idea.
the apache world used Redirects(mod_rewrite) to do the same.
The Servlet world started using filters
The ruby world (or the RoR) does more of the same stuff and they call it routing.
So, there's no getting around it (Unless you want to use smart regex through out -- which has been tried and it works just fine).