How to call java method using Angular JS? - java

I am using Angular JS with Spring MVC for my project. I want to call Java method directly from html using Angular JS to populate Drop Down.
Thanks.

You just can't because Angular JS is working on the client side while Java is most likely running on the server. What you need is a REST interface that accepts invocations from the Angular JS app. As the REST interface is (most likely) written in Java on the server side, you can then make the desired Java call to get the drop down values and send them back as response as part of the REST call. Use Angular JS then to interpret and use the response from the server.
Sorry for not being more precise but that should give you a pretty good starting point.

Related

Integrate Angular + Nodejs + Spring Boot Java REST API to make Angular Universal work

I have a question about how to integrate Angular with NodeJS and Spring Boot.
I have developed an application with Angular as UI and Java/Spring Boot as backend REST API. The problem is that SEOs in a SPA do not work well. For that reason, some developers created Angular Universal to render Angular applications on the server side.
Unfortunately, Angular Universal works only with ASP.NET or NodeJS.
Some attempt was made to make it work with Java as well, (see: https://github.com/swaechter/angularj-universal), but the repo has been archived/abandoned.
If I understand it correctly, the approach would be to run a NodeJS server that is in charge of rendering the Angular UI server side only, while the java service would be in charge of exposing the REST API, but since I have no experience with NodeJS or Express.js, I am not sure if that's the right way to proceed and what implications should I be care of. I couldn't find any guide online.
But my question is, should the code in angular make requests to nodeJS and then nodeJS forward them to the java service, or should angular contact directly the java service to get the data, and only NodeJS to do the SSR?
Thanks
If I understood you correctly, what you are trying to do is called prerendering an Angular application, AFAIK Angular Universal uses NodeJS to do that.
An Angular application is usually compiled to vanilla JS that the browser can understand, which means that in build time you could compile the parts of your application that are static (aka "known at build time"), this is done by NodeJS. This has a problem, if your static parts use browser facilities (e.g. localStorage) or call backend services you have to wrap them with:
if (isBrowser) {
localStorage.set('example', 5);
fetch('example.com/service');
}
In your pipeline you have to build the project and move the generated JS, HTML and CSS to be served directly through your application server or proxy if you have (e.g. nginx).
Here I have an example of a real project that you could take a look that do prerendering with Angular js and Spring Boot. In this project the angular application is served through Spring Boot. https://github.com/afdezcl/kemenu-web

Is there an easy way to log the content of the console to a web page?

Currently, I develop with java / spring-boot / log4j2 , and as much as I have searched I have not seen anything interesting to log directly to a web page in real-time (something like swagger style with requests, which is by configuration without having to write code) .
Do you know anything?
You need Javascript to modify the DOM of a webpage, not Java.
You could use Spring to send Server-Sent-Events (SSE) to a frontend JS library, or host a WebSocket or other REST API on some web server and use AJAX to issue requests, upon which the DOM is modified as part of a response, but this really has nothing to do with Java/Spring/log4j
If you want a packaged distribution of Spring w/ some Javascript framework, JHipster is a popular option.

How do I integrate my Java code to a web application with standard HTML, CSS, Javascript?

sorry for the newbie question so please go easy on me. I am a bit new to Java but have web development experience working with a MERN stack to connect front end to back end.
Could anyone point me in the right direction to a tutorial I could reference to integrate Java into my web app as Google is of little help? Are you able to for example use Java methods inside of Javascript functions for event handlers?
Say I wanted to add a Java object to an arraylist when an onClick event triggers. Would I be able to call the Java method to add inside of the onClick event handler?
No, you are not able to call a Java method from JavaScript directly.
One of the solutions would be to have a Java web server that listens to requests. Then from JavaScript (web client), make requests to the Java server to obtain the responses.
For Java server: simple Web server, simple REST service, Spring framework, Play framework, etc...
For JavaScript client: vanilla AJAX request, JQuery, other JS framworks, ...

use rest api written for angular js in android app

I need to know if I write Rest API in JAVA using Spring framework and I will be using it in angular js front end, is it possible to use the same API's for Andriod app later. Is it a good to use same api's for both web and app, I have no idea about android development, please help.
If your API is RESTful, and by that I mean at least stateless and resource based. Then yeah, it doesn't matter what program you're requesting data from.
If your are able to process the response into your front end (reading the format, etc). It'll be fine. Just make sure your API's endpoints work correctly and are configured to handle the request your programs will be using to communicate with your API (most of the times these are http requests)

I need to interact with web logic server for response using Ajax in angularjs?

I am actually working with webRTC technology .So, in order to maintain my front end effectively i am interested moving to angularjs.
i need to work with web logic server by using ajax and angularjs as front end.
and java is for my back end purpose . If it is possible please suggest me how to?
thank you
Short question, short answer:
Use angular's $http service.

Categories

Resources