This post has been migrated to http://www.thinkcode.se/blog/2009/04/06/how-can-i-create-a-simple-web-application-using-spring-mvc
I want to to create a very simple web application using Spring. I want to create it using Maven as build tool.
Start with the usual Maven directory setup:
example |-- pom.xml `-- src |-- main | |-- java | | `-- se | | `-- somath | | `-- experimental | | `-- controller | | `-- SimpleFormController.java | `-- webapp | |-- index.html | `-- WEB-INF | |-- jsp | | `-- landingPage.jsp | |-- SimpleSample-servlet.xml | `-- web.xml `-- test `-- java `-- se `-- somath `-- experimental `-- controller `-- SimpleFormControllerTest.java
Let’s start with the content of the Maven pom:
This is a fairly standard pom. The dependency list isn’t anything exotic. We want JUnit, we always do. And we need spring-webmvc since this is the framework we want to use.
The next step is to develop a simple controller, connect it and create a form to call it from and a landing page to display the result.
Let’s start with a test for the controller:
This is a simple JUnit test. The only exotic here might be the annotation
@Test that is used to indicate that this is a JUnit test that should be verified every time we build the project with Maven. We verify that a model contains some expected values and that the return value from the controller is the name of a landing page. In this case it will be a jsp.
The next step would be to compile the project with
This will of course fail, we haven’t implemented the controller. Let’s do that in:
The things to notice here are the annotations
- @RequestMapping(method = RequestMethod.GET)
- @RequestMapping(method = RequestMethod.POST)
@Controller annotation will indicate to Spring that this is a controller, the C in a Model View Controller, MVC, framework.
@RequestMapping("/form") indicates that this controller should be called when somebody sends a request to web application with the name indicated in the pom earlier,
MavenSpringWeb-1.0-SNAPSHOT/aServletName/form. AservletName isn’t defined yet, it will be defined soon in
@RequestMapping(method = RequestMethod.GET) and
@RequestMapping(method = RequestMethod.POST) indicates which method that should be called when a get or a post is received. To keep things simple, I just forward all calls to post to get and forget about them.
The get method doesn’t really do anything interesting. This is where you probably would place a call to your model, perform some computing and then return the control to the method and view some kind of answer to the user. All I do is mirror two parameters and instruct that the view to show the user should be something called ‘landingPage’.
Ok, we have a test and we have some production code. Now we want to make sure that this can be used as a part of a web application. Lets start with defining a
web.xml so we get a servlet that will help us to forward any calls to our controller. Let’s define it in
All we do here is stating that we want a servlet bundled with Spring to be used as a dispatcher,
Then we map this servlet so it catches any call to /SimpleSample with the row:
Spring requires that we define a file called
ServletName-servlet.xml which will be
SimpleSample-servlet.xml in our case. Define it in
The things to note here are:
- A view technology
- Suffix for the landing pages
- Prefix for the landing pages
We will use jsp as view technology. This could be a number of other technologies, but jsp will be good enough in this case.
The prefix defines a location where we should locate our jsp pages and the suffix indicates that they will be called .jsp.
So now we have a test, a controller, we define a servlet that will dispatch calls to the controller and we have defined a way to display the result. Let’s create a jsp page that will be used to view the result. Create it in:
The two first rows will define variables for Intellij Idea, it is only there as a convenience for Idea users. The two rows that may be a bit interesting are:
But the only thing we do here is to expand two variables so we can see the values we sent to the controller.
Finally we want to call the web application. This could be done from a browser. But it will be a bit easier if we define a web page with a form. This could be the subject for some automated testing using a tool like Selenium, but that is a bit out of scope today. Let’s create a simple form in:
If we build and deploy the web application, we should be able to see that what ever name we entered in the form will show up on the landing page.
Build the project with Maven:
mvn clean install
Deploy the result
on a servlet container and try to access is with a url similar to http://localhost:8080/MavenSpringWeb-1.0-SNAPSHOT/
And the result should look something like this: