Can i use servlet with Angular JS and implement MVC architecture? - java

I am new to MVC and searched a lot about it and following are the things which i found on Stackoverflow.
1. With angularJS at client side ---> NodeJS , Rails are good to be used on server side as they have certain benifits.
Now i have a question :-
even spring is an MVC framework, but can i use Servlet and Angular JS and implement MVC with it?
Thanks in advance.

Angular doesn't really care what you're using server side it just expects to be able to send JSON encoded data in the body of requests and get responses that are JSON encoded. So the short answer is yes you can. Regarding MVC there's basically layers of this design pattern used throughout the client side framework itself, on the server side typically I just have code that loads data from a database (or updates/inserts/deletes data) then encodes it and sends it to the client. This way the server is essentially decoupled from the client and either could be rewritten or appended/supplemented without replacing either.
MVC just means model view controller, which is a design pattern for dividing up code. The model is the data itself and is "the source of truth" for updating the view, the controller is responsible for making changes to the model, and the current state of the model is always reflected in the view.
With Spring MVC traditionally the server would be responsible for wholly processing the request using a controller that updates the model then generating a view that was delivered to the client.
With Angular you use AJAX requests to get data from the server, then you update your model (typically through a controller as a proxy to a service/factory in angular terms) and then the bindings/watches in angular automatically update the view.
So in the case of angular really your server side is not responsible for creating the view but only responsible for persisting data (and dealing with authentication and authorization). On your server side you may still maintain a model that corresponds to some schema in a database or otherwise and you may have some route handling layer that you could consider the server side controller that acts on the model, you're just not really dealing with the view at all anymore.
There are lots of advantages to this approach both for the end user and in terms of development. You can test your backend and frontend separately and you can refactor or replace either or build additional frontends (native clients etc.) or add backend modules without disturbing the clients. The only thing you need to take care of is maintaining a consistent interface between your client and server components.

In short, Yes.
Just make sure you are passing the data in JSON format because AngularJS expects JSON, as explained by #shaunhusain.
This link will be very much helpful to understand how to create folder structure for the application.

Related

Spring mvc - Server side programming

I have a spring mvc application which i use as only a server. I have deployed some html and javascript files on my hosting account and sending post requests to my server only. Retrieve data from database and send back the data to the html or javascript pages. I don't use any servlets,jsp or jsf. Everywhere i look it says that i should use them however. Am i doing something wrong? It feels like bad practice but i don't know the right way to do it i guess. Any help would be appreciated.
As I already mentioned in my comments, you can look into creating a webservice. Since you are using Spring already, try this guide on Building a RESTful Web Service.
After creating the service, you can call it up like this, where you pass in input parameters to your rest endpoints,
http://localhost:8080/greeting?name=User
Now your service will respond back to your GET request and you'll be output a JSON/xml string which you can process later at the client side. The sample json response looks like this,
{"id":1,"content":"Hello, User!"}
Here's another example blog article on Spring Restful Web Service Example with JSON, Jackson and Client Program
Well, I think you should first expand your question a bit since one needs to assume a lot to give you an answer.
IMO, JSF is not great and there are other better options for UI and JSPs are dead. But I'm biased since I used last time JSF 1.X and then went with Spring MVC and Angular or Spring and Apache Wicket more recently.
I used for instance Spring MVC to implement an HTTP RPC API with JSON payloads to which an Angular frontend connected and it worked just fine. I also had the feeling that everyone is doing that nowadays:).
I also guess you are using at least one servlet to configure Spring DispatcherServlet. Right?

Understand the Spring framework application backbone concept

Let me start by saying I was just thrown into a Spring MVC project and my only experience has been from a GWT project.
In GWT we created a hierarchy of pages filled with 'presenters' which indicated a new page. So if I wanted to create a new page I could simply create a new presenter and widget number and that number was inserted into the URL, thus creating a new web page. From there I could assign text boxes, tables, etc to that widget number, which in turn would populate my new page.
Questions
In a Spring MVC hibernate project how would I indicate I want a new page? where would information for this page be contained? Much of the Java files I see in my project are validators and logic oriented and less page structure and layout.
I used click listeners and handlers a lot in GWT. Why have I been unable to find any in my project's existing code base? How is this type of thing handled in Spring?
1) MVC means model-view-controller pattern, so you need to learn how to integrate your views (html, jsp, jstl) into Spring. Refer here:
http://docs.spring.io/spring/docs/2.0.8/reference/view.html
2) There are also listeners in Spring, for example, ContextLoaderListener.
These might help:
http://www.docbyte.com/fr/blog/integrating-gwt-with-spring-and-hibernate
http://www.javacodegeeks.com/2010/05/gwt-2-spring-3-jpa-2-hibernate-35-2.html
A Spring MVC application is a web application built on top of the Servlet API. You run such an application in a Servlet container which acts as an HTTP server.
Spring MVC follows the Model-View-Controller architectural style. A controller is dispatched to handle the request based on the mappings you configure. The controller performs some logic, delegates to the model, prepares it and makes it available to the view. The view can be pretty much anything. You can have it have it generate HTML, XML, JSON, some other binary content type, etc. That content is written to the HTTP response which your HTTP client can then display/render/download.
In a typical Spring MVC app, you'll have the views setup with JSPs. Your configuration will declare an InternalResourceViewResolver which will forward to JSPs based on view names that the controller handler methods (methods annotated with #RequestMapping) return.
GWT follows a completely different methodology than Spring MVC. Spring MVC is straight up client/server. The client sends a request. Spring MVC receives it, dispatches a controller, and returns a response. The lines are clear. In GWT, not so much. IMO it feels more like a standalone application with buttons and listeners. The actual underlying implementation is still client/server, it's simply hidden from you as a developer.
When you click the button on a form, a browser typically sends an HTTP request to the server, the server responds and the browser renders some other page (or does something else, depending). In Spring, the flow is as described above. The web app receives the request, dispatches to a controller which returns a view, and then returns a response based on that view. As a developer, you code most of that.

how to invoke spring mvc from pojo

I am new to spring world. I am trying to add to new functionality to an existing spring MVC based reporting project. The new functionality would give the user an ability to schedule a particular report to run every so often.
I see that in current design, model and Controller sections of MVC is tightly coupled. Due to time constraints, I am not trying to separate Model and Controller instead want to use the existing infrastructure as is. My plan is to mimic the browsers request in a pojo and somehow call spring's MVC to handle my request. Am i going in right direction, if so how to I invoke the spring MVC from a POJO, if not, what would be the right approach. Please help........
If i understand you correctly, basically you would like to :
create a non-web application that makes requests to existing Spring MVC controllers
and that you have to reuse existing controllers
and NOT reusing the business model, because you dont have the time to separate the business model from the controller
Basically you are doing web services with this approach, where you make requests to the controllers via http using a http client library. And perhaps, get the response as json, which you can later map into a java bean, and continue your work from there.
You can have the controller to return the view's model as xml, or json, etc, or even directly return a bean as json or xml using spring's message converter.
If you are already using Spring 3, there is a RestTemplate that you can make use of to simplify this.
You could also make use of Apache's HttpClient, whose interesting example that you can see in this REST template code.
This related Q&A on java http clients could also be helpful.

Which Web technologies allow for the development of static user interface/presentation layers

I have a Spring-WS web service that runs on a Jboss application server. I also have a Spring-MVC application running on a separate server running Jboss 7. The Spring-MVC application is used mainly for the user interface. The Spring-WS application contains all the services for the business logic.
The standard approach to use the components we have would be in the following order:
Client Browser ---(HTTP Request)---> Spring MVC ----(SOAP Request)-----> Spring-WS
Client Browser <---(HTTP Request)--- Spring MVC <----(SOAP Response)<----- Spring-WS
There is a requirement to change some of the requests coming from the Client Browser to go direct to the Web Services instead of via the Spring MVC application. The Spring MVC application will be used to load the initial presentation screens but the actions that involve any updates/writes will be going through the Spring-WS process.
To achieve this, we have a bespoke process which runs on the same machine as the client browser that traps all HTTP requests. Its purpose is to convert the request to a SOAP message and the response to a HTTP response. The path is shown below:
Initial Request (Retrieve presentation/user interface)
Client Browser ---(HTTP Request)---> Spring MVC
Subsequent requests
Client Browser ---(Http Request) ----> SOAP Converter (Local process) ------> (SOAP Request) ----> Spring-WS
Client Browser <---(Http Request) ---- SOAP Converter (Local process) <------ (SOAP Request) <---- Spring-WS
There are two paths that happen in the above scenario. The initial request to display the pages on the screen will be a request to the Spring-MVC process. Any subsequent requests that involve changing the data will be via the path shown above.
The problem that i have now is that all responses from Spring-WS (the webservice) are in XML format. This means that when the request comes from the browser, the data will have to come from the web service but the pages will need to be refreshed from the Spring-MVC application. This somehow feels a bit wrong as each request will involve to calls. One to get the data and one to get the presentation data.
To overcome this, i would like to implement the Spring-MVC layer using a technique where i only need to make one initial request. This means that the user interface will rendered on the screen. All subsequent requests to the Spring-WS service should not result in the browser to be rendered other than refreshing the data.
I am interested in knowing what kind of technologies i can use to achieve this. One way of doing this is by using Applets but this has been ruled out for security reasons. I have seen several websites which work exactly the way i have described above. i.e. the page never refreshes. A very good example is the Sonatype Nexus Maven repository manager user interface shown below:
It runs in a web browser and when the Nexus user interface loads on the browser, it is almost like a Swing type application. (Any one know what technology Nexus use for the user interface?)
I guess my question is which Web based user interface technologies (preferably open source) can i use which has a swing type look and feel but is not Swing and requires minimal requests to the server to refresh the screens?
Thanks in advance.
Nexus' interface is built using Sencha GXT3, which now also contains ExtJS.
Have a look at the GXT API: it contains a lot of web components which knows how to update their own state without doing the full request/response cycle (using Ajax), which is what you seem to be trying to get away from.

Using Spring in Java swing app

We are seeing Spring in school right now but we don't have the time to wait till the end of the semester to start developing an application. We continue using an app we made last year, and are writing the service layer right now.
The problem is our "client" wants to have a desktop client and a webpart, which used the same dtatabase. This would be no problem if we hook up a server that can handle RMI. So basically we want to be able to retrieve/send data to the server that runs our service layer, and use the objects on the client side as well.
I have no idea where to start digging in Spring to figure out how to do this, so some help would be appreciated.
PS: At this point I do not need MVC yet. MVC is handled from within the desktop app where we have views and controllers.The model is the same from the one on the service layer. How do we use the same model without copying it?
Check out spring remoting: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html
It's easy to expose your spring beans remotely, using a variety of protocols.
You might want to take a look at the REST paradigms. With this in mind you could have a web server running your server part of the application and communicating with clients through the HTTP protocol. A simple client could be a webpage in the browser which gets the corresponding HTML pages from the server, or a Swing client which communicates over JSON with the server.
The server can implement different methods for JSON or HTML communication and the server can decide what implementation to use by looking at the Accept Header of the Request objects sent to it, that's what they call Content Negotiation
JSR-311 is implemented as Project Jersey which is a framework for RESTful webservices. You might want to take a look at that.
hope that helped

Categories

Resources