com.mycompany.controllers.MainController.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.controllers.MainController.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.mycompany.controllers;

import com.mycompany.model.Klub;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author nowickik
 */
@Controller
public class MainController {

    @RequestMapping({ "/home", "/" })
    public String homePage(Model model) {

        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml");
        SessionFactory factory = cfg.buildSessionFactory();

        //creating session object  
        Session session = factory.openSession();
        List<Klub> clubList = session.createCriteria(Klub.class).list();
        model.addAttribute("clubList", clubList);
        session.close();
        factory.close();

        return "/home_view";
    }

    @RequestMapping({ "/login" })
    public String loginPage(Model model) {

        return "/login_view";
    }

    @RequestMapping({ "/loginSuccess" })
    public String loginPage2(Model model) {

        return "/login_success_view";
    }

}