Three-Tier Architecture REVIEW

  • Presentation Layer
    • Spring MVC
  • Business Layer
    • Spring
  • Persistence Layer
    • Mybatis

What the presentation layer needs to do:

  1. Receive requests
  2. Return responses

MVC REVIEW

  • Model
    • java bean
  • View
    • jsp || HTML
  • Controller
    • servlet

Advantages of Spring MVC

Clear role separation

  • Front Controller (DispatcherServlet)
  • Mapping from requests to handlers (HandlerMapping)
  • Handler adapter (HandlerAdapter)
  • View resolver (ViewResolver)
  • Handler or page controller (Controller)
  • Validator
  • Command object (the object that request parameters are bound to is called a command object)
  • Form object (the object provided for form display and submission is called a form object)

Execution Flow

  1. Client – Request -> DispatcherServlet
  2. DispatcherServlet – Request for Handler -> HandlerMapping
  3. HandllerMapping find the Method position
  4. HandlerMapping – Response an executable chain -> DispatcherServlet
  5. DispatcherServlet – Request for execute -> HandlerAdapter
  6. HandlerAdapter – Execute -> Handler/Controller
  7. Handler/Controller – Response Model and View -> HandlerAdapter
  8. HandlerAdapter – Response Model and View -> DispatcherServlet
  9. DispatcherServlet – Request for view resolve -> ViewResolver
  10. ViewResolver – Response the View -> DispatcherServlet
  11. DispatcherServlet – Apply Colors to a Drawing -> View
  12. DispatcherServlet – Response -> Client

Common Annotations

@RequestParam

@RequestMapping("/Path")
public String method(@RequestParam(name = "nameInRequest") String username) {
    ...
}

@RequestBody

// get 不适用;异步时传json有用
// 获得类似("username=name&age=20")
@RequestMapping("/Path")
public String method(@RequestBody String body) {
    ...
}

@PathVariable

// REST 风格
@RequestMapping("/Path/{id}")
public String method(@PathVariable(name = "id") String id) {
    ...
}

@RequestHeader

// 获得请求头的值
@RequestMapping("/Path")
public String method(@RequestHeader(value = "HeaderName") String header) {
    ...
}

@CookieValue

// 获得cookie
@RequestMapping("/Path")
public String method(@CookieValue(value = "CookieName") String cookie) {
    ...
}

@ModelAttribute

// 放在方法上,该方法会先执行
@ModelAttribute
public String attributeMethod(String id) {
    ...
    return attribute;
}

// 获得attribute
@RequestMapping("/Path")
public String method(Attribute attribute) {
    ...
}
// 第二种写法,利用Map
@ModelAttribute
public void attributeMethod(String id, Map<String, Attribute> map) {
    ...
    map.put(id, attribute);
}

@RequestMapping("/Path")
public String method(@ModelAttribute(value = id) Attribute attribute) {
    ...
}

@SessionAttribute

// add attribute to session
@RequestMapping("/Path")
public String setSessionAttribute(Model model) {
    model.addAttribute("key", value);
    ...
}

// get attribute from session
@RequestMapping("/path")
public String getSessionAttribute(ModelMap modelMap) {
    result = (String) modelMap.get("key");
    ...
}

// delete attribute from session
@RequestMapping("/path")
public String deleteSessionAttribute(SessionStatus status) {
    status.setComplete();
    ...
}

Supplement

REST style:

restfule: for multiple methods, the request address is the same, and different functions are implemented based on different request methods.


Difference Between Interceptors and Filters

Interceptors are specific to SpringMVC, while filters are part of Servlet.

  • Filters:
    • After configuring /* in url-pattern, they can intercept all resources to be accessed.
  • Interceptors:
    • They only intercept accessed controller methods. If it is jsp, html, and so on, they will not intercept.

Understanding: an interceptor is a weakened version of an interceptor.

Order of interception methods in an interceptor:

preHandle - > method - > postHandle - > jsp - > afterCompletion

If there are two interceptors (1 -> 2 -> Controller):

preHandle1 - > preHandle2 - > method - > postHandle2 - > postHandle1 - > jsp - > afterCompletion2 - > afterCompletion1