Creating a Simple Spring MVC Web App with NetBeans 5.0
Creating a simple Spring MVC application is pretty straightforward. In this article, I aim to show how you can do this with NetBeans 5.0
Creating the Project
The first stage is to create a new web project. Select "File | New Project" and select to create a Web Application as shown in the following picture.
Select the "Next" button and enter the details for the project. All that is really required here is the project name - in this example, we will call the project "HelloSpring".
At this point, we can press the "Finish" button because the final tab in the wizard allows us to add Struts and JSF support - neither of which we are going to use in this tutorial. We now have an basic project that we can build upon.
Adding the Required Libraries
Now we need to add the Spring libraries into the project. You can download the latest version of Spring if you haven't already got if from www.springframework.org. The version used in this tutorial is 1.2.6. you may find it easier to download the package containing all the dependencies so that you know that all the necessary jar files are present.
Right click on the "Libraries" entry in the Projects pane in NetBeans and select "Add Library". On the following dialog, select "Manage Libraries". Create a new library called "Spring1.2.6" and add all the jars from the spring/dist directory as shown in the following picture. Not all of these jars are required for this simple tutorial, but if you continue using Spring, you'll need them eventually.
Close this dialog to be returned to the list of libraries you have defined in NetBeans. Select "Spring1.2.6" and "JSTL 1.1" as the libraries to be used by the project and press the "Add Library Button". The project should now be configured correctly in NetBeans to use all the required libraries.
Application Configuration - web.xml
Spring MVC uses a Spring servlet class (org.springframework.web.servlet.DispatcherServlet) as the controller of the application. This needs to be configured within the web.xml file for the application. Open up the "Configuration Files" node in the project tree and open the web.xml file. Press the "Add Servlet Element" button and enter the details for the servlet as shown in the following dialog.
We want this servlet to start up when the application is deployed, so change the "Startup Order" for the servlet to be "1".
Application Configuration - Spring
Now that we have configured the basics of the web application, we turn to configuring Spring for our application. Within a Spring web application, all of the Spring related application configuration is managed within an XML file. This file uses the controller servlet's name as its basis, with the string "-servlet.xml" appended to create the full file name. In our case, the file is called "HelloSpring-servlet.xml". Create this file underneath the WEB-INF directory and enter its contents as follows:
This file configures 2 spring beans. In reverse order, the second bean configures a simple url mapping and states than any request for the url "/hello.html" goes to the HelloSpringController. The first bean configures this controller and states that it is managed by the HelloSpringController class.
Creating the Web Pages
Since this is a very simple application, there are only 2 web pages involved. The first presents an edit box to the user asking them for their name, and the second echos their name to the browser. Edit the file index.jsp to have the following contents.
This file has contains a simple form asking the user for a single piece of text input. When the return key on the keyboard is pressed, the action "hello.html" is performed.
In our configuration file (HelloSpring.xml), you will remember that we mapped "hello.html" onto the controller HelloSpringController. We'll discuss that in a minute. For now, let's create the output page. Create a JSP page and call it "welcome.jsp". The contents of this JSP are shown below.
This page is very simple. It simply takes the value "name" from the model passed to it and outputs it to the browser.
Creating the Controller
The final stage in this simple tutorial is to create the controller class. Right click on the source package node in the project and create a new class called HelloSpringController as shown below. Its not good practice to create classes without a package, but this is a trivial example so we'll proceed bearing that warning in mind.
The contents of the HelloSpringController.java class are shown below.
The first important thing to note about this class are that it implements Controller. This interface specifies that the class must implement a handleRequest method. The implementation of this method provides the main work for the controller. This method returns a ModelAndView. The parameters to the contruction of the ModelAndView specify the view ("welcome.jsp") and the model (the variable name) to be returned to the view. Thats all there is to this simple example. The handleRequest method gets the user inputted name and returns it to a jsp page for display.
Build and Run
Since we've created all of this application within NetBeans 5, its very easy to run the application. Simply right click on the "index.jsp" file in the project explorer and select "Run File". If you've typed everything in correctly, the application should be built and deployed to the embedded Tomcat within NetBeans. After a few moments you should see a browser displayed as shown in the following screen shots.
That's the end of this brief tutorial on how to create Spring MVC applications using NetBeans 5. I hope I've showed you how easy it can be to create a Spring MVC application and I hope you're interested to learn more. This tutorial only covers the bare basics of Spring MVC. You can find out a lot more by visiting http://www.springframework.org.
You can discuss this article here.