, ,

Spring Web MVC 4 easy tutorial

July 12, 2014

Spring is a java based framework that has a lot of features to offer, if you’re new to spring go to Spring Wiki. You can visit their documentations here Spring,  I bet you wont read them until you get to feel it first. I’ll create a simple web mvc for you to start with.

1. I’ll be using spring STS Version: 3.5.1.RELEASE, it is built on top of eclipse by the way. Go to File -> New -> Maven Project.

Download Spring STS. What is Maven? I won’t be discussing this tools here, just click the links to know what those are.

start maven

 

2. Locate your preferred workspace and leave the Create a simple project unchecked then hit next. Then type webapp in the Filter then choose maven-archetype-webapp.

maven webapp archetype

 

3. Fill in the Group Id, Archetype id and Package. Le’s just say group Id and package are the same and Artifact Id is the name of your project or web app just to keep it simple.

maven arche

 

 

4. You have created this project. Notice there is an error saying ‘The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path”. don’t worry we’ll fix this.

Spring MVC project tree

Spring MVC project tree

5. In figure 4 “Spring MVC project tree” you’ll find a file called pom.xml, open that then include this dependency.

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>

6. You should now see your pom.xml like this.

servlet dep

 

7. Right click the project go to Run As -> Maven install. Your project will update its dependency. Now problem fixed.

mvn install

 

8. Create java src folder. Right click the project New ->Source Folder

java src

 

9. Create Controller. Right click the src/main/java then New -> Class. Then enter the package and class name.

helloController

 

10. Now we need to add the spring mvc dependencies in our pom.xml. Spring has a lot of artifacts we only need three dependencies the core, web and webmvc. Add the below dependencies in you pom.xml and Don’t forget to build the project again! Right click the project Run As -> Maven install

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>

It should look like this.

spring deps

By the way, when you run Maven Install, it automatically download the jars and include those in the project, so you won’t need to add them manually.

 

11. Configure and integrate spring mvc in our web application. Locate the WEB-INF folder.

webinf

 

Right click WEB-INF then New -> Other ->scroll down and look for the XML -> XML File, then click Next. Create an XML file, will just name it spring-mvc-servlet.xml

12. Now we need to place all jsp files in one folder under the WEB-INF as seen in figure 11, create a folder, right click WEB-INF New -> Folder, will just name it pages.

pages

 

Now we have the spring-mvc-servlet.xml and pages folder.

13. Now we need to tell spring that all transactions in our controllers located in the package com.jabahan.springmvc in step 4, will be forwarded and handled by our jsp located in the pages folder . Open spring-mvc-servlet.xml, then paste this xml.

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.jabahan.springmvc" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

14. Java doesn’t know anything about spring, we need to tell the java web that we are running a spring mvc framework in this application. First we need to tell the container what version, namespaces and schema we are using.

a.) To do this, locate the web.xml as seen in figure 12, open it then add these attributes in your <web-app> element.

<web-app id="SpringMVC" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

b.) Next, paste the below xml inside the <web-app> element

<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

your web.xml should look like this.

web

15. Create the jsp file. Right click the pages folder we did before, New -> Other -> go to Web folder then select the JSP File, will just name it myfirstcontroller.jsp. Now place this ${name} anywhere in the page you want, will explain this later.

16. Open the controller we created before (Step 9). Paste the below code.

@Controller
public class HelloController {

@RequestMapping("/myFirst")
public String anymethodname(@RequestParam(value="name")
String name, Model model) {
model.addAttribute("name", name);
return "myfirstcontroller";
}

Controller should look like this.

helloController2

 

RequestMapping /myFirst is the url, RequestParam is the parameter we can send to the controller, spring mvc will send an implicit object Model to our method. Remember the ${name} we added in the jsp? we are mapping the name parameter with the name attribute.

17. Spring STS has already a server we can use. Go to the server window.

server

 

18. Locate the server, right click then click Add and Remove.

add remove to server

 

19. Add the Spring project we just created. Highlight the project click Add > then hit finish.

add remove to server2

 

20. In the server window hit the start button.

server start

 

Wait til it finish loading all xmls and jars. Server should have a status of [Started, Synchronized]

21. Go to your favorite browser then hit this URL http://localhost:8080/HelloSpringMVC/myFirst?name=Optimus

browser

 

Congrats! You just made your first Spring MVC. :)

1 comment

  1. quickfix - July 14, 2014 4:33 pm

    something wrong with the spring-mvc-servlet.xml, for me this one works (tomcat 7): http://pastebin.com/vF9W7Arz

    Reply

Have your say