Spring Mvc - Java

  • Spring MVC Lifecycle
    • Client sends request and serches File system
    • Then send to web server, reads Servelet info from web.xml
    • FrontEndController => Reads servelet name, Loads that, Creates spring context
      • Reads cstsconf-web-servelet.xml
      • Creates Service and returns response to BackEndController
    • BackEndController executes handler method and prepares view
      • Validates from DAO and Returns response to FrontEndController
    • DispatcherServelet identifies the view, prepares the response, and sends it back to client
  • Context Configuration
    • Used to locate Spring configuration files from different location and name
  • Context Hierarchy
    • Root/Parent WebApplicationContext
    • Servelet/Child WebApplicationContext
  • View Resolvers
    • urlBasedViewResolver
    • InternalResourceViewResolver
    • ResourceBundleViewResolver
    • XmlViewResolver
  • Spring Form
    • Spring MVC provides a tag library to generate bariety of UI components
    • @Controller => Spring modal access and submits data to controller
    • @ModalAttribute => Used to access spring modal in controller
      • Placed at method level
        • Methods marked are executed before handlers marked with @RequestMapping
        • Used for creating dynamic controllers
      • Placed at method parameter level
        • Used to bind the form data with bean
    • Validations
      • JSR 303 Bean Validation API standard is used
      • Types => Custom message can be displayed using ReloadableResourcebundleMessageSource
        • Standard
          • @NotEmpty
          • @Size(min=N)
          • @AssertTrue
          • @NotNull
          • @Range(max=N, min=M)
          • @Future
          • @DateTimeFormt(pattern="dd-MM-yyyy")
          • @Valid => FIre validations configured with business bean
        • Custom
          • Field
          • Cross Field
            • Steps
              • Create the annotation > Validator > Link both > Use annotation with business object class
      • First both Field validations are fired then Cross field validations, if all passed then BindingResult is free of errors and request can be passed
    • Exception Handlers
      • @ExceptionHandler(value="ExceptionName")
      • modelAndView.setViewName(ExceptionHandlerPage);
      • modelAndView.addObject("message", exception.getMessage());
  • Session Management
    • Request scope gets refreshed during each submission
    • @SessionAttributes("nameBean")
  • Spring MVC with JPA
    • cst-root-ctx.xml => Info of spring file
    • cst_jpa_spring_config.xml => Info of properties file, database, EntityManagerFactory, TransactionManager
    • cstspconf-web-servelet.xml => Info of JSP files
  • Spring Rest (Representational State Transfer)
    • Architectural style used to design Restful API
    • Similar to speing mvc except for the way in which BackEndController is created
    • HttpMessageConverters
      • @RestController => Controller + ResponseBody
        • @Controller
        • @ResponseBody => Converts return value directly into JSON/XML files, instead of a web page
      • @ResquestBody
      • @PathVariable
      • @ResponseEntity
    • @PathVariable => Used to read the parameter from URL
    • @RequestBody => Used to annotate the parameter of handler method
    • HTTP Verbs
      • Methods
        • Get => Safe
        • Post => Not Idempotent
        • Put
        • Delete
  • Spring Rest with JPA Data => Same as spring mvc
  • RestTemplate
    • Error handling is also present implicitly
    • APIs
      • getForObject(String URL, Class responseType): T
      • postForObject(String URL, Object requestBody, Class responseType): T
      • put(String URL, Object requestBody): void
      • delete(String url):void
Share: