Why do we need an Spring MVC or Struts framework if we intend to proceed with single page apps - spring

I have a design problem at hand. Traditionally I had been developing Spring MVC/Struts/jsf applications with either REST/SOAP service serving the data.
Now we have HTML5 and other javascript frameworks. In this light, do we still need to create Spring MVC applicationS as such (assume that the data is served from a RESTful service)
If I go ahead with pure HTML5, CSS UI (with Ajax calls to RESTful services), what are the possible issues that I may get into? does this approach have security holes like being prone to cross site scripting?
Is it a good approach to start off with? Would this approach be called a single page app?

You still need to serve your single page app from somewhere, along w images and css files. This could be a Spring MVC servlet, or whatever.
I'd look into Spring Data REST which is a servlet that creates RESTful HATEOAS endpoints for you.
It's probably easier if your SPA and REST service are on the same domain, so you don't have to worry about cross-domain restrictions.
They are still vulnerable to XSS, as is any system unless you take counter-measures. Spring security can help here. Make sure you follow the OWASP guidelines.
Use Hibernate Validator's #SafeHTML annotation to prevent unsafe HTML input into your database. Use Spring Security's <content-type-options/> , <xss-protection/> , and <header name="Content-Security-Policy" value="..."/> settings to help fight XSS.

Spring MVC, Struts, etc. provide two core functions: Routing, and Binding.
Routing is dispatching an HTTP request to the relevant piece of code.
Binding is converting the world of Strings that the HTTP request sends you in to something more useful, such as a generic Java bean with not just Strings, but integers and dates.
Along for the ride are aspects such as validation.
Finally, they provide an environment that's particularly friendly to your view layer.
None of these aspects, per se, are obsolete in a world where all you have is JSON coming up and JSON heading back. You still need routing, but now you may care to route not simply on the request url itself, but on HTTP verb. You still need binding, having the framework marshal the JSON payload into some easier for java to work with is very handy.
But, (and I speak in general terms, not specifics as I don't know Spring or Struts well at all), while the MVC frameworks can fill the role of a backend system for a more JSON oriented and raw HTTP world, the specific REST frameworks do the job better.
There's no mistake that the MVC frameworks were a significant step up over raw Servlets. Raw Servlets are functional, but simply too primitive for real work. But with design decisions made from a world 10 years ago, some parts show their age and get in the way in the new world of more raw HTTP requests.
If you're doing a mixed app, some MVC, and some HTTP/Ajax methods, then it's better to work with your MVC framework than adopt a new one.
If you're doing a pure singe page app, then it's worth your while to adopt a framework that tends to that niche. They'll simply be a better fit.
Mind, also, they can live side by side. You can have both co-habitate in a single WAR if you wish, particularly if you're adding SPA features to a legacy application. It doesn't have to be an either/or situation.
But I wouldn't introduce an entire new infrastructure component simply to handle a couple of ajax calls. In the end, they're just HTTP calls, and the MVC frameworks (in contrast to most component frameworks) do just fine with raw HTTP for the most part.

Related

Web client for spring-data-rest CRUD endpoints?

Spring Data REST creates a CRUD web server with a discoverable API, so it seems it should be possible to write a generalized web client application for it. Is there such an application?
May be you are looking for a HAL browser
https://www.baeldung.com/spring-rest-hal
or
something like https://www.npmjs.com/package/angular-spring-data-rest
https://www.npmjs.com/package/angular4-hal
I hope you mean sample client stubs. Actually a web client cannot be generalized beyond the resources it has. That will not be quite meaningful.
You can try below with swagger. Using swagger here would be really convenient (over raml etc) since spring-data-rest generates swagger it self for you.
Take your swagger spec
Paste it at https://editor.swagger.io/.
Go Generate Client => Your favorite programming language.
Then it will generate sample client stubs for you in the language you have selected.
I think this should be the far most generalized point that makes sense.
-Addition-
The primary problem spring-data-rest has solved is abstracting out all the common functionalities attached to controller (ex: response/request mapping etc) and making them readily available and configurable, so that the developer no longer needs to re-invent/duplicate them every time when they are coding a new endpoint.
So as you have suggested generating client-stubs is completely out of spring-data-rest scope. Please read the documentation for more info.

CRUD operations using asp.net Icontroller Versus using asp.net ApiController inside my asp.net MVC

If I need to build a web application that implement functionalities related to managing an Medical hospital institution. That application include functionalities to add, update, delete, search and retrieve patients, medical visits, etc.
So I am confused on which approach to follow inside my ASP.NET MVC web application, either to have my controller classes inside my asp.net MVC.
Derived from IController to perform the CRUD operation
OR
Derived from ApiController to perform the CRUD operations
SECOND question is there an approach that is taking over the other, and what are the advantages/disadvantages of each approach over the other?
You can absolutely use both. Nothing stops you to use good old Controller to do CRUD in JSON and MVC is capable of doing it. However, you do not get all the nice features of Web API such as content negotiation, message handlers, media type formatters, etc.
If you do not need any of Web API specific features, then use MVC. But on the long run, Web API will be a better investment as I believe adoption will rocket soon.
If you are going to generate classical HTML pages from your controllers, use the MVC ones. If what you want to return to a client is some kind of serialized data (XML, JSON, ...), use Web API controllers since these make it easier, regarding e.g. content negotiation.
See also Getting started with Web API

Server side MVC +Client side MVC

An application is both JS intensive and has a complex business logic +huge data flow that uses backboneJS to structure JavaScript code and there is a necessity for the server side MVC also (Spring in this case).
Is it a good idea to implement server side MVC (spring ) along with client side MVC (backbone) and are there any compatibility issues between them?
In case both can be implemented.How am i going to coordinate the spring views with the backbone view.
How will client side MVC with template engine collaborate with 'View' of Spring.
Though this question has been asked before, I could not find suitable answer for this. Can somebody help me with a detailed answer?
Backbone.js is primarily used for SPA's (Single Page Applications). Your backbone.js segments ('views') are rendered and re-rendered by data, that is fetched or changed. If your application is going to be javascript intensive, backbone is a great minimal framework that will help organize your application. However, backbone is not so simple when your javascript application becomes quite large. You have to have a thorough understanding of the fundamentals of javascript and have a programming decided pattern. Otherwise you application can become quite a mess even with the use of backbone.js.
Anyhow I digress. Think of you client side application (backbone.js driven) completely separate from your server side application (MVC driven). All your client application needs from your server application is the data. That is why backbone.js is built around a REST API. If you are looking to develop a large javascript application, I would look at using MVC 4's Web API for your REST API, and backbone.js to help organize and standardize your client application.
This article describes how to make an application architecture with both server side MVC and JS MVC , respecting the MVC pattern.
http://blog.javascriptmvc.com/?p=68

Frontend ajax framework work with Grails

I have some knowledge about Flex and Java EE, they are good for web application development. Anyway when I try to write a typical web page that is based on HTML/CSS/Javascript, I think I should take a look at some new program language/framework.
I heard much good news about grails and finally decided to learn it instead of python, ruby, scale… But I still don’t have an overview of the whole structure. Grails is a backend framework like php, jsp, jsf right? So that probably means, it’s a replacement of Java EE in backend, then how about the frontend (need ajax functionality), what are people using with grails?
thanks
Grails is not a replacement, it is an abstraction around the tradition Java EE stack and some extremely popular libraries like Spring and Hibernate, that allows you to go faster by using "convention over configuration".
One component of Grails is GSPs, groovy server pages, which is a front end technology, the V in the MVC (Model View Controller) paradigm. You also have Domain Objects, which are the M (Model), and Controllers, which are the C. Grails also has Services which are best put into the M category (IMHO) of the MVC paradigm. So the Model arrangement in Grails gives you relatively easy persistence (using hibernate under the covers), the Services give you great reusability in your business and transactional logic, and the Controller simply invoke the right logic for a given request, and return the response.
One part of that response is what gets displayed on the screen. In a simple webapp, GSPs fill that role -- the controller tells the browser to render a specific GSP which has data bound to it from the service method that was invoked in the controller. However, it is easy to have the controller return json, so if the endpoint bound to the controller is an ajax request, the client can handle the response itself.
You can use any front end technology you want in a grails app. The default is GSPs, which is an extension of JSPs, which are part of the traditional java stack, but you can use jQuery, Sencha, Sproutcore, Backbone, anything you want. You would have one GSP in that case which bootstraps your javascript code, and the rest would be handled by the client application.
Grails is a web framework and is not just a backend framework. It supports both JSP and GSP ( Groovy Server Pages) for views.
If you plan to use Ajax functionality, you can make use of one of many javascript frameworks available. You can also go ahead with Flex (since you already know it) or use a javascript framework like ExtJs, Dojo, YUI etc...

spring mvc vs seam

Spring mvc is a framework that has been long time out there, it is well documented and proven technology. A lot of web sites are using spring.
Seam is a framework based on jsf - rich faces implementation.
It has a lot of ajax based components.
It uses some heavy stuff like EJB, JPA.
All of this is prone to errors and this framework is so slow (at my computer it is almost impossible do develop something because it is really slow, especially redeploying on jboss)
But is is very good for back office applications.
Does someone have a professional experience with this two frameworks?
Can you recommend the better one ?
Why?
Regards
I use both: Spring-MVC (2.5) and Seam
Because Seam uses Java Server Faces Technology (A server-side based Technology), behind the scenes, It is better designed for small and medium applications. (Each JSF view Tree is stored on Session - You can store on client side, but be aware bandwidth issues). But it has some advantages:
Typically web application uses the following path
view >> controller >> service >> domain
With Seam, you can get
view >> service >> domain
Or even (by using mediator pattern provided by Seam Framework)
No controller, No service
view >> domain
Besides that,
JSF 2 supports JSR 303 - Bean Validation
You can use Wicket instead of JSF if you want
Conversation and Business process management support
Use can use Spring DI if you want
Spring-MVC
It has a powerful web-Tier infrastructure
Handler Mapping (It chooses which Controller should handle the request)
View resolver (It chooses which View should render the response)
It can be used for large applications
Powerful data-binding
Spring 3.0 supports Annotation-based Controller (JSR 303 - Bean Validation, coming soon)
But i still not use Spring 3.0 because
By using (and extending when needed) MultiActionController, i can get convention over configuration without no xml settings to define your Controller (You just need to set up your MultiActionController as #Component)
SimpleFormController provides similar behavior found in Spring 3.0 annotation based controller
...
About The learning path, i think both are similar.
I have worked professionally with Seam and it is a killer framework. It really boosts up your productivity. You can use POJOs instead of EJBs, if you think EJBs are slowing you down. About the deployment, just consider deploying to Tomcat instead of JBoss. On my machine redeployment in Tomcat is done in a couple of seconds. But I still haven't used Spring MVC to compare them.
We have been using Seam for a very large site using POJOS and communicated with web services or Hibernate deployed on Tomcat. We have found that the back button support does not work well or is prone to being specific to implementation patterns. Additionally, there is a tendency for sessions to grow very large if using server side state. Attempts to reduce the session size impact back button support or iframe usage due to reduce number of view states stored in session. More or less, our issue have all been performance related. The tomcat requires more memory and supports less users when compared to a struts 1.2 or spring mvc. We are using around 50 tomcats to support our userbase.
I have used both SEAM and Spring MVC for a few months.
I prefer Spring MVC to SEAM. BTW I noticed JBoss halted the SEAM3 development.
I found a interesting article about comparison of SEAM and Spring MVC.
The link is
http://java.dzone.com/articles/why-java-ee-lost-and-spring?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+javalobby%2Ffrontpage+%28Javalobby+%2F+Java+Zone%29&utm_content=Google+Reader
Thanks
I have used both Seam 3 and Spring 3 frameworks.While Seam 3 is easier to code and has a lot of features supporting session management and transaction management ,the performance is slow.Spring is much faster.

Resources