Some basic concepts about Spring MVC

Tram Ho

Spring

Concept

Spring is a framework for developing Java applications, it helps applications with high performance, easy to test and reuse code, …

Spring is organized in modules so it is easy to reuse.

Spring projects:

  • Spring MVC: is designed for building web-based applications.
  • Spring Security: provides authentication and authorization.
  • Spring Boot: framewrok develops applications quickly
  • Spring Batch: Make it easy for users to scheduling, processing.
  • Spring IO
  • Spring Cloud
  • Spring Mobile
  • Spring for Android
  • Spring Session

Spring MVC

Concept

Spring MVC is a framework designed for building web-based applications.

It follows the MVC (Model-View-Controller) pattern.

Basic operation model:

  • Model: are POJO, Service, DAO files that access database, process business
  • View: are JSP, html files …
  • Control: is the Dispatcher Controller, Handler Mapping, Controller – to handle the request.

Flow in Spring MVC:

  • Any request to web application will be sent to Front Controller (Dispatcher Servlet).
  • The Front Controller will use Handler Mapping to know which controller will process the request
  • The controller receives the request, calls the appropriate service classes to handle the request.
  • After processing is complete, the Controller will receive the model from the Service layer or the DAO layer.
  • Controller sends the received model to the Front Controller (Dispatcher Servlet).
  • The Dispatcher Servlet finds the view templates, uses the view resolver, and passes the model to it.
  • View template, model, view page are built and returned to the Front Controller
  • The Front Controller sends a page view to the browser to display it to the user.

Advantages:

  • The layers in Spring MVC are independent so unit testing is easier.
  • The view section can be integrated with many UI Frameworks such as JSF, Freemarker, Themeleaf …
  • Spring MVC base is on POJO class so its actions are quite simple
  • Supports both Annotation and XML config for faster and cleaner code development.
  • Provide a clear, flexible division between controller, service, data acces layer.

Anomations

@Controller

@Controller is the main annotation that indicates the annotated class serves as the Controller of MVC. The Dispatcher Servlet scans the classes annotated with it to map web requests to methods annotated using @RequestMapping .

@Controller inherits from the @Component annotation just like other Spring annotations, such as @Service and @Repository .

@RequestMapping

@RequestMapping to map requests to controller methods. It has different properties to match URL, HTTP method, request parameter, title, and media type. You can use it at the class level to represent shared mappings, or at the method level to narrow it down to a specific endpoint mapping.

-> Class HomeController will handle 2 URLs “/ method1”, “/ method2”.

Similar has:

  • @GetMapping
  • @PostMapping
  • @PutMapping
  • @DeleteMapping
  • @PatchMapping

@ModelAttribute

  • On a method argument in the @RequestMapping methods to create or access an Object from the model and associate it with the request via WebDataBinder.
  • Is a method-level annotation in the @Controller or @ControllerAdvice classes that initializes the model before any @RequestMapping method calls.
  • On the @RequestMapping method to mark its return value as a model attribute.
  • This section discusses the @ModelAttribute methods – the second item in the previous list. A controller can have any number of @ModelAttribute methods. All such methods are called before @RequestMapping methods in the same controller. The @ModelAttribute method can also be shared on controllers via @ControllerAdvice . See section Controller Advice for more details.
  • The @ModelAttribute method has flexible method signature. They support many of the same arguments as the @RequestMapping method, except the @ModelAttribute itself or anything related to the request body.

@InitBinder

  • The @Controller or @ControllerAdvice classes can have @InitBinder methods that initialize copies of WebDataBinder, and these methods can:
    • Bind the request parameters (that is, form or query data) to a model object.
    • Converts string-based request values ​​(such as request parameters, path variables, titles, cookies, and others) to the target type of the controller method argument.
    • Format model object values ​​as String values ​​when displaying HTML forms.
  • The @InitBinder method can register @InitBinder specific java.beans.PropertyEditor or Spring Converter and Formatter components. Alternatively, you can use the MVC configuration to register Transform and Format types in a globally shared FormattingConversionService.
  • The @InitBinder methods support many of the same arguments as the @RequestMapping methods, except for the @ModelAttribute (command object). Usually, they are declared with the WebDataBinder argument (for registration) and the value returns void.

@ExceptionHandler

The @Controller and @ControllerAdvice classes can have @ExceptionHandler methods to handle exceptions from controller methods.

Some Anomations support validating

  • @NotNull : value cannot be empty.
  • @Min : number must be equal to or greater than the specified value.
  • @Max : the number must be equal to or less than the specified value.
  • @Size : the size should be equal to the specified value.
  • @Pattern : string obeys specified regular expression. For example: email format, password must have lower case letters, …

Multi language

Spring MVC – Multilingual example code in Spring MVC, Internationliaztion-i18n

Similar to JSF Framework, Spring MVC also supports multiple languages, helping to display different languages ​​without having to hard code. I will make .properties files containing label / text for each language (English, Vietnamese, Japanese, French). Whenever you select a language, it will display the label / text of that language.

It supports the following classes:

summary

Above are some of the basic knowledge for newcomers about Java language in general and Spring MVC in particular. Hope you learn well!

Share the news now

Source : Viblo