com.test1.controller.IndexController.java Source code

Java tutorial

Introduction

Here is the source code for com.test1.controller.IndexController.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.test1.controller;

import com.test1.dao.UserDAO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author FrancAnthony
 */
@Controller
@SessionAttributes("userId")
public class IndexController {

    @RequestMapping(value = "/index")
    public ModelAndView index() {
        return new ModelAndView("index");
    }

    @RequestMapping(value = "/loginSubmit", method = RequestMethod.POST)
    public ModelAndView login(@RequestParam("username") String username,
            @RequestParam("password") String password) {
        ModelAndView mav = new ModelAndView("index");
        try {
            UserDAO userDao = new UserDAO();
            if (userDao.findUser(username, password)) {
                mav.setViewName("redirect:home");
                mav.addObject("userId", userDao.getUser(username).getId());
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return mav;
    }

}