Sunday, 12 August 2012

Spring MVC (Annotation Based)

Annotation Based Spring MVC

Follow the directory structure for Annotation Based Spring MVC
For Eclipse IDE

1.Create a Dynamic Web Project named it Test
2. In SRC folder create the package com.vikash.web
3.Under this create a Class UserController
4. Now come under WebContent Folder uder this create a jsp page index.jsp
5. Under WEB-INF folder create a sub-folder named JSP.
6. Under JSP folder create a jsp page named welcome.jsp

Directory Structure for this Application is as follows:
Test
    |
  SRC
       |
     com.vikash.web
          |
     HelloWorldController.java

 WebComponent
          |
   index.jsp
  WEB-INF
          |
     web.xml-> It's a deployement descriptor file contain the information about servlet mappings.
     vikash-servlet.xml-> Keeps Configuration information contains the information about spring related      configration information.
     JSP-> Contains the jsp pages.
       |
      userPage.jsp
      welcome.jsp

Now follow the following Step:
Step 1:
@Controller
Class HelloWorldController
{
 @RequestMapping(value="/userPage.htm",  method=RequestMethod.GET)
  public ModelAndView userView(HttpServletRequest request)
{
  return new ModelAndView("userPage");
}
  @RequestMapping(value="/welcomePage.htm",  method=RequestMethod.POST)
  public ModelAndView helloWorld(HttpServletRequest request, HttpSession session)
{
  String name=  request.getParameter("name");
  session.setAttribute("name",name);
  return new ModelAndView("welcome");
}
}

Step2: index.jsp- under webcomponent folder parallel to WEB-INF foler.
<%response.sendRedirect("userPage.htm");%>

 Step 3:
Create the jsp page name it userPage.jsp
<html>
<head>
<title>User Page</title>
</head>
<body>

<form action="welcomePage.htm" method="post">
Name:<input type="text" name="name"></br>
<input type="submit" value="Submit">
</body>
</html>

Step 4:
Now, create the jsp page named it welcome.jsp

<html>
<head>
<title>Welcome Page</title>
</head>
<body>
Welcome,<%=session.getAttribute("name");
</body>
</html>
Step 5:
          Add the following lines in the web.xml file

<servlet>
        <servlet-name>vikash</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>vikash</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
 <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
Step 6: Now in the vikash-servlet.xml file add the following lines

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    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">
    <!--This line is for find the main Contoller that handle the request-->
    <context:component-scan base-package="com.vikash.web"></context:component-scan>
    <!-- Declare a view resolver -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>

How to run:
Go under the webcontent folder and select the index.jsp and right click on it the select run as ->run on servler option.