How to configure spring MVC using xml configuration

The web browser sends the request to the front controller. the front controller is known as a dispatcher servlet. dispatcher servlet will redirect the request to the appropriate controller. the Controller will perform some operation to add or get the data into the model to fulfill requirements and return the view to display on the web browser.



Spring MVC configuration steps

  1. Create web.xml and servlet-context.xml file in WebContent/WEB-INF folder.
    1. the Web.xml file contains the dispatcher servlet, and URL mapping configuration.
    2. the servlet-context.xml file containing component scanning, annotation-driven support, and view resolver configuration.
  2. Configure Spring MVC dispatcher servlet. (web.xml)
    
    <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
  3. Set up URL mapping for Spring MVC Dispatcher Servlet (web.xml)
    
    <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
  4. Add support for component scanning
    
    <context:component-scan base-package="com.nilhartech.springdemo" />
    
    
  5. Add support for conversion, formatting, and validation support
    
    <mvc:annotation-driven/>
    
    
  6. Define Spring MVC view resolver
    
    <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
    </bean>

Comments

Popular posts from this blog

Coding Problem 3 - Weather Observation Station 7 [HackerRank - SQL]

public static void main(String [] args) - Full Explanation

Primitive Data Types in java