Can AngularJS work in Java EE MVC Architecture : JSP->Servlet->EJB3.1->Business Object->DAO - angularjs

I have a quick question on considering AngularJS (current stable version - 1.3.9) for an upcoming application that we are building on an existing framework. The current framework has a Java EE MVC architecture and here are the current components mentioned in sequential order in which they get invoked:
View - JSPs: This layer gets as response Java Objects and we use jsp:useBean to access its properties and display on screen.
If any modifications are done on the page, it goes through a ControllerServlet i.e a Java file which has code to access the HttpRequest and HttpSession related information. The controller also does a lookup in JNDI to find the name of the bean to invoke based on the HttpRequest parameter name, e.g. PageId
Once the EJBBean lookup is returned, the controller invokes an EJB 3.1 "no-interview" view - These are Stateless Beans annotated with #Stateless
EJBBean classes then invoke BusinessObject classes, we call them "BO" which internally gets referenced by the DAO interface
A DAO implementation class is the one which is responsible for CRUD operations
Our Problems as of now:
The view is tightly coupled to Java Objects that are returned from DAOs and since the response is not converted to JSON, a lot of scriptlet code is used to display their value (I know scriplets are oldskool, but being a legacy solution there is no choice)
jQuery is used to manipulate the DOM before sending it to the controller layer
View is not the official record of whats happening on the screen, unlike AngularJS where I could easily understand
Developers write custom CSS for different browsers manually
Proposed Solution
View shall be designed for the new application using AngularJS
Take advantage of Bootstrap css classes which has readily available CSS which can be combined with AngularJS
Each request goes to ControllerServlet using $http service to ensure we use existing MVC architecture i.e routing every request through Controller
EJB Layer to be RESTFul to return data in JSON Format
Viewport specific css code for responsive web design - i.e same screen should render on multiple devices and platforms
Questions:
Is the proposed solution feasible? What are the downsides?
Is it a good practice to reference EJB Bean classes as RESTFul services?
Do we get access to all the Java EE objects / interfaces using AngularJS? For e.g. HttpRequest, HttpSession, etc.
Will it help in performing better by switching to this architecture?
Question from Management - Why not stick with jQuery! - Probably the hardest of all the questions to convince the management of Angular's benefits
Hope you guys can help throw some suggestions

i'm using spring 4.0.1 , hibernate 4.3.5 ,jackson 1.9.2 , I'm creating a RESTful webservice that returns a data in JSON format and angularjs in front-end which is loosely coupled with back-end.
concerning your questions;
the solution is feasible of course , until now there no downsides
except session management is tricky because Restful ws is stateless
.
it's provide a very elegant feature that you can access all your
functionality via any application web-client-side, desktop or
mobile.
angular app is fully isolated from back-end app you can maintain
requests and session in the server and respond with what you like in
json format to angular
same as 2
i hope it's helpful

Related

what does AngularJS has to offer to an ASP.NET MVC project? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I heard a lot about how AngularJS is so much better than jQuery for communicating with server.
But all i found was replacing C# controllers and cshtml views with purely client-side javascript files. And i have no interest in that.
is anything that I'm missing?
I am currently using jQuery Ajax for communication between client and server through JSON. I was looking forward for a better solution for this purpose.
Once a controller returns a view, what is sent to the browser is pure HTML. Razor is rendered server side as HTML. Or your action can return pure JSON.
Therefore, whether you want to use jquery, angularjs, or any other JS/HTML based library, there is really no restriction there imposed by MVC. From the perspective of client side code, controller actions are just URLs that return return HTML or JSON.
Additionally, MVC controller actions which return JSON, or WebAPI controllers, are perfect RESTful endpoints for your AngularJS to interact with. If you need HTML fragments for refreshing dynamic areas of a page, then a controller action returning a partial view will come down to the client as an HTML fragment. So your interactive requests through Angular can still leverage your backend architecture of Controller/Business/Data layers.
What Angular Offers MVC
AngularJS offers an MVC application the same thing it offers any web applicaion. It is a great way to abstract the presentation of UI elements from the interactivity of those elements with the backend. The problem with common approaches with jquery is you react to interaction with specific HTML elements via selectors such as $('.someClass').on(... and then in the handler collect information from the element about what was clicked and submit that in an ajax request, and then take the response and update specific elements $('.areaToUpdate).html(response)`.
So you have three concerns not properly separated:
What element responds to interaction(button click, drop down change, item drag/drop)
The interaction with the server to compile a request, submit it, and parse response.
Updating the UI based on the server's response
Often times because of the way we try to make this code less fragile, you are restricted to a logical hierarchy of objects that also matches your UI's HTML element hierarchy. Long story short, the lack of proper separation of concerns means changing any one will impact a chunk of code that has multiple purposes, and thus changes are more challenging because you have certain restrictions imposed due to lack of decoupling.
AngularJS provides a client side separation of these concerns by using a client side model as the intermediary between these concerns. UI interactions update the model, other code responds to changes to the model and makes server requests which update the model in response, and yet other code can respond to those changes and update the UI in response to the model changing. You are free to modify the UI's structure, and have a much more smaller set of code to update to get the UI changes wired up.
The fact that AngularJS is modeled from an MVC pattern makes it seem as though it would be mutually exclusive with ASP.NET MVC. However, they serve two different purposes. ASP.NET MVC provides separation of concerns for the server side querying of data, population of server side model, and rendering of server side HTML. It also provides a great way to support the rendering of JSON or HTML fragments(partial views) that might be leveraged by a client side interactive framework. AngularJS provides a model appropriate for writing clean client side code for highly interactive pages.
Personally, I think they are very complimentary.
Trying to do highly interactive pages with jquery+ASP.NET MVC is doable and can be efficient, but AngularJS adds a lot to the maintainability of the client side code. ASP.NET MVC provides a good framework for your server side logic. Additionally, if you have simple pages that fit a more traditionally PRG pattern or simply rendering HTML content, then ASP.NET MVC gives you an efficient way to quickly implement these pages without the unnecessary boilerplate code you'd need with Angular. In other words, you can choose to use AngularJS only on the pages that will really benefit from it.
You can mix MVC and angularjs with out completely removing MVC controllers.E.g if you have 5 modules on your app create that parent routes on MVC routing.(E.g Students,Teachers,Attendance etc..)
initially render your first view say student view renders through asp.net MVC then include all your JS files(including states related to that module) on that view(e.g students.cshtml)
.JS files related to this module may be
studentModule.js,studentController.js , studentService.js etc.
Do all your CRUD operations on angular JS.
Here your studentService will interact with MVC controller for all your db operations.
Benefit you are getting with this is
you are interacting with your server asynchronously and you can use
all angularjs features like(Directives,resources etc)
You can authorize all your MVC controller actions with asp.net
identity.
-Also to render any html template on state you can call your MVC controller action from templateUrl.return csHtml from MVC controller
action.
There are lot of applications on live using both asp.net mvc and angular js. here are few things i know.you can check some tutorials for better understanding.
.
Angular and ASP.net are both MVC frameworks. They are in a sense at odds with each other - I work on an Angular / ASP.net project where we serve and initial ASP.net view and then let Angular SPA routing take over, but its kind of a hack and I sometimes which we separated the two and had a plain WebApi implementation with Angular front end.
Using AngularJS just for its Http library is like using a sledge hammer when a hammer will do. You are wasting bandwidth in downloading large amounts of unused JS.
I would look to a pure JS library to handle your Http communication if you are unhappy with JQuerys implementation.
Axios is an amazing example of one of these libraries and uses Javascript promises just like Angular - which make the whole process really neat and easy.
Good luck!

Strongloop AngularJS reflection services

I've a Java developer who's begun working in the Javascript enterprise domain. Specifically I've begun working with Strongloop/Loopback API and AngularJS in the client.
I'm creating entities with ease with Strongloop and am very impressed with how quickly I can generate an expanded model. When it comes to the client however things appear to slow down as I have to manually create the Angular Controllers, Services and the crud template views.
I've used reflection in Java considerably in the past and I found it to be very effective. I was hoping someone might be able to let me know if there's either a reflective way to initialise Controllers so that I could have one EntityController if you like that would export the CRUD methods. Alternatively if there's a tool that might be able to access the restful api and generate generic templates, controllers and services for the restful API?
Thanks in advance for any help!
Mark.
I believe that you are looking for loopback angularjs SDK.
It will not generate views and controllers but you will have angularJS services generated that contains all the LoopBack models and methods you have defined. You have to register the AngularJS module lbServices as a dependency of your app.
Documentation covers this very well with step-by-step instructions how to setup client application.
After you setup loopback angular client, then it is easy to use models in your controllers. All you have to do is to add your model as a dependency in your controller.
If you want to avoid manual work of generating angular controllers, routes, views etc then you should consider using one of the angular application generators ( i.e. yeoman generator-angular ).
To make long story short:
use angular generator to generate angular application.
generate angular services using loopback angularjs SDK.
add loopback model as dependency to your controller or service
See also Angular SDK built-in models API

I need to implement Asp.net Web API 2 and consume it by Sencha Ext JS

I want to implement a web-based API (using ASP.NET Web API 2) and consume it by the client Side library (Sencha Ext JS).
My application should include
A simple user registration form.
A login page for admin.
CRUD operations for users' submissions.
Notes:
I do not want to include any backend code (i.e C#) in the we application, I want to implement it using the HTML/Javascript only, that is Ext JS.
I want the Web API to be RESTful.
I want to protect admin pages.
I want to use the SQL Server to store users' submissions.
All of that requirements should be implemented using the ASP.net Web API 2 and Ext JS only.
So far, I did initial search and I got a lot of learning for either the ASP.net API 2 or the Ext JS. But I couldn't have a guide that help me to fulfill the above requirements or help me to have both technologies work together.
Pleas help me on either way.
Or generally, can you help me get started work in combining both: Asp.net Web API 2 and any client side that consumes it, such as Sencha Ext JS or any other client side. It is not necessarily to be Ext JS.
Thank you so much.
Thanks to StackOverflow.com
If it were me, I'd use the DirectAPI for asp.net https://github.com/elishnevsky/ext-direct-mvc
You create webapi controllers, just like you normally would. The only difference is the the controllers that need to be used by EXT should inherit from DirectController.
If you follow the directions on that page, you'll end up with a globally available proxy object that matches the name of the controller and the public methods hanging off of the controller become methods of that object.
That is, server side controller MyAwesomeController with method DoSomething() becomes MyAwesome.DoSomething.
If you attribute the method as [NamedArguements] you can create methods such as
DoSomething(int id, int foo)
and pass from javascript as DoSomething({id: 20, foo: 30});
Since it is still just a controller, you can attribute permissions and return json as you would in any other situation.
If you get stuck, use the debugger and spend the time to figure out what's really going on. This all works in 4.x and I've tried it in 5.x and it still works there as well. But I wouldn't jump into 5.x just yet as there are still several bugs that need to be worked out by the sencha team before it is ready for prime time.
ExtJs has a REST proxy for the data. So what you try to do should be possible. The proxy can be configured and be finetuned.
I used the JSON proxy. ExtJs has very powerful filter and sort capabilities, both server and client side. In my experience difficulties arose when filtering and sorting server side. There is only sparse documentation on how the parameters are passed and which configurations have what effects.
Since you also develop the REST api, you can adapt to those details. You just have to do some research.
Here is not the place to ask about guides. For Asp I cannot help you, I never touched it. If you use ExtJs, you are free to choose you backend. For ExtJs, the start is pretty straight forward :
get Sencha cmd and generate a skeleton app.
follow the tutorial
create one file per class definition.
the API docs are great. If you still lack something SO is great too.
what you have to find out by yourself is the exact way parameters are passed to the backend and how to format the response.

In my Angular.js app - Should I bind to the model that the Web API provides

I'm building an application with Angular and ASP.Net MVC.
All the data that I use in the application is provided by a Web API (I'm making Ajax calls to my ASP.Net controllers, which in turn call the Web API and return the API's response to the html page).
The models that the web api uses map my needs very well and I don't really have to change anything.
But should I simply bind to the model provided by the Web API? The risk I'm seeing is that if anything changes in the Web API, I might end up changing all over in my application. If I translate the web api model to my own in one place, I will only have to do changes there.
But on the other hand - mapping the Web API model to my own seems like a big unnecessary job, when the models will end up practically the same.
Keep things simple, introducing a mapping layer when you have control over both sides of the application seems unnecessary.
Only introduce extra complexity when it is required, for instance in our application we use asp.net w/ Nancyfx as our rest api. There are a few times we map from the api to models but those are specialized circumstances (model that contains data but also behavior).
One place where we used mapper was mapping for .net properties Pascal case to json properties camelCase.
But look at this blog post, even this can be fixed by providing a correct media formatter, and hence the mapping layer can be removed.
Mapping layer should be used for case by case basis. Hope it help you.
1 - Browser makes calls to WebAPI
2 - WebAPI returns JSON data - which is actually domain (or model) objects - replicas of database tables.
3 - Javascript translates those JSON objects into different objects depending on your presentation (presentations are always sophisticated - tables, graphs, lists, formated things, pivot tables).
4 - HTML is bound to those sophisticated javascript objects
You can find very beautiful examples here: upida4net.codeplex.com
with knockout and angular.

What is the difference between angularjs and dust.js?

I am currently using the Backbone philosophy which involves dust.js for template style. Recently I came across AngularJS, which extends the HTML syntax with custom elements and attributes.
Cons of Backbone+dust.js environment:
Upgrading components is time consuming.
Module specification and identification is not easy.
If I move my functionality to AngularJS will it be helpful or does it feel the same?
Can anyone explain to me what the major differences among these two libs are, as they seem similar to some extent?
dust.js is purely a templating module. So, it allows the combination of json with a template to deliver html output.
Angular.js is client side framework that allows binding of logic to variables defined in a template (your page).
So, with dust.js you are responsible for deciding when to run the json through the template. Typically you feed in the json on the server (or client) and ask it to render the results.
With angular.js when the model (the json) changes the framework re-renders as appropriate. Triggers for that change could be user actions (such as filling a form in) or it could be due to loading some fresh json from a service.
Typically you would use angular.js if you want a single page JS app (think gmail). dust.js is perhaps more akin to a traditional approach with multi pages with content driven by passing in json.
You could even use the both of them in tandem - server side rendering using dust.js with dynamic client side logic in angular.js.

Resources