Spring MVC – Servlet XML file | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

This image has an empty alt attribute; its file name is image-1.png

Spring Tutorial Index Page: Link

  • In Spring MVC we can load xml file 3 ways
  1. Using <servlet-name>
    Check examples from Spring MVC – Handler Mapping.
  2. Using <init-param> or config param
  3. Using <context-param>
  • In Spring MVC, XML file should be <servlet-name>-servlet.xml (Spring Configuration File) and <servlet-name> from the web.xml. Example: <servlet-name>hello</servlet-name> so xml file should be hello-servlet.xml and in this file we provide mapping b/w view and controller.
  • If we want to specify different file name then we can achieve this using below code in web.xml.
  • Using init parameters.
<init-param>
	<parameter-name>contextConfigLocation</parameter-name>
	<!-- for single file -->
	<parameter-value>/WEB-INF/spring.xml</parameter-value>
	<!-- for multiple file -->
	<parameter-value>/WEB-INF/spring.xml, classpath:resource/test.xml</parameter-value>
</init-param>
  • Using context parameters.
<context-param>
	<parameter-name>ContextConfigLocation</parameter-name>
	<!-- for single file -->
	<parameter-value>/WEB-INF/spring.xml</parameter-value>
	<!-- for multiple file -->
	<parameter-value>/WEB-INF/spring.xml, classpath:resource/test.xml</parameter-value>
</context-param>

<listener>ContextLoaderListener</listener>
  • * In case of init parameters DispatcherServlet is responsible to load data and in case of context parameters ContextLoaderListener will load xml file data.
  • Project success page location and extension configure in hello-servlet.xml
  • InternalResourceViewResolver have 2 setters, setPrefix() and setSuffix().
<beans>
	<bean name="/hello.htm" class="com.codeFactory.HelloController" />
	<bean class="InternalResourceViewResolver">
		<property name="prefix" value="/jsp/output" /> <!-- file folder -->
		<property name="suffix" value=".jsp" /> <!-- file extension -->
	</bean>
</beans>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s