Giter VIP home page Giter VIP logo

spring-interceptoreshttp's Introduction

CONTINUACION DEL CURSO SPRING & SPRING BOOT

SECCION 6 - INTERCEPTORES HTTP: Horario de acceso a clientes

1) Creacion de proyecto

- Seteamos el proyecto con las dependencias que venimos trabajando

    -> DevTools
    -> Spring Web
    -> Thymeleaf

- Creamos toda la configuracion inicial como veniamos trabajando

    -> Controller: App controller con el GET map para mostrar la vista index
    -> Creamos la vista index en el template para mostrarla despues

- Dentro del properties definimos el rango de horarios para la atencion de clientes

    -> ESTA DEFINICION DE HORARIO ES LA QUE VAMOS A INYECTAR EN EL INTERCEPTOR DE HORARIO
    PARA LUEGO DETERMINAR SI, CON EL HORARIO ACTUAL, EL USUARIO ESTA DENTRO DEL RANGO DE LA ATENCION
    DE LO CONTRARIO, VAMOS A REDIRIGIR A UN METODO ALTERNATIVO PARA ELLO.

2) Creando el interceptor horario

- Creamos el package para interceptores
- Creamos la clase que implementa HandlerInterceptor y le agregamos el @Component para que Spring lo pueda reconocer
y podamos INYECTAR luego los valores que colocamos como horario apertura y cierre en el properties

- Para poder inyectar usamos VALUES, por lo que definimos un par de atributos dentro del interceptor
para el horario de apertura y cierre

3) Registrando el interceptor en el MVC config

- Creamos la clase MvcConfig, @Configuration e implementamos el WebMvcConfigurer.
- Sobreescribimos el metodo addInterceptor con el nombre del interceptor luego de INYECTARLO con el qualifier

- OJO! VEREMOS UN ERROR
    -> Ingresamos en horario fuera del rango y nos lleva al error: "localhost te redirigió demasiadas veces"
    -> EL INTERCEPTOR SE APLICA A TODOS LOS METODOS HANDLER! AL INDEX Y AL CERRADO INCLUSIVE
    -> CLARO: Index redirige a cerrado porque esta fuera de horario OK, PERO, CERRADO TAMBIEN ESTA DENTRO
    DEL FUNCIONAMIENTO DEL INTERCEPTOR, por eso el error.

4) Para evitar el error NullPointerException (opcional)

- Agregamos la condicion para modelAndView != null && handler instanceof HandlerMethod

SECCION 7 - MANEJO DE ERRORES

1) Creando nueva aplicacion y controlador

spring-interceptoreshttp's People

Watchers

PabloLencinas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.