I have a method residing at backend of GWT framework , I usually call this method via RPC.
I am in a situation where the application redirects to a separate JSP page , Now from this jsp page i like to call the same method which resides at the backend.
Is there a possibility i can call that method from my jsp page via RPC or some other means .
There are 2 approaches I'd suggest:
Load the app's JS file (in your JSP), and have a JS function "exported" from it, which knows how to make RPC calls. This function would thus work as a passthrough JSP->GWT->Backend
Expose your GWT backend through an easier protocol, one that can be used easily from JSP (such as a REST API, or even a simple ajax call), and avoid RPC alltogether.
Other than this, please be aware that you can't easily decode and/or compose a RPC message, so I don't think you can do it yourself.
Related
How can we know the name of the page or service or state of page which has called RESTful API in java file. Something like $state.current.name in AngularJS.
I have created api's in java using #RequestMapping(). In this java file I want to know about the name of page that has called this API. As I want to call same api on different pages and want to log different strings according to the page which has requested the API. I am using $http.get(api) in AngularJS to call API.
We found the most reliable way to achieve knowledge of the current URL/state is to send it as part of the request payload. This gets around issues of HTTP referer header stripping. You already have $state.current.name, so adding this to your API-calling JS service function should be straightforward.
If you need to add this to every request, one option is to use an interceptor to add the metadata into every HTTP request which contains JSON.
I have web-service exposed at my side and I'm able to get it called when some one hit the URL like
curl http://localhost:8080/api/jsonws/XXXXX-Portlets.foo/verify-service \
-u test#liferay.com:test \
-d action=''
Here verifyService(String action) is my method in class now when some one hit this service with curl I'm just taking the example of curl.
You can also call this service like below as well,
http://localhost:8080/api/jsonws/XXXXX-Portlets.foo/verify-service/action/{valueOfparameter}
Now this service hit by third party client and what I want to achieve is when some one call this web service I want to update my jsp with that parameter or want to call my controller.
I am using MVC pattern so its like I have to call my controller class from the my web-service class's method (verifyService).
If "Controller" means "Portlet" in your case (the Liferay context suggests this) I'd say that your architecture has it wrong. Move the code that you need to call to a service and it'll be natural to call it. Have the portlet as well as the service call the same service with your reusable code.
Consider the portlet world the UI for an application. You wouldn't call UI layer code from your business (or service) layer - whose UI would you call, especially on a webserver where there are potentially huge numbers of concurrent users.
To answer your first comment on this post, I'm not sure what you mean with "CMS endpoint". Liferay's API is available, for historical reasons the CMS interfaces' names start with "Journal", so you can actually create or read articles from your own services by delegating to JournalArticleService etc. This gives you access to the content side, there are also a lot of different APIs for changing/creating pages, adding content to the pages etc. - no need to go through a portlet. All of Liferay's functionality is available through an API. How much you need and which one is probably too much to answer in a single stackoverflow answer - check https://dev.liferay.com for some chapters on accessing Liferay's API. Also, a good starting point is this blog article (series)
I want to call values from Java Managed beans using AJAX calls directly not using JSF capabilities. It means using simple AJAX or JQuery AJAX retrieve a value stored in a managed bean field, without using JSP write method; I have seen some samples doing this. and I think that's not a good solution. I should say as a test I want create a Web UI using plain HTML, CSS, JavaScript or AJAX and populate the UI with AJAX calls of any type without using any JSF UI components. Is it possible?
thanks,
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 have a database from which I want to expose data.
Ideally I would like to be able to just add a URL into some other web page and that URL would then call the correct datum using the web app I use to interact with the database.
Would a web service be the best option?
Looks to me like a perfect job for ODATA:
The Open Data Protocol (OData) is a Web protocol for querying and updating data that provides a way to unlock your data and free it from silos that exist in applications today. OData does this by applying and building upon Web technologies such as HTTP, Atom Publishing Protocol (AtomPub) and JSON to provide access to information from a variety of applications, services, and stores.
See it action (showing query results in a browser is just one way to use ODATA).
A URL-based solution as you describe would only work if:
a) the web app framework you use can resolve the URL automatically as it parses and sends the HTML to the browser, or
b) the browser resolves the URL (e.g. the IMG element)
If the web app framework you use can resolve the URL (or if you can extend it so that it does), then you still need something that listens at that URL and retrieve the correct element from the database.
The approach here depends on whether you are doing Ajax style web pages or simple HTML, where each UI update refreshes the whole page.
The latter, a traditional page by page web site, it probably the simplest thing. For this explore JSP technologies. The idea is that you write what looks like an HTML page, but embed in it references to Java objects (or even Java code). In this case you should read up on simple frameworks such as Struts. The broad-brish idea is that you get this sequence of processing
Request arrives from Broswer, interpret it to figure out what the user wants to see
Some Java code talks to the Database gets data puts it in a Java Object
A JSP is chosen, that JSP picks items from the Java Object we just prepared
The JSP renders HTML which is sent to the Browser
In the case of Ajax, JavaScript in the Browser decides to display some data and calls a service to get it. So here, yes a "Web Service" of some kind is needed. Usually we use REST services, which return a payload in JSON format, effectively the data is transferred as JavaScript.
There are plenty of libraries for creating RESTful Web Services, for example Apache Wink.