官术网_书友最值得收藏!

Key building blocks of a Spring Web MVC application

In this section, we will learn about the key building blocks of a Spring web MVC application. The following diagram represents the key building blocks:

Figure 2: Key building blocks of Spring web MVC framework

The following are the details in relation to the preceding diagram:

  • Dispatcher servlet: Dispatcher servlet, also termed the front controller, is at the core of the Spring Web MVC framework. Simply speaking, the Dispatcher servlet determines which controller class and method needs to be called when a page request or an API request arrives. In addition, it sends the response using the appropriate JSP page or JSON/XML objects. It dispatches the incoming requests to the appropriate handlers (custom controllers) with different handler mappings. This is integrated with the Spring IOC container, which allows it to use all the features that the Spring framework provides.
  • Handler Mappings: Handler mappings are used to map the request URL with the appropriate handlers such as controllers. The Dispatcher servlet uses the handler mappings to determine the controllers which will process the incoming requests. The handler mappings are specified in the XML file, or as annotations such as @RequestMapping,  @GetMapping, or @PostMapping, and so on. The following diagram represents the @RequestMapping annotation that is used for URL mapping.
  • Handler Interceptors: Handler interceptors are used to invoke preprocessing and post-processing logic before and after the invocation of the actual handler method respectively.
  • Controllers: These are custom controllers created by the developers and used for processing the incoming requests. The controllers are tagged with annotations such as @Controller or @RestController. Controllers are used to access the application behavior through one or more service interfaces. Controllers are used to interpret the user input, pass them to the services for implementation of business logic, and transform the service output into a model which is presented to the user as a view. The following diagram shows the @Controller annotation which represents the DemoApplication class to play the role of a controller:
Figure 3: Representing handler mappings (URL mapping) and Controller annotation
  • Services: These are the components coded by the developers. These components contain the business logic. One or more methods of services are invoked from within the Controller methods. Spring provides annotations such as @Service for identifying services. The following code represents the service class UserService, which consists of a method, getUsername. Pay attention to the @Service annotation. Note how the instance of UserService is defined in @Controller, as shown in the preceding code, and the method getUsername is invoked on the instance of UserService.
        import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.services.dao.UserDAO;
import com.services.domain.User;

@Service ("userService")
public class UserService {

@Autowired
private UserDAO userDAO;

public String getUsername(User user) {
return userDAO.getUsername(user);
}
}
  • Data Access Objects (DAO): The classes which represent DOA, are used to do data processing with the underlying data sources. These classes are annotated with annotations such as @Repository. The preceding code of UserService consists, a DAO, namely, UserDAO. Note the @Repository annotation used by the class, UserDAO, in the following code:
        import org.springframework.stereotype.Repository;

import com.services.domain.User;

@Repository ("userDAO")
public class UserDAO {

public String getUsername(User user) {
return "Albert Einstein";
}
}
  • View Resolvers: View resolvers are components which map view names to views. They help in rendering models in the browser based on different view technologies such as JSP, FreeMarker, JasperResports, Tiles, Velocity, XML, and so on. Spring comes with different view resolvers such as InternalResourceViewResolver, ResourceBundleViewResolver, XMLViewResolver, and others. View resolvers are used by the Dispatcher servlet to invoke the appropriate view components.
  • Views: Views are used to render the response data on the UI. They can be represented using different technologies such as JSP, Velocity, Tiles, and so on.
主站蜘蛛池模板: 恩平市| 辉南县| 嘉善县| 兴和县| 天峨县| 泰兴市| 土默特左旗| 论坛| 麻城市| 河津市| 资阳市| 珲春市| 六枝特区| 昭觉县| 广西| 安溪县| 韶关市| 宜宾市| 连州市| 平凉市| 汪清县| 阿巴嘎旗| 鹤峰县| 德保县| 临清市| 灵宝市| 濮阳县| 贵港市| 昌平区| 北辰区| 堆龙德庆县| 德令哈市| 红原县| 衡东县| 泽州县| 治多县| 河南省| 定州市| 普安县| 苍南县| 蒙自县|