Simple Spring MVC Annotation Example Step by Step.

Simple Spring MVC Annotation Example explain Step by Step Procedure.

Tools and Technologies used in this Example.

1. Eclipse
2. Java 7
3. Spring 3.2

Steps:
1.Create Dynamic Web Project by using Eclipse and all jar to lib folder.
   Jar files List.
   spring-web-3.2.0.RELEASE.jar
   spring-webmvc-3.2.0.RELEASE.jar
   spring-beans-3.2.0.RELEASE.jar
   spring-context-3.2.0.RELEASE.jar
   spring-core-3.2.0.RELEASE.jar
   spring-expression-3.2.0.RELEASE.jar  and 
   commons-logging-1.1.1.jar

 Project Explorer is 





2. Configure web.xml file in the following.

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_3_0.xsd" id="WebApp_ID" version="3.0">

 <display-name>Spring MVC</display-name> 
  <listener>
    <display-name>contextLoaderListener</display-name>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>

3. Create Controller By using Annotations

MyController.java



package com.javafws.blog;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value="/main")
public class MyController {

@RequestMapping(value="/hello")
public String hello(){

System.out.println("We are in hello()");
return "display";
}

@RequestMapping(value = "/welcome")
public String sayWelcome(Model model){

                // To send message to JSP view from Controller
model.addAttribute("msg","Welcome To Spring MVC with Annotation Example");
return "welcome";
}
}


4. Create Spring Configuration files

applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">



</beans>



spring-servlet.xml


<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:annotation-config/>
<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans. For example @Controller and @Service. Make sure to set the correct base-package-->


<context:component-scan base-package="com.javafws.blog"/>
<!-- Configures the annotation-driven Spring MVC Controller programming model. Note that, with Spring 3.0, this tag works in Servlet MVC only! -->

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
    

</beans>




5. Create folder pages and place all jsp files in this folder.

hello.jsp

Hello

welcome.jsp


<html>
<body>

<h2>Hello : ${msg}</h2>

</body>
</html>


Finally URLs :
 1. http://localhost:8090/webmvc/main/hello.html
 2. http://localhost:8090/webmvc/main/welcome.html



Tags: ,

0 Responses to “Simple Spring MVC Annotation Example Step by Step.”

Post a Comment

Thanks for your comments

Subscribe

© 2014 Java Frameworks. All rights reserved.
Designed by Blogger