Spring MVC configuration file

web.xml file:

The web.xml file contains the entry of DispatcherServlet for handling the requests. Keep the web.xml file in WebContent/WEB-INF directory of your application. Spring framework first initialize the DispatcherServlet and then load the application context from file [servlet-name]-servlet.xml in WebContent/WEB-INF directory.

Example:

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
   <servlet>
      <servlet-name>HelloWorld</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
 
   <servlet-mapping>
      <servlet-name>HelloWorld</servlet-name>
      <url-pattern>*.html</url-pattern>
   </servlet-mapping>
 
</web-app>

Note: The [servlet-name]-servlet.xml is the default name and WebContent/WEB-INF is the default location for application context file. If we want to use some other name or location we have to inform spring framework by adding ContextLoaderListener in web.xml file.

Example:

<web-app...>
 
<!-------- DispatcherServlet definition ----->
....
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/HelloWorld-servlet.xml</param-value>
</context-param>
 
<listener>
   <listener-class>
      org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>
</web-app>

[servlet-name]-servlet.xml:

Spring framework loads the application context from [servlet-name]-servlet.xml file. It is used to create or override the beans definitions. The context:component-scan tag is used to activate Spring MVC annotation scanning. The InternalResourceViewResolver is used to define the rules to resolve the view names.

Example:

<?xml version="1.0" encoding="UTF-8"?>  
<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"
   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">
 
   <context:component-scan base-package="com.w3spoint.business" />
 
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>
 
</beans>

Controller:

A controller is responsible for executing the specific functionality for a request. The @Controller annotation is used define a class as Spring MVC controller. The @RequestMapping annotation is used to map a request URL. A request URL can be mapped to an entire class or a particular method.

Example:

@Controller
public class HelloController {
	   @RequestMapping("/sayHello")  
	   public ModelAndView sayHello() {
	      String message = "Spring MVC Hello World Example.";
	      return new ModelAndView("helloWorld", "message", message);  
	   }
}
Please follow and like us:
Content Protection by DMCA.com