Step 1:
Open your Eclipse IDE, create a new Project under Dynamic Web Project
Step 2:
Download the jar file
1. commons-io-1.2.jar
2. commons-logging-1.1.1.jar
3. jstl-1.2.jar
4. log4j-1.2.14.jar
5. servlet-2.3.jar
6. slf4j-api-1.5.6.jar
7. slf4j-log4j12-1.5.6.jar
8. spring-asm-3.0.3.RELEASE.jar
9. spring-beans-3.0.3.RELEASE.jar
10.spring-context-3.0.3.RELEASE.jar
11.spring-core-3.0.3.RELEASE.jar
12.spring-expression-3.0.3.RELEASE.jar
13.spring-web-3.0.3.RELEASE.jar
14.spring-webmvc-3.0.3.RELEASE.jar
15.validation-api-1.0.0.GA.jar
Now create lib folder under WEB-INF folder and copy all the jar files inside lib folder
Now add these jar file to the project.
Step 3:
1. Create index.jsp inside WebContent folder.
2. Create hello.jsp inside WEB-INF/views/ folder.
3. Create dispatcher-servlet.xml under WEB-INF folder.
4. Create package name controller and create class HelloWorldController.java inside package controller.
for more detail refer below figure.
index.jsp
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 3, MVC Examples</title>
</head>
<body>
<h1>Spring 3, MVC Examples</h1>
<ul>
<li><a href="views/hello.html">Hello World</a></li>
</ul>
</body>
</html>
hello.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
<context:component-scan base-package="controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/views/*</url-pattern>
</servlet-mapping>
</web-app>
HelloWorldController.java
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping({"/hello"})
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
System.out.println(message);
return new ModelAndView("hello", "message", message);
}
}
Comments
Post a Comment