com.MyHistory.Controller.UsuarioController.java Source code

Java tutorial

Introduction

Here is the source code for com.MyHistory.Controller.UsuarioController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.MyHistory.Controller;

import com.MyHistory.Service.ServiceCuenta;
import com.MyHistory.Service.Message.ResponseLogin;
import com.MyHistory.Service.View.UsuarioView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Alonso
 */

@Controller
public class UsuarioController {
    @Autowired
    ServiceCuenta _LoginService;

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public ModelAndView cargarHome(HttpServletRequest pRequest) {
        System.out.println("Pasando por indexController method:GET");
        ModelAndView model = new ModelAndView("home");
        HttpSession session = pRequest.getSession();
        UsuarioView usuario = (UsuarioView) session.getAttribute("Usuario");
        if (usuario == null) {
            model.setViewName("redirect:/login.htm");
            return model;
        }
        model.addObject("user", usuario);
        return model;
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView cargarPaginaInicial() {
        System.out.println("Pasando por LoginController method:get");
        ModelAndView model = new ModelAndView();
        model.setViewName("login");
        return model;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView abrirSesion(HttpServletRequest pRequest) {
        System.out.println("Pasando por LoginController method:post");
        String nombre_usuario = pRequest.getParameter("Nombre Usuario");
        String password = pRequest.getParameter("Password");
        ModelAndView model = new ModelAndView();
        ResponseLogin response = _LoginService.loguear(nombre_usuario, password);
        if (!response.resultadoExitoso()) {
            model.setViewName("login");
            model.addObject("response", response);
            return model;
        }
        HttpSession sesion = pRequest.getSession();
        sesion.setAttribute("Usuario", response.getVistaUsuario());
        model.setViewName("redirect:/home.htm");
        return model;
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public ModelAndView cerrarSesion(HttpServletRequest pRequest) {
        HttpSession sesion = pRequest.getSession();
        sesion.invalidate();
        ModelAndView model = new ModelAndView();
        model.setViewName("redirect:/login.htm");
        return model;
    }
}