Making a Simple Web App with Spring MVC
For this tutorial we’re going to assume that you already have knowledge of basic Java web development with JSP’s and servlets. Like before with the other spring tutorials we are going to start out with importing the required libraries from Maven, these are the ones you will need to build a simple web application using the Spring MVC framework:
After creating a Dynamic Web Project we’re going to start with creating our dispatcher servlet which will handle all application requests and determine which controller the request should be directed to to serve up the proper view. So create a servlet in your project and select the “Use existing class” option during creation, click the Browse button, and if you have imported the proper libraries you will be able to select the “DispatcherServlet” class. If you check your web.xml file now you will see that a dispatcher servlet has been added to the configuration. Now you will want to change the display name and the servlet name of the dispatcher servlet in the web xml to something that makes sense for your application. You will also want to change the URL pattern to just “/” so that the servlet is the default file to be run on startup of the application. Then in your servlet class you will want to add the “<load-on-startup>” tag and give it a value of “1” between the tags.
Now you will want to create a Spring-beans-configuration file in your WEB-INF folder and name it the same name as your dispatcher servlet in the web.xml followed by “-servlet”, this is where your project will load beans from. Before you move on you will have to make sure Maven Dependencies is included in the Java build path for your project so the server will be able to locate the libraries upon running the project. Now, if you try to run your project it will not work as we still have to add a controller to the project.
To create a controller for the project you will just create a normal Java class in your project and give it whatever package name you like following typical conventions. The class can also be given whatever name you like but it should still make sense within the context of your application just for convenience sake. Now well want to give this class the annotation of “@Controller” and import the Spring import for it. Now we’ll just give the class one simple method that returns a string of say “home”, and give that method the annotation of “@RequestMapping(“/”)”. This defines what view to use upon receiving a request. We’re just giving this a return of “home” because it will map to some sort of simple home page.
Now we need to add this controller class as a bean so we will go to our offers-servlet.xml file and add the namespaces of “context” and “mvc” by checking the boxes in the namespace tab. Now you can go to the context tab and right click “beans” and insert “<component-scan> as a bean and set base-package of this bean to the package name of you controller class. This defines the path your project looks to by default to load in beans. Now you can go to the mvc tab and insert “<annotation-driven>” under the beans so we can configure the project using annotations instead of manually configuring our .xml.
The View Resolver
The final step to get things up and running is to add a View Resolver to the project. The view resolver takes the data from the dispatcher servlet and determines what view to display. So we’ll make a folder in the WEB-INF and call it something like “jsps”, this is just to store the jsps that will be displayed by the view resolver. So lets add a jsp called “home”, this name corresponds to the string we are returning in the dispatcher servlet. We are going to use the class “InternalresourceViewResolver” in the spring-webmvc jar to resolve our logical name “home” from the dispatcher servlet to the appropriate jsp file. So go to your libraries and copy the qualified name for the web.servlet.view package under the webmvc jar. Now we’re going to go to the servlet sml under WEB-INF and create a new bean called something like “jspViewResolver” and paste in that package name into the class field and type “.InternalResourceViewResolver” after the path name. You can check you got this correct by clicking the Browse button and assuring that the class shows up in the pop up window.
Now insert a property element into the newly created bean called “prefix” and set it’s value to the path of your “jsps” folder, so something like “/WEB-INF/jsps/”. Now we will insert another property element called “suffix” and give it a value of “.jsp”. This will direct the view to load up a jsp in our jsps folder given a logical name from the dispatcher servlet. The InternalResourceViewResolver class is the thing that does all the actual heavy lifting for us as far as taking in the logical information and serving us up the appropriate view.
So this should give you a basic understanding of how the Spring MVC framework works. There’s a large abundance of useful tools this framework gives us that we will explore later such as adding security for web forms and authenticating users.