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.
Related
How can I know if a Java app uses the MVC design pattern? For example I want to know if this app from github uses the MVC design pattern: https://github.com/eveningstar33/GoalTracker
It is a fullstack app, the frontend is made in Angular and the backend is made in Spring Boot. And if yes I want to know who is the Model? I think that RestController is the Controller and the Angular app is the View. Isn't it? Or if an app uses Spring MVC does it also use MVC design pattern?
MVC part in Spring MVC stays for implementation (very good implementation :)) of broader JSP/Sevlets based MVC concept, where typically we have Front Controller Servlet, which dispatches requests to corresponding View Controllers and resolve/prepare result views to be rendered for user.
The MVC parts could change when we have both Angular (or any other Single Page Application JS framework) and Spring MVC working together. As in Angular usually you have "Controller" class per each component, which can have a lot of logic inside, connecting to different services. So less logic stays on server and it acts as a proxy to DAO layer to perform CRUD operations. For such cases it is quite common to see:
View - Angular View templates (e.g. Goal View)
Controller - Angular Controllers (e.g. Goal Component)
Model - REST API (built with or without Spring MVC framework) (e.g. Goal Resource)
MVC is one of the design patterns in software development. Model is usually a POJO (Plain Java Object) that encapsulates the data of the business objects, View is the one that users interact with that is also responsible to take request from users and render response and Controller sits in between View and Model to transfer data as well as act as a navigation decision maker.
You can write the back end in Spring Boot providing HTTP APIs to any client including Angular. Angular internally uses TypeScript ( a super set of JavaScript ) that you can use to just consume the APIs.
Here is one such example, where I have written written services in Sprint Boot - https://github.com/royalghost/PortfolioTrackerServices
And then there is a separate project that consumes the above services using Angular - https://github.com/royalghost/PortfolioServicesTrackerClient
Spring MVC is one of the framework that implements the MVC design patterns. You can take a look at a separate project that uses Spring MVC - https://github.com/royalghost/UserRegistration . Also note that this project uses Spring Boot since Spring Boot is just an easy way to put all the required libraries together to quickly build, start and deploy an application.
Angular is more popular with Single Page Application ( SPA ) and a lot of server side computation is now pushed towards client ( this is also to scale the product. ) Therefore, it is completely up to the developer on which design patterns or architecture to choose when using the same technology and stacks.
References:
MVC - https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
Angular - https://angular.io/guide/architecture
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.
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.
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.
I am new to Spring MVC3 framework in java but I am familiar with java coding.
I want to write two application using this framework.
First application recieves requests through a SOAP web services and sends response in form of SOAP XML Object.
Second application have a simple servlet to recieve request and send responces.
I have studied Java MVC3 framework. It requires view to be called who are mapped against which controller will handles its request. But,
How I can do this using a webservice, so that when a specific method using SOAP services is called, I can forward that request to its relevant servlet and sends response back as a SOAP xml file.
How can I do this for my second application as well that recieves request through a servlet.
I hope all this make sense.
regards,
Aqif
If you want to stick with Spring, you can use Spring Web Services for application 1. Application 2 would be a more traditonal Spring Web app (uses a servlet, but framework does not require you to work in the servlet...instead you will work in more fine grained components).
If you dont want to stick with Spring for the web services, you can always use something like Apache Axis
The usual structure is as follows:
you have spring-mvc controllers to handle your browser requests
you have other components that handle the SOAP requests
both of the above invoke the same underlying services which serve them with the data that is to be sent to the user. The data is in java objects, which are later transformed to whatever is required
For the 2nd point you can pick some JAX-WS implementation, like CXF (it has nice spring support as well)
Spring Web Services specifically supports a Spring MVC-like model for responding to SOAP calls, as you describe.
the second one is Spring MVC directly. Heck, it sounds like - though I can't be sure without more information - that you're trying to build RESTful web services. There, too, Spring MVC is the right choice.