Pages

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!

Thursday, September 5, 2013

Leap Motion- the magical revolution!!

I got a chance to attend a nice technical session on Leap Motion last evening organized by Melbourne Java & JVM user group, and it was an amazing new technology which all of us were seeing only in fictional films. It might be a killer technology in the future and let's look some details of this interesting device.



As you can see, using this we can interact nicely with our computer by hand gestures. The device captures the hand gestures in 3 dimension and we can use the data received from the device in anyway we want. It's a small device which connects through USB to the computer and above all it's not that much expensive!

Airspace- the Appstore of leap motion introduces this as "Hands and fingers are free to move anywhere, so apps are free to do anything"

So you can
  • Zoom in & navigate using your hand in Google Earth
  • Mold a masterpiece in 3D
  • Play air guitar for real
  • Fly a plane with your hands
  • Play many games using hands

Think how nice it would be to play Fruit Ninja or Stick cricket using this ;)

Worth noting that still this is in evolving stage, so there are some improvements required and being improved as well. There are arguments that this is no more powerful than Microsoft's Kinect and not able to detect tiny movements. True, after all no technology could be invented error free! So we could expect much much improvements in this soon.

There are Leap Motion Developer SDKs available for Windows, Linux & Mac with required examples and libraries, so it's worth giving a try in this revolutionary device. Here is another useful video containing more details of this:



Hope it was helpful guys and catch you with another interesting topic soon!

Monday, September 2, 2013

How to include external CSS file in JSP with SpringMVC

In previous post we solved the issue of loading JSPs with SpringMVC without providing the file extension. When we solve this issue, there might be some possibilities that the external CSS files not loading properly and you might get the following error:




So the question is, how to include external CSS file or Bootstrap CSS file properly into JSP??

It's easy as adding few lines in your SpringMVC 3 XML file! Before that, let's see what the issue is.

When you keep your CSS/ images/ JS files inside your web folder and try to load it in JSP, it goes to Spring's Dispatcher Servlet. There is no mapping for those files and Spring won't load them.

Add the following line to the dispatcher-servlet.xml and put all the static resources like CSS, images & JavaScript files inside a folder inside your web folder as mentioned below:
















And the JSP file needs to access the external CSS file as follows. In this example, I've used Bootstrap, but same applicable for your own CSS as well.



Now a final tip before finishing- did you notice the first line of the spring config file? Make sure you have the line <mvc:annotation-driven/> in place when you are using annotation based controller definition in your source. Try without that line and you'll have a frustrating situation where the resources will work fine and the JSPs won't be loading properly!

The reason is this. When you add the  line, SpringMVC adds its own handler mapping and the default DispatcherServlet's mappings  are broken. Therefore other handler mappings should be declared explicitly, either by  or manually as beans. So adding <mvc:annotation-driven/> does the trick for you. That's all!



Error 404 Not Found when calling url without file extension in SpringMVC

If you are new to web frameworks and trying some sample programs available in the net, you might face this error at initial stages.



If you try the same with the extension, it will work:

/testMVC/index.htm OR /testMVC/index.jsp

In this scenario, have a look at the  in web.xml. If the url pattern is something like



<url-pattern>*.htm</url-pattern> OR <url-pattern>*.jsp</url-pattern>

you might need to do a change to make it working without the extension.

If you want to make sure it works without the extension, you need to change in the web.xml as follows:
Wondering what is the change? We are telling Spring servlet to take care of all url patterns. So all the requests will be handled by Spring. When you do this fix and make it working, you might encounter another issue which we'll see in the next post. Look forward to solve the issue: Unable to load external CSS file in SpringMVC