Tuesday, December 30, 2014

Dispatcher servlet and first Controller

It has come to use MVC pattern, so I create first controller. It is very simple HomeController and it is manage the view component, in this cas the home.jsp page.

To take an incoming URI and find the right combination of handlers (generally methods on Controller classes) and views (generally JSPs) that combine to form the page or resource I must create the dispatcher-servlet.xml file.

The way how dispatcher-servlet works looks at the picture below:

So, the The dispatcher servlet is the bit that "knows" to call that method when a browser requests the page, and to combine its results with the matching JSP file to make an html document.

Without create dispatcher-servlet.ml I must configure in the application the web.xml file. In the web.xml file there are defines everything about the application that a server needs to know. It is placed in the /WEB-INF/ directory of the application. The <servlet> element declares the DispatcherServlet. When the DispatcherServlet is initialized, the framework try to load the application context from a file named applicationContext.xml located in /WEB-INF/conf directory. So, I've created the dispatcher-servlet.xml and the <servlet-mapping> lement in web.xml file, which  specifies what URLs will be handled by the DispatcherServlet.

for now web.xml file looks:
 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xmlns="http://java.sun.com/xml/ns/javaee"  
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
      id="WebApp_ID" version="3.0">  
      <display-name>PizzaDeliverySystem</display-name>  
      <welcome-file-list>  
           <welcome-file>home.htm</welcome-file>  
      </welcome-file-list>  
      <context-param>  
           <param-name>contextConfigLocation</param-name>  
 <!--           <param-value> /WEB-INF/conf/applicationContext.xml</param-value> -->  
           <param-value>/WEB-INF/security-context.xml</param-value>  
      </context-param>  
      <context-param>  
           <param-name>log4jConfigLocation</param-name>  
           <param-value>/WEB-INF/conf/log4j.xml</param-value>  
      </context-param>  
      <!-- <context-param> -->  
      <!-- <param-name>spring.profiles.active</param-name> -->  
      <!-- <param-value>dev</param-value> -->  
      <!-- </context-param> -->  
      <!-- <context-param> -->  
      <!-- <param-name>spring.profiles.default</param-name> -->  
      <!-- <param-value>dev</param-value> -->  
      <!-- </context-param> -->  
      <context-param>  
           <param-name>spring.liveBeansView.mbeanDomain</param-name>  
           <param-value>dev</param-value>  
      </context-param>  
     
      <listener>  
           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
      </listener>  
      <!-- <listener> -->  
      <!-- org.springframework.web.context.request.RequestContextListener -->  
      <!-- </listener> -->  
      <!-- Creates the Spring Container shared by all Servlets and Filters -->  
      <!-- The definition of the Root Spring Container shared by all Servlets   
           and Filters -->  
      <servlet>  
           <servlet-name>dispatcher</servlet-name>  
           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
           <load-on-startup>1</load-on-startup>  
           <init-param>  
                <param-name>contextConfigLocation</param-name>  
                <param-value>WEB-INF/dispatcher-servlet.xml</param-value>  
           </init-param>  
      </servlet>  
      <servlet-mapping>  
           <servlet-name>dispatcher</servlet-name>  
           <url-pattern>/</url-pattern>  
      </servlet-mapping>  
      
 </web-app>  

The dispatcher-servlet.xml file is placed in /WEB-INF/ directory. This is the file where all beans created, such as Controllers, will be placed and defined. So, the HomeController, that is the controller in application is defined here, and will be shown in next steps. The <context:component-scan> tag is used so that the container will know where to search for the classes.

My dispatcher-servlet.xml file looks:

<?xml version="1.0" encoding="UTF-8"?>  
 <beans xmlns="http://www.springframework.org/schema/beans"  
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"  
      xmlns:context="http://www.springframework.org/schema/context"  
      xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"  
      xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c"  
      xmlns:security="http://www.springframework.org/schema/security"  
      xmlns:task="http://www.springframework.org/schema/task"  
      xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd  
           http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd  
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  
           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd  
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
           http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd  
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">  
     
 <context:annotation-config />  
      <!-- Scan for annotations... -->  
      <context:component-scan base-package="com.pizza.delivery"></context:component-scan>  
      <!-- Configures the @Controller programming model -->  
      <mvc:annotation-driven />  
      <jpa:repositories base-package="com.pizza.delivery" />  
      
      </bean>  
      <bean id="viewResolver"  
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
      <property name="prefix">  
           <value>/WEB-INF/views/</value>  
      </property>  
      <property name="suffix">  
           <value>.jsp</value>  
      </property>  
 </bean>  
      
 </beans>  

The InternalResourceViewResolver maps the jsp and html files in the ?WEB-INF/ folder and it allows to set properties such as perefix or suffixto the view name to generate the final view page URL.

Finally create HomeContoller and home.jsp page. My first controller is very easy and display "Hello world" on the page and looks:

package com.pizza.delivery.controllers;  
 //import javax.servlet.http.HttpServletRequest;  
 //import javax.servlet.http.HttpServletResponse;  
 import org.springframework.stereotype.Controller;  
 import org.springframework.web.bind.annotation.RequestMapping;  
 import org.springframework.web.bind.annotation.RequestMethod;  
 import org.springframework.web.bind.annotation.RequestParam;  
 import org.springframework.web.servlet.ModelAndView;  
 @Controller  
 public class HomeController {  
      @RequestMapping(value="/home", method = RequestMethod.GET)  
      public String home(ModelAndView model) {  
           String message = "Hello World, Spring 3.0!";  
           model.addObject("message", message);  
           return "home";  
      }  
 } 

Annotation @RequestMapping is one of the most widely usedby Spring MVC annotation and is using  for mapping web requests onto specific handler classes and/or handler methods. In above method /home is the URI for which this controller will be used.This concept is very similar to servlet context of a web application.




aaaa

No comments:

Post a Comment