Monday, 5 October 2015

How to configuration Spring MVC Application

1. Create a dynamic project with maven
    











2. Add dependency inside the pom for spring core + Spring web+ Spring MVC
     
        <!-- Below three jars are for spring IOC -->
<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>


3. Define Front Controller inside web.xml (DispatcherServlet)

    <servlet>
<servlet-name>bas-mvc</servlet-name>
<servlet-class>
         org.springframework.web.servlet.DispatcherServlet
         </servlet-class>
         <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/bas-context.xml</param-value>
         </init-param>
<load-on-startup>1</load-on-startup>
</servlet>

4. Create spring web application context file inside WEB-INF folder
    name =<dispatcher-servlet name>-servlet.xml
    /WEB-INF/bas-context.xml 


5. Create the controller with annotation @Controller

 @Controller
 public class WorkingDaysController {

@Autowired
@Qualifier("BasFacultyServiceImpl")
private BasFacultyService basFacultyService;

 }


6. Add the package name of above controller inside the file
<dispatcher-servlet name>-servlet.xml
         /WEB-INF/bas-context.xml 
<!-- Scanning controller for annotation for spring mvc --> 
<context:component-scan base-package="com.bas.common.web.controller"/>
<context:component-scan base-package="com.bas.employee.web.controller"/>
<context:component-scan base-package="com.bas.admin.web.controller"/>

Step-7
Defining the view resolver as shown below..........

<!-- Configures the annotation-driven Spring MVC Controller programming 
model. Note that, with Spring 3.0, this tag works in Servlet MVC only! This 
is annotation is used to generate xml and JSON response and also enable some 
special annotation in Spring SMV like @RequestBody, @ResponseRequest etc -->
<mvc:annotation-driven />

<!-- Declare a view resolver XMLViewResolver -->
<!-- The InternalResourceViewResolver will have rules defined to resolve 
the view names. -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp" p:order="1"/>

<!--  
Resolves view names based on the names declared on the declared xml location
All our Jasper views are declared inside the specified xml location.
Take note of the ordering of this ViewResolver.
-->
<bean class="org.springframework.web.servlet.view.XmlViewResolver"

p:location="/WEB-INF/jasper-views.xml"  p:order="0" />

Spring mvc flow....




























Spring Basic Architecture:-


No comments:

Post a Comment