Pages

Showing posts with label Form Validation. Show all posts
Showing posts with label Form Validation. Show all posts

Saturday, September 14, 2013

SpringMVC application with Hibernate + Bootstrap + JUnit + Mockito + Form Validation

In this post, let's look at a basic, but technology wise rich SpringMVC application. Wondering why yet another tutorial in SpringMVC while enough available in net? The whole purpose of this post is to develop a simple application with required important technologies which could be understood by a beginner also!

In abstract, this sample application uses the following technologies. It's perfect to give a kick start in each technologies, so happy coding!

  • Spring MVC 3.2
  • Maven 3.1
  • Hibernate 3.6.3
  • Jetty-Maven 8.1.12
  • Mockito 1.9.5
  • JUnit 4.11
  • Bootstrap 3.0
  • MySQL
If you wish to start right now without spend much time in reading, you can go ahead and download the code to develop your application on top of it. This post includes the basics to get into the project. Await for posts with explaining the technologies used & configuring them in near future :)

Download the full project from GitHub


This is a basic customer management system where you can add, view & delete customer details.

  • The JSP pages are using Bootstrap for CSS, which makes your life easy by giving best UI experience out of the box
  • The full project is based on Maven, so all the advantages of Maven comes along- Easy dependency management, unit tested automated builds, so on. 
  • Jetty-Maven plugin is used to deploy & run the application within seconds of time. 
  • There is a sample unit test including Mocking objects used in this application, so it'll be a starter for people who are new to Mocking and Mockito
  • Form validation is done through annotations and achieved by javax.validation API. Also it is done in a way to support Internationalization- so that it can be adapted to various languages and regions without engineering changes
  • Annotation based Hibernate is used as ORM framework and HQL used to query the MySQL DB.

Table creation



According to the sample, you need to create a schema named customerMgr and run the above table script. Or else you can create a schema name at your choice and make sure to change it in the database.properties file. Similarly, change the database username, password in the same file.

Start the project


I love IntelliJ IDEA for it's best features, but it's possible to use any IDE as this is a Maven project. Just open the pom.xml file and Idea does the rest for you. Once imported, the following structure should be available without any errors after Maven finishes downloading necessary library files.




Changing the logging directory or name of the file


You can edit the log4j.xml in the resources directory if you wish to change the logging directory/ name of the file / the logging level.



Form Labeling and Validation errors


All the labels and validation error messages in this application are supporting internationalization. If you wish to change any of the labels/ Error messages, go ahead and change them in the messages_en.properties file under resources folder.


Here, labels can have any meaningful names at your selection. But the keys under validation error messages section has to follow some standards. There will be a separate post about validation, but for the time being, you need to know that this format should follow the below format:

{ValidationClass}.{modelObjectName}.{field}


Changing the Context Path of this application


It's easy to change the context path in jetty-maven-plugin section of the pom.xml file. Currently it is set to customerMgr and if required, you can change this. 


Here, you can see another useful feature during development phase that the auto scanning for changes. Currently it is set for 5 seconds and while the Jetty is running if we do any changes in the source & recompile, it'll be detected automatically within 5s & applied without any hassle.

Running & testing the application 


To run the tests, use the command mvn clean or simply right click on tests phase under Maven projects tab & select Run.

To Run the application, use the command mvn jetty:run or simply right click on jetty:run under Maven projects tab & select Run.

Once successfully started the application, you can go to the page from he following url. Make sure to change the context path if you have changed in the pom file.




Home page



Customer add form

Form with validation error

That's it! Hope you guys enjoyed and it was clear enough. Wait for the posts with more details of the technologies used in this application and the ways to configure them. Until then, bye!