Create Simple Project Spring MVC

1.) Create your Web Spring MVC Project, I’ll Show how to create using maven command for Windows User :

  • Before that make sure you have download and install:
  • java 8 or 7
  • Eclipse Mars
  • Tomcat 8

open CMD , and your folder  project , add this command and Enter :

mvn archetype:generate -DgroupId=<Your domain name> -DartifactId=<Project Name> -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

  • My Example: mvn archetype:generate -DgroupId=com.fndong -DartifactId=HelloSpringMVC -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
  • If you check the result is : Build Success , its mean you are successfully create new web app project using maven.
  • Some info: If failed to download lib to local repository because your office Network PC have the proxy setting. To solve it by setting the setting.xml file and add in .m2 folder. You can get the setting.xml from maven Apache installer,  Enable proxy setting :

<proxies>
<!– proxy
| Specification for one proxy, to be used in connecting to the network.
|
–>
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username></username>
<password></password>
<host>1**.**.**.**</host>
<port>8080</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>

</proxies>

2.) Then , Import the HelloSpringMVC  maven project to your Eclipse Mars.

2.1) after importing , check your Build Path , some folde java are missing “src\main\java” and “src\test\java“.

  • remove the missing , and Add new “Add Folder..

proj_7_1.jpg

3) Then Check the Error.

proj_7_2

  • This is occurred because missing HttpServlet, you can add dependencies or using Tomcat Server  lib Servlet, by add the project to Target Runtimes :   Target Runtimes > select Apache Tomcat 8
  • Then The Error will disappeared.

4) Create web.xml , Map Spring MVC in /Webapp/WEB-INF/web.xml file.

  • In web.xml will map org.springframework.web.servlet.DispatcherServlet with url pattern  <url-pattern>*.htm</url-pattern>.
  • The Name of servlet in <servlet-name> tag in web.xml. Once the DispatcherServlet is initialized, it will looks for a file name [servlet-name]-servlet.xml  in WEB-INF folder of web application.

<?xml version=1.0 encoding=UTF-8?><web-app version=3.0 xmlns=http://java.sun.com/xml/ns/javaee xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd> <display-name>Archetype Created Web Application</display-name>
<display-name>Archetype Created Web Application</display-name>

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

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
</web-app>

5.) Create Spring Configuration Bean file. /Webapp/WEB-INF/mvc-dispatcher-servlet.xml

  • Description About mvc-dispatcher-servlet.xml: , mvc-dispatcher-servlet.xml configuration file, we have defined a tag <context:component-scan> . This will allow Spring to load all the components from package com.fndong.controller  and all its child packages.

<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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd>

<context:component-scan base-package=”com.fndong.controller” />

<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>

6.) Add Spring dependency in pom.xml.

  <properties>
<spring.version>4.0.1.RELEASE</spring.version>
<jdk.version>1.8</jdk.version>
</properties> 

<dependencies>

<!– Spring dependencies –>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

7.) Make sure to add Maven dependencies, right click project > properties > Deployment Assembly > Add > Maven Dependencies

maven-depency.jpg

8.) Update your index.jsp , which one button will have action to go to others page – Hello.jsp.

<!DOCTYPE html>
<html>
<head>
http://span%20style=

$(document).ready(function(){
$(“p”).mouseover(function(){
$(“p”).css(“background-color“, “yellow“);
});
$(“p”).mouseout(function(){
$(“p”).css(“background-color“, “lightgray“);
});
});

<style>
.important {
font-weight: bold;
font-size: xx-large;
}

.blue {
color: blue;
}
</style>
</head>
<body>
<p><a href=”hello.htm“>Say Hello…</a></p></body>

9.) Hello.jsp ( for the second Page)

<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″
pageEncoding=”ISO-8859-1“%>
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd“>
<html>
<head>
text/javascript” src=”jquery-1.2.6.min.js“>
<title>Good Job</title>
</head> <body> <center>
<h2>Great To see You Man, Good Job</h2>
<h4>${message}</h4>
</center></body></html>

10.) Java Controller , HelloController.java.

package com.fndong.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {
String message = “Good Job Man !“;

@RequestMapping(“/hello“)
public ModelAndView showMessage() {
return new ModelAndView(“hello“, “message“, message);
}
}

11.) OUTPUT, Add to Server (deploy) Tomcat 8, then start the server.

say_hello.jpg

12) click Say Hello …,

result_21.jpg

13.) Full Source Code Project :

full_project_hello_world_spring.jpg

14.) Download This Project from my GitHub by click Download button below:

download

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s