Is there a way to localize messages in Java PlayFramework 2.2.0 in AJAX calls ?
Now I am make calls and hardcode messages (#Mesages("message-id")(lang)) by checking of the languages used per a page, but I am pretty sure there is a better way to do it.
Thanks in advance,
Simeon
The thing you are trying to do seems to be impossible, as your code is trying to get the data from Message file in configs which is done at the time of rendering that view. So scala is compiled on server side and you are trying to make the request for particular message in AJAX which is javascript code and client side execution.
Alternate solution for this is create an Post api for it which takes message-id as parameter and returns data from message file in configs through ajax call.
Related
Background: I am creating an Android app that will get data from a web page (that will be created by me) and parse it. This data can be in XML or JSON format. I would personally prefer JSON but if there is an easy way to achieve it with XML, I would most certainly use it. The app is a Live Score app. So, my page would have XML or JSON with the current score, the current time and minute by minute commentary.
Progress so far: My app is able to read and parse JSON data using the HttpGet and HttpResponse methods alongwith the JSONObject. My concern however is that I am unable to create a dynamic web page. I tried to create and host a page using Google App Engine and Python. The problem was that everytime I'd make changes to the JSON file, I would need to re-deploy the program from the Google App Engine to ensure that the changes are reflected on the URL. I felt this was relatively time consuming.
My Questions:
1.) Is there any way to host a dynamic web page? Preferably with Eclipse and Java. One that I can add the commentary to, as and when an event occurs and it is reflected on the page.
2.) I am not good with Web Services but would GET and POST be a better alternative as opposed to deploying my JSON file with the GAE? If so, how can I go about with that?
3.) Is GCM a good alternative for all this? I tried reading up some of the documentation but it seemed really complex to me.
Thanks.
Sure, it's called AJAX. It requires client-side (browser) javascript code making a request and server-side java code making a response. See: How to use Servlets and Ajax?
Yes, you should use POST and GET to create/retrieve data on server. Your server-side java code should receive data on POST, save it to datastore, and then serve it back on GET. I'd suggest using objectify library for storing data, and GSON for converting java objects to JSON.
GCM is an asynchronous way to notify Android apps (aka notifications) when apps are not running. While with GCM it's possible to send data both ways, it's more complex then plain POST/GET and also only works on Android devices with Google Play.
I have a Java servlet from which I need to send few notifications to the HTML page before I send the final response (which is a PDF file).
Basically I need to keep the user updating on steps I am performing while generating the PDF for them.
I guess, one way could be set various attributes using HttpSession and have them printed using JS. But I don't know how to do that dynamically without loading the page again.
Any idea on how can this be done? I am kind of new to JSP and servlets.
There are two ways:
AJAX polling - you send ajax requests periodically, and the server responds. The page does not get refreshed. Check jQuery for an easy way to make ajax calls
Comet - that's an umbrella term for what you are trying to do. Usually it means keeping an open channel with the server. There are many ways to implement comet (including the option above), but in Java, on the server side, there are two things to check:
Servlet 3.0 asynchronous support
Atmoshphere - chooses the best underlying mechanism provided by the container, which includes the async support above
You should probably look forward to use some implementation of COMET (with js library of choice).
If your html code is allowed to use html5 features, you could also use websockets
I am making a automation website to send multiple files to an another site to prevent filling form every time to send a file.
I want to to make the POST request from server, because AJAX doesn't allow request to other domains.
How I can make this?
I am using Spring MVC3
Use apache http components - it allows you to perform http requests. You can also use (without 3rd party libraries) new URL(..).openConnection(), but it's less pleasant to code with it.
You can use Apache HTTP Components to implement pretty much any HTTP calls you want in your application. Also, note that it is possible to do cross domain AJAX with certain helper technologies like Flash ... if you really needed too.
What are the various options to call a "specific" Java method from Javascript and have callback, etc?
E.g. One i know is using DWR..But wanted to know if there are any other libraries that can be used OR if this can be done by AJAX itself..
What is the recommended approach?
Put simply, with the possible exception of a Java applet running in the same page context as the JavaScript, there is no way for JavaScript to directly call a Java method, or vice-versa. DWR isn't calling any Java methods directly from JavaScript, it's sending an AJAX request to the server and using its own intermediate layer to instrument the request and response processing such that it looks as if it is calling Java methods.
But anyways, the typical approach for getting JavaScript code in the client and Java code on the server to talk to one another is to define an API on the server that is exposed to the client. The client can then make requests (using AJAX or whatever other mechanism you prefer) using the exposed API. The client isn't calling any Java methods directly, instead it's sending a request to a given URL on the server, which gets mapped back to some bit of Java code. Most popular JavaScript frameworks provide convenience methods for simplifying AJAX request handling and callbacks.
If for some reason you want functionality equivalent to being able to call an arbitrary method in an arbitrary class and have the result returned to you, then you could define an API method that takes as parameters a class name and a method name and then uses reflection to perform the method invocation. This would probably be very close to what the server-side portion of DWR is doing.
I know you can do this when using Mozilla Rhino, but I'm guessing that you're talking about a browser run-time environment, so maybe this response isn't very helpful.
I am building a REST based API (Read only) for accessing services of my application. I plan on writing a web application that will leverage these APIs to provide basic information on the status of the application. I plan to use AJAX (using jQuery) to show the information.
Originally I planned on using Grails, Spring MVC, RoR or one of the web frameworks to handle the back end of my application. The REST APIs I will be providing though are already built on a stanalone REST framework so I would only be leveraging the web framework for the core application and not the business logic. In the future, I might need the server side framework to handle other tasks but for now most of the work is done in the REST APIs.
My question is, should I bother with using a web application framework on the server side? I should be able to make all the API calls I need from AJAX directly from the browser. I cannot think of much I would need to do on the server side. Would it make sense to have the application be standard HTML + AJAX + REST?
Its hard to say without actually knowing more about your current setup. This is what your situation sounds like:
You already have an app ready to go with all of the business logic contained. Sounds like its written in Java?
You already have the services written and exposed through a REST api using some other standalone framework. Meaning that if you wanted to, you could access the data right now with the browser without any extra work.
You have not yet built the web application, but when you do, it will get all of its content from the REST api using XHR and jquery. I say that, because otherwise, I would think that you would already be using some kind of framework to generate the other content.
If I am correct in my assumptions, then I would say that you have no need for an additional framework layer. Grails, RoR, SpringMVC my use ajax, and aid in exposing REST services, but the bulk of what they provide is an easy way to make an application that must generate html on the server, deal with form submissions, and handle sessions in a request/response cycle. It doesn't really sound like you'll be doing any of that, and it will likely make your app more complicated.
If you did at some point need the things that rails etc. provides, I would say that you may not need to use rails to expose the rest apis you have now. You could use rails just for what you need, and continue to use what you have for the REST api.
Well, the AJAX calls need to pull data from a server somewhere. If the goal is to avoid a complicated setup on the server side, CherryPy can keep the server side code VERY small.
I've written a simple exapmle below. The first class would be where you put the logic for your ReST API. The code below the class is all you need to get the server up and running.
Install Python 2.6, save the code below to restExample.py. Then in your command line run the python file by doing "python restExample.py". Point your browser to http://localhost:8080/blog/999 and see some JSON come back.
import cherrypy
import json
# Create the controller
class Blog_Controller(object):
def get(self, entryID):
cherrypy.response.headers['Content-Type'] = 'application/json'
return json.dumps({'title':'Entry Title from DB', 'entry_text': 'Text From DB'})
def update(self, entryID, titleFromPOSTFormInput, textFromPOSTFormInput):
# Update DB with passed in arguments. entryID comes from URL,
# other two entries come from POST
cherrypy.response.headers['Content-Type'] = 'application/json'
return json.dumps({'success':True})
# Setup URL routes
d = cherrypy.dispatch.RoutesDispatcher()
d.connect(name='blog_entry', route='blog/:entryID', action='get',
controller=Blog_Controller(),
conditions={'method': ['GET']})
d.connect(name='blog_entry', route='blog/update/:entryID', action='update',
controller=Blog_Controller(),
conditions={'method': ['POST']})
config = { '/' : { 'request.dispatch': d } }
cherrypy.tree.mount(root=None, config=config)
# Start the webserver
engine = cherrypy.engine
try:
engine.start()
except:
sys.exit(1)
else:
engine.block()
It sounds like this might be a good use case for GWT with Restlet.
Forgive me for tooting my own horn, but if you are doing AJAX REST stuff with jQuery, you should probably check out my JSON-REST plugin:
http://plugins.jquery.com/project/rest
If you are getting XML back, then this won't be as useful, but you may still be able to adapt some of the code to your needs.