com.kalai.controller.UserListController.java Source code

Java tutorial

Introduction

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

import com.kalai.Employee;
import com.kalai.HibernateUtil;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author kalaiselvan.a
 */
@Controller
public class UserListController {

    private HibernateUtil h;
    private Employee em;
    private Session session;

    @RequestMapping("/userlist")
    public String userlist(ModelMap map, HttpSession ses) {
        String user = "";
        try {
            user = (String) ses.getAttribute("username");
            if (user != null && !user.equals("") && user.trim().length() != 0) {
                session = HibernateUtil.getSessionFactory().openSession();
                session.getTransaction().begin();
                Query qr = session.createQuery("from Employee");
                List allUsers;
                allUsers = qr.list();
                em = new Employee();
                em.setName("new name");
                em.setRole("testing");
                map.put("Person", em);
                map.put("emploee", allUsers);
                map.addAttribute("msg", "Page redirection");
                map.addAttribute("helloAgain", "Hello (Again) Spring from Netbeans!!");
                session.getTransaction().commit();
                return "userlist";
            } else {
                return "index";
            }
        } catch (Exception e) {
            session.beginTransaction().rollback();
            return "PageNotFound";
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }
    }
}