Spring MVC Multi page form Data Handling in controller - java

I want some stateful bean or wizard functionality for multiple page form.
I have a page to capture data. We spited into multiple page (4- steps) for User Experience. Project is using Spring MVC (Controller) for web layer. I need to submit and validate data after each step. I also need to use data captured from first step in third step to give user choice. Data can not be persisted in DB/Session.
Can someone help!!!!!

Spring Controllers are stateless and that is not its use. I recommend you use another tools like Spring Web Flow.
https://projects.spring.io/spring-webflow/

Related

best way to import form data from HTML to java Spring controller as JSON request body

I am building my first spring boot project with java in intellij. When capturing form data from an HTML page, is there a 'best practice' way of transporting the data to the server controller as a JSON request body? I've seen solutions with javascript / ajax, but I prefer something more native to the spring library.
There are two ways.
Setting your Back-End as a REST API and completely separate your Front-End and communicating through REST calls. (as you said with JS/Ajax)
Setting your complete app under one roof. Spring and Thymeleaf are a good option. It works like this, every time you do a /GET request then spring will provide a template. Which is a Thymeleaf HTML document which has Thymeleaf specific syntax to make your life easier.
You can submit your data to your back-end through forms. When you submit the form then your app decides what happens next, will you be redirected somewhere else or will another template be opened...That's your call.
If you want a quick way to do it, go with the second way. Personally, I like the first one. It separates the concerns perfectly.

Migrating Spring MVC application from JSP to AngularJS

I am considering moving from server-side rendering view technologies like JSP, Struts to client-side rendering view technologies using AngularJS,
A popular Javascript framework for modern browsers.
When we are making this change and what are all the things that I may encounter.
If anybody who are experienced in Spring Web MVC and JSP development and would know how Spring MVC can work together with a client-side Javascript like AngularJS.
Kindly Answer.
You have to rewrite the view part using angularJS, and replace the logic from the server with a REST API.
It's often a huge work, and the hardest part is to migrate the existing server-side session management to the browser, because a REST API is stateless.
Below is one of the suggested way in which i recently migrated my existing spring MVC web-application in Single page application using AngularJs as client side java-script framework:
1).First you needs to bisect your core business logic which must needs to handle in DB layer,if not done already. i.e. SP's,trigger's,etc...
2).Then for the thing mentioned as hardest by Toilal ("to migrate the existing server-side session management to the browser, because a REST API is stateless"), you can consider Spring Boot as one of the options to build things quickly instead of Spring MVC with fusion of java8 features to yield the JSON response via API's.
3).And than last but not the least, build UI layer with AngularJS in front End encapsulating the data returned by the spring boot API's. Please note that as angular is super power full, you can do most of your business logic at client side,your DB layer just needs to give you the whole bunch of business data to render the screen,other things you can easily handle in angular.
Thus, this way by making the DB layer thin and imbibing angular at front end, you can improve performance of your existing application drastically.
You can also check this AngularJS with Spring-mvc useful thread in same context.

Webflow vs. Angular or Both?

We are developing an Enterprise application with the following technology stack:
Websphere Application Server
Spring (Webflow (Session in View pattern), JPA (Hibernate), Core)
DB2 osZ
Frontend (JSP Rendering HTML5 (CSS 2.0), Ajax in combination with webflow, JQuery)
Multiple Single Pages design combined by using webflow for supporting
subflows
Development Methodology
- Domain Driven/Component Driven Application
- Test Driven Development
Current Situation
Our domain model has very deep domain class hierarchy and therefore we decided to use webflow to allow deep class navigation in sub flows.
We are using the “session in view pattern” because there are many screens reflecting the class hierarchy of the domain model.
This worked very well because of having the backend managing session scoped data for the frontend.
What do we want to do next?
Single Page Design
Control User data in session
Control User data across screens
Support multiple instance of a browser in one user session
Want to be able to talk to the backend (Enterprise Java/Spring) to
retrieve data and persist
Support more state-of-art user experience
Technology Stack
Websphere Application Server
Spring (Webflow (Session in View pattern), JPA (Hibernate), Core)
DB2 osZ
Frontend (Angular, JQuery, Bootstrap 3)
So the discussion internally is how to integrate Angular?
Should we give up Webflow and solve everything with Angular?
Does a mix make sense?
We have taken the following chart as a reference:
http://vschart.com/compare/angularjs/vs/spring-framework
We know what both frameworks are able to do, and know how to make them work. We are interested in other teams experience on how to integrate Angular? Did someone ever mix Webflow and Angular?
We are interested to see some best practices and how teams have transitioned to either framework or keeping both?
Thanks for input,
Andrew
I suggest you to check the Java library thymeleaf in order to get directly the benefits of HTML5 which you can easily add AngularJS and build on top of it (with bootstrap if you need it), and use Spring Security for Auth & session management.
As loose coupling at all levels (front-end included) is most of the times desirable, try to avoid technology mixing (WebFlow & AngularJS in this case) where doesn't makes sense.
Last thing, you and your team can discuss the possibility to develop an Angular based single page application which consumes a server side API coded in Java.
It is too late but might be useful for someone looking for this sort of answer.
I would recommend
Angularjs SPA(single page application) and use html template instead jsp.
Implement Token based authentication rather session based and implement restful spring web mvc api.
web flow/navigation can be controlled using the Angular services or browser's localstorage. I would recommend Angular services to manage app state instead browser localstorage.
And obviously bootstrap 3 to make the UI pretty.
Example implementation: jhipster

Spring web flow v/s simple mvc controllers

I have been doing some research about spring web flows and have some questions for which I cannot find any clear answers.
Question 1: Does Spring Web Flow replace a simple MVC Controller? Can we have a simple MVC Controllers and Web flow configured in one project?
I withdraw question 2 as I was looking at documentation for a Spring web flow 2.0.9.
you can do anything with spring MVC,web flow often use to reduce flow base code such shopping car ,for example If you want implement shopping cart you have to write controller code for each step,even if you are not using database ,that take too long time to get parameters and path again to view response and hold them in DOM ,Web flow help you to keep this information
until flow going to be finish,
But in spring ,Java server face is base on web flow.
Web Flow can not replace in all manner with MVC, for some reason:
1-MVC architecture helps isolate between html designer and java developer ,in web flow you can not do this!
2-In MVC you can make layers such database layer, business layer,service layer in simple way ,It reduce complexity of program ,and reuse again in other controllers injecting same service trigger, in Web flow ,trigger depend on flow step,you can use it again but in less functionality,for example you can not inject controller from controller.
3-Ajax in spring 3 has killer usage,I prefer Jquery response body over JSF ajax usage
If you are using Spring MVC, it will completely replace simple MVC structure, however you must have knowledge of Simple MVC as well. Refer documentation
http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html
And model is a part of Spring MVC you can find it in document. However I did not get your 2nd question.

Best way to implement Register in a Java Website

I have started working on a Web java learning project.
I am making a webapp and I want to register users to the website:
Whats the best way to implement registration functionality:
Have a register.html and in form call a servlet register which registers the user into database.
Or have a jsp page which does all this or similarly call the servlet .
Or any other..
Please explain the reasons too, that why one is better than other, or why some method should be used or preferred?
Thanks
All three that you cite are equivalent. JSPs are compiled into servlets that are HTML factories.
Yes, you need a web UI.
You'll need a database to persist the data, so you'll be using JDBC.
You'll need some object and relational models representing users and their credentials.
You'll want to read about Model-2 MVC for web apps. It describes an architecture where JSPs interact with a servlet, which delegates to other objects to do the work and redirects the response to the right JSP depending on what happens.
You'll want to read about the front controller servlet.
I will go for option 1 :
"Have a register.html and in form call a servlet register which
registers the user into database."
but with a sort of javascript/jquery validations.
Create a html page which contains form and js to validate form fields and a servlet to make
database entries for newly registered user. Options 2 is also similar with this.
Both the methods mentioned by you are same. Take html page and then call a servlet to persist in database or you take JSP page to do,both are same,as in both you are using servlets.. In both methods you will use server side validation and also handle for SQL injection.
If you are comfortable with any of the MVC framework then try to use it.

Categories