For BeanNameUrlHandlerMapping, all URL patterns are relative to DispatcherServlet’s
<servlet-mapping>.
Assume Application context is "HelloWorld"
Ex 1: if bean defined as follows
<!-- Application Handlers -->
<bean name="/hello.htm" class="com.suresh.controller.HelloWorldController"/>
Then . URL is
http://localhost:8080/HelloWorld/hello.htm
Ex 2:
<!-- Application Handlers -->
<bean name="/HelloWorld/hello.htm" class="com.suresh.controller.HelloWorldController"/>
Then URL is
http://localhost:8080/HelloWorld/HelloWorld/hello.htm
The dispatcher servlet definition is
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<!-- <servlet-class>com.suresh.dispatcher.DispatcherServlet</servlet-class> -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Monday, August 20, 2012
Can we override Dispatcher Servlet?
Yes. We can extends Dispatcher servlet if we want any special functionality during initialization.
If we want to display Bean names in Web Application Context
package com.suresh.dispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import org.springframework.beans.BeansException;
import org.springframework.web.context.WebApplicationContext;
public class DispatcherServlet extends org.springframework.web.servlet.DispatcherServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println("In suresh dispatcher servlet init method");
super.init(config);
}
@Override
protected WebApplicationContext initWebApplicationContext()
throws BeansException {
// TODO Auto-generated method stub
System.out.println("In suresh dispatcher servlet WebApplicationContext method");
WebApplicationContext wac = super.initWebApplicationContext();
if(null!=wac){
System.out.println("WAC Display Name " + wac.getDisplayName());
String[] beanNames = wac.getBeanDefinitionNames();
for(String bean:beanNames){
System.out.println(" bean names " + bean) ;
}
}else{
System.out.println("Web Application context is null ") ;
}
return wac;
}
Web.exml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>com.suresh.dispatcher.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
If we want to display Bean names in Web Application Context
package com.suresh.dispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import org.springframework.beans.BeansException;
import org.springframework.web.context.WebApplicationContext;
public class DispatcherServlet extends org.springframework.web.servlet.DispatcherServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println("In suresh dispatcher servlet init method");
super.init(config);
}
@Override
protected WebApplicationContext initWebApplicationContext()
throws BeansException {
// TODO Auto-generated method stub
System.out.println("In suresh dispatcher servlet WebApplicationContext method");
WebApplicationContext wac = super.initWebApplicationContext();
if(null!=wac){
System.out.println("WAC Display Name " + wac.getDisplayName());
String[] beanNames = wac.getBeanDefinitionNames();
for(String bean:beanNames){
System.out.println(" bean names " + bean) ;
}
}else{
System.out.println("Web Application context is null ") ;
}
return wac;
}
Web.exml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>com.suresh.dispatcher.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Can you explain what happens in Dispatcher Servlet Initialization?
INFO: Initializing Spring FrameworkServlet 'dispatcherServlet'
Aug 20, 2012 12:36:47 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcherServlet': initialization started
Aug 20, 2012 12:36:47 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.web.context.support.XmlWebApplicationContext@302a0a5: display name [WebApplicationContext for namespace 'dispatcherServlet-servlet']; startup date [Mon Aug 20 12:36:47 EDT 2012]; root of context hierarchy
Aug 20, 2012 12:36:47 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml]
Aug 20, 2012 12:36:47 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.web.context.support.XmlWebApplicationContext@302a0a5]: org.springframework.beans.factory.support.DefaultListableBeanFactory@6b98e8b4
Aug 20, 2012 12:36:47 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6b98e8b4: defining beans [beanNameUrlMapping,viewResolver,/home.htm]; root of factory hierarchy
Aug 20, 2012 12:36:47 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcherServlet': initialization completed in 149 ms
Aug 20, 2012 12:36:47 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcherServlet': initialization started
Aug 20, 2012 12:36:47 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.web.context.support.XmlWebApplicationContext@302a0a5: display name [WebApplicationContext for namespace 'dispatcherServlet-servlet']; startup date [Mon Aug 20 12:36:47 EDT 2012]; root of context hierarchy
Aug 20, 2012 12:36:47 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml]
Aug 20, 2012 12:36:47 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.web.context.support.XmlWebApplicationContext@302a0a5]: org.springframework.beans.factory.support.DefaultListableBeanFactory@6b98e8b4
Aug 20, 2012 12:36:47 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6b98e8b4: defining beans [beanNameUrlMapping,viewResolver,/home.htm]; root of factory hierarchy
Aug 20, 2012 12:36:47 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcherServlet': initialization completed in 149 ms
what is the default Handler Mapping in Spring MVC?
The default handler mapping used by DispatcherServlet is BeanNameUrlHandlerMapping, which uses the bean name as the URL pattern
ex:
http://localhost:8080/home.htm
<bean name="/home.htm" class="com.suresh.controller.HelloWorldController">
</bean>
We can give multiple mappings to Bean
<bean name="/home.htm /home.html /HomePage.htm" class="com.suresh.controller.HelloWorldController">
</bean>
ex:
http://localhost:8080/home.htm
<bean name="/home.htm" class="com.suresh.controller.HelloWorldController">
</bean>
We can give multiple mappings to Bean
<bean name="/home.htm /home.html /HomePage.htm" class="com.suresh.controller.HelloWorldController">
</bean>
what is the default Application context file name for ContextLoaderListener?
If we don't specify Application context file name for ContextLoadListener will look for a Spring configuration file at /WEB-INF/applicationContext.xml
we can specify file names using context configuration parameter
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/myapp-service.xml
/WEB-INF/myapp-data.xml
/WEB-INF/myapplication-security.xml
</param-value>
</context-param>
we can specify file names using context configuration parameter
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/myapp-service.xml
/WEB-INF/myapp-data.xml
/WEB-INF/myapplication-security.xml
</param-value>
</context-param>
How to modify default Application context file name or path of Dispatcher Servlet?
By Default Dispatcher Servlet construct the name of Application context file
Default file:
servlet-name-servlet.xml
e.g. "test-servlet".xml for a servlet-name "test"
Default Path: /WEB-INF/test-servlet.xml
If we want to change this default file name or Path we can use contextConfigLocation servlet INIT parameter
Ex:
Default file:
servlet-name-servlet.xml
e.g. "test-servlet".xml for a servlet-name "test"
Default Path: /WEB-INF/test-servlet.xml
If we want to change this default file name or Path we can use contextConfigLocation servlet INIT parameter
Ex:
<servlet> <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/web-application-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
We can also send Multiple files,
<servlet> <servlet-name>spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/bean.xml, /WEB-INF/bean-service.xml, /WEB-INF/bean-dao.xml</param-value> </init-param> <load-on-startup>1</load-on-startup></servlet><servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.html</url-pattern></servlet-mapping> what is the default contextclass for dispatcher servlet?
The DispatcherServlet uses XmlWebApplicationContext, by default to load Application context bean definitions.
If you want to use another Context class, we can use "contextClass" servlet initialization parameter of DispatcherServlet.Can we define dispatcher servlet more than once in Web.xml?
A web application can define any number of DispatcherServlets. Each servlet will operate in its own namespace, loading its own application context with mappings, handlers, etc. Only the root application context as loaded by ContextLoaderListener, if any, will be shared.
Sunday, August 19, 2012
what is the default file loaded by ContextLoaderListener?
When we want to keep our beans definitions in multiple files we can use ContextLoaderListener.
This Context loader listener reads one context parameter where we specify list of files : contextConfigLocation
By default this listener look for a Spring configuration file at /WEB-INF/applicationContext.
xml
To specify multiple files
<listener>
<listener-class>org.springframework.
web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/ourapp-service.xml
/WEB-INF/ourapp-data.xml
/WEB-INF/ourapp-security.xml
</param-value>
</context-param>
The contextConfigLocation parameter is specified as a list of paths (relative to
the web application root)
This Context loader listener reads one context parameter where we specify list of files : contextConfigLocation
By default this listener look for a Spring configuration file at /WEB-INF/applicationContext.
xml
To specify multiple files
<listener>
<listener-class>org.springframework.
web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/ourapp-service.xml
/WEB-INF/ourapp-data.xml
/WEB-INF/ourapp-security.xml
</param-value>
</context-param>
The contextConfigLocation parameter is specified as a list of paths (relative to
the web application root)
Subscribe to:
Comments (Atom)