Annotation SpringMvc-based framework design (1)
Recently (by the end of 2014), I have integrated the popular frameworks by myself. If you want to write a framework by yourself, refer to the following content.
1) MVC Framework (SpringMVC)
2) data pool (dbcp)
3) hibernate ing framework (hibernate)-use Hibernate for ing, and use hibernate + SpringJdbc for database operations.
4) CSS framework (bootstrap)
5) log management (slf4j + log4j) ---- The thymeleaf framework uses slf4j. Therefore, slf4j is used as the framework and lig4j logs are referenced.
6) Cache Management (oacache)
7) One AOP instance (which can be done, exception handling, log management, etc. The instance has been written and ready for use by yourself)
8) optimized the project startup time (no http) ---- no
9) Transaction Management (declarative annotation)
10) Static Page (thymeleaf)
11) File System (webservice)
----------------------------------------------------------- Split line --------------------------------------------------------------
I. annotation SpringMVC let's talk about the concept of MVC: the browser sends requests,
Core ControllerRefer to the configuration file to assign each request to different actions for processing. Before the controller sends the request to the action for processing, the request parameters are encapsulated into one
Parameters
Object, Called after obtaining the parameter object
Verification
MethodCheck, pass the object to the specified method after the action is executed, and return
Result View, Associate to a page through path parsing. 1. The core controller web program entry is on the web. xml: Write a Dispatcher (scheduling) Servlet, which is equivalent to the FilterDispatcher of struts2. It is also used as the Spring entry, all *. action requests are distributed and executed by the scheduler. The scheduler is a Servlet responsible for process control (scheduling of duties. the bean in xml is instantiated into the container.
Spring
Org. springframework. web. servlet. DispatcherServlet
ContextConfigLocation
/WEB-INF/default. xml
1
Spring
*. Action
Create a bean object in default. xml. We use annotations.
Select the scan package path. We will scan all the packages at the dao, service, and action layers.
2. After the parameter object + Result view bean is initialized, the request enters different actions based on the redirection path. Before the controller sends the request to the action for processing, the request parameters are encapsulated into a parameter object, in spring, it is a ModelAndView object (there are many spring parameter objects. Here we use ModelAndView as an example, which will be described in detail below) to replace struts2's tedious attribute encapsulation. ModelAndView = model + view, model refers to the meaning of sharing data between containers and pages (in fact, they are equivalent to the map of a parameter object). view refers to the result view to be returned after the Action is executed, of course, this view can also be a string like struts2, and then find the page through path Parsing
@ Controllerpublic class MainAction {@ RequestMapping ("/mainPage") // if you want to return a string as the result view, you need to use a modelMap as the parameter, share objects in the container public String mainPage (HttpServletRequest request, ModelMap modelMap) {String message = request. getParameter ("message"); modelMap. addAttribute ("message", message); return "main/main" ;}@ RequestMapping ("/mainPage1 ") // if you return a ModelAndView, place the shared object in ModelAndView (no path resolution at this time) public ModelAndView mainPage1 (HttpServletRequest request) {String message = request. getParameter ("message"); ModelMap modelMap = new ModelMap (); modelMap. addAttribute ("message", message); return new ModelAndView ("forward: jsp/main. jsp ", modelMap );}}You can directly use $ {message} to receive messages on the page. So far, the forwarding of a request to the end of processing has been completed, that is, the completion of a MVC. Write data pools, hibernate ing, and database operations tomorrow.