Spring MVC

Creating first Spring MVC Web Application using Eclipse IDE
1.Modify web.xml file.
2.Create spring-dispatcher-servlet.xml
3.Create the HelloController.java class(A controller)
4.Create the HelloPage.jsp file(A View).

1.Modify web.xml file.
<?xml version="1.0" encoding= "UTF-8"?>
<web-app id= "WebApp_ID" 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> FirstSpringMVCProject</display-name >
   <servlet >
      <servlet-name >spring-dispatcher</ servlet-name>
        <servlet-class>
                  org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
  </servlet >
  <servlet-mapping >
       <servlet-name> spring-dispatcher</servlet-name >
        <url-pattern> /</ url-pattern>
  </servlet-mapping >
   
</web-app>
2.Create spring-dispatcher-servlet.xml
<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-2.5.xsd" >
<bean id="HandlerMapping"  class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
 <bean name="/welcome.html" class="com.om.HelloController" />
 <bean id="viewResolver"
          class ="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix" >
            <value> /WEB-INF/</value >
        </property>
        <property name="suffix" >
            <value> .jsp</value >
        </property>
 </bean >

</beans>
3.Create the HelloController.java class(A controller)
package com.om;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloController extends AbstractController{
       @Override
       protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
            ModelAndView modelandview = new ModelAndView("HelloPage");
             modelandview.addObject("welcomeMessage" , "Hi User, welcome to the first Spring MVC Application");
            
             return modelandview ;
      }
}
4.Create the HelloPage.jsp file(A View).
<html>
<body>
       <h1> First Spring MVC Application Demo</h1 >
       <h2> ${welcomeMessage}</h2 >
</body>
</html>

O/P:- 

First Spring MVC Application Demo

Hi User, welcome to the first Spring MVC Application

Writing an annotation based controller class -@Controller, @RequestMapping

1.Modify web.xml file.
<?xml version="1.0" encoding= "UTF-8"?>
<web-app id= "WebApp_ID" version ="3.0"..........>
  <display-name> FirstSpringMVCProject</display-name >
   <servlet >
      <servlet-name >spring-dispatcher</ servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     </servlet >
  <servlet-mapping >
       <servlet-name> spring-dispatcher</servlet-name >
        <url-pattern> /</ url-pattern>
  </servlet-mapping >
</web-app>
2.Create spring-dispatcher-servlet.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd" >

 <context:component-scan base-package= "com.om" />
 <mvc:annotation-driven />
  <bean id="viewResolver"
          class ="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix" >
            <value> /WEB-INF/</value >
        </property>
        <property name="suffix" >
            <value> .jsp</value >
        </property>
 </bean >

</beans>
3.Create the HelloController.java class(A controller)
package com.om;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController
{
      @RequestMapping("/welcome" )
       public ModelAndView helloWorld() {

            ModelAndView model = new ModelAndView("HelloPage");
             model.addObject("msg" ,"hello world" );

             return model ;
      }
}
4.Create the HelloPage.jsp file(A View).
<html>
<body>
       <h1> First Spring MVC Application Demo</h1 >
       <h2> ${msg}</h2 >
</body>
</html>

O/P:- 

First Spring MVC Application Demo

hello world

No comments:

Post a Comment