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
- Create web.xml and servlet-context.xml file in WebContent/WEB-INF folder.
- the Web.xml file contains the dispatcher servlet, and URL mapping configuration.
- the servlet-context.xml file containing component scanning, annotation-driven support, and view resolver configuration.
- 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> - Set up URL mapping for Spring MVC Dispatcher Servlet (web.xml)
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> - Add support for component scanning
<context:component-scan base-package="com.nilhartech.springdemo" />
- Add support for conversion, formatting, and validation support
<mvc:annotation-driven/>
- 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
Post a Comment