Giriş
Bu sınıf "Front Controller" olarak çalışır. Calls stack çıktısı şöyle
Servlet 2.5 öncesinde bu sınıf ApplicationContext nesnesini yaratır. Kodu şuna benzer.
Akış şöyle.
1. Controller'ı bul. Bu iş için HandlerMapping kullanılır.
2. Controller model ve view ismini döner
3. View'u bul. Bu iş için ViewResolver kullanılır.
4. Response dön
web.xml İçinde DispatcherServlet Tanımlama Örnekleri
Bu sınıf "Front Controller" olarak çalışır. Calls stack çıktısı şöyle
org.springframework.web.servlet.DispatcherServlet.checkMultipart(...)
org.springframework.web.servlet.DispatcherServlet.doDispatch(...)
org.springframework.web.servlet.DispatcherServlet.doService(...)
org.springframework.web.servlet.FrameworkServlet.processRequest(...)
org.springframework.web.servlet.FrameworkServlet.doPost(...)
javax.servlet.http.HttpServlet.service(...)Servlet 2.5 öncesinde bu sınıf ApplicationContext nesnesini yaratır. Kodu şuna benzer.
public class DispatcherServlet extends HttpServlet {
  ApplicationContext ctx = null;
  public void init(ServletConfig cfg){
    // 1. try to get the spring configuration document with default naming conventions
    String xml = "servlet_name" + "-servlet.xml";
    //if it was found then creates the ApplicationContext object
    ctx = new XmlWebApplicationContext(xml);
  }
  ...
}Akış şöyle.
1. Controller'ı bul. Bu iş için HandlerMapping kullanılır.
2. Controller model ve view ismini döner
3. View'u bul. Bu iş için ViewResolver kullanılır.
4. Response dön
web.xml İçinde DispatcherServlet Tanımlama Örnekleri
SpringBoot ile artık DispatcherServlet ve web.xml tanımlamaya gerek kalmadı
web.xml - Dosya ismi Olmadan
xml dosyasında org.springframework.web.servlet.DispatcherServlet servlet olarak tanıtılıyor.
Bean tanımlarının yapıldığı dosya ismi contextConfigLocation veya init-param tag ile belirtilmiyor
Örnek
Şöyle yaparız.
<servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>
    org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>*.do</url-pattern>
</servlet-mapping>
contextConfigLocation Tag
contextConfigLocation <context-param> içinde ve ya <servlet> tag'in altındaki <init-param> içinde belirtilebilir.
contextConfigLocation <context-param> içinde ve ya <servlet> tag'in altındaki <init-param> içinde belirtilebilir.
context-param Kullanımı
Örnek
Şöyle yaparız. Tüm bean'ler application-context.xml dosyasındadır.
<servlet> <servlet-name>spring-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-servlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping><context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/application-context.xml</param-value> </context-param>
Örnek 
Şöyle yaparız. Burada bean tanımları /WEB-INF/spring-servlet.xml içinde
Şöyle yaparız. Burada bean tanımları /WEB-INF/spring-servlet.xml içinde
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="..." xmlns="..." xmlns:web="..." 
 xsi:schemaLocation="..." id="WebApp_ID" version="2.5">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-servlet.xml</param-value>
  </context-param>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
   <display-name>Spring-Hibernate</display-name>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>init-param Kullanımı
Örnek Şöyle yaparız. Burada bean tanımları /WEB-INF/rest.xml içinde
<?xml version="1.0" encoding="UTF-8"?>
<web-app ...>
  <display-name>xptraining</display-name>
  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/rest.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
Örnek
Şöyle yaparız<servlet>
  <servlet-name>mvc1</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/mvc1-config.xml</param-value>
    </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>Şöyle yaparız. Diğer örnekler ile aynı sadece url-pattern olarak / yerine * kullanılıyor
<web-app>
  <!-- The front controller of this Spring Web application, responsible 
  for handling all application requests -->
  <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/config/web-application-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>example</servlet-name>
    <url-pattern>*</url-pattern>
  </servlet-mapping>
</web-app> 
Hiç yorum yok:
Yorum Gönder