ua.aits.oblenergo_site.controller.AjaxAndFormController.java Source code

Java tutorial

Introduction

Here is the source code for ua.aits.oblenergo_site.controller.AjaxAndFormController.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 ua.aits.oblenergo_site.controller;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.context.annotation.Scope;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ua.aits.oblenergo_site.model.UserModel;

/**
 *
 * @author kiwi
 */
@Controller
@Scope("session")
public class AjaxAndFormController {

    UserModel Users = new UserModel();

    @RequestMapping(value = { "/system/ajax/check/user", "/system/ajax/check/user/" }, method = RequestMethod.GET)
    public @ResponseBody String archiveCheckUser(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        return Users.isExitsUser(request.getParameter("user_name"), request.getParameter("user_password"));
    }

    @RequestMapping(value = { "/system/login.do", "/system/login.do/" }, method = RequestMethod.POST)
    public ModelAndView login(@RequestParam("user_id") String user_id, @RequestParam("user_name") String user_name,
            @RequestParam("user_password") String user_password, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        UserModel user = Users.getOneUserFullById(user_id);
        HttpSession session = request.getSession(true);
        session.setAttribute("user", user);
        if (user.user_role == 1) {
            return new ModelAndView("redirect:" + "/system/index");
        } else {
            return new ModelAndView("redirect:" + "/system/index");
        }
    }

    @RequestMapping(value = { "/system/logout.do", "/archive/logout.do/" })
    public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession(false);
        if (session != null) {
            session.invalidate();
        }
        return new ModelAndView("redirect:" + "/system/login");
    }

@RequestMapping(value = {"/system/users/checkUserName", "/system/users/checkUserName/"}, method = RequestMethod.GET)
public @ResponseBody String ?heckUsername(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    return Users.isExitsUserName(request.getParameter("user_name"));
}

    @RequestMapping(value = "/system/users/insertdata.do", method = RequestMethod.POST)
    public ModelAndView doAddUser(HttpServletRequest request) throws SQLException, ClassNotFoundException,
            InstantiationException, IllegalAccessException, UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        String user_name = request.getParameter("user_name");
        String user_password = request.getParameter("user_password");
        String user_role = request.getParameter("user_role");
        String user_enabled = request.getParameter("user_enabled");
        String user_sections = request.getParameter("user_sections");
        Users.addUser(user_name, user_password, user_role, user_enabled, user_sections);
        return new ModelAndView("redirect:" + "/system/users");
    }

    @RequestMapping(value = "/system/users/updatedata.do", method = RequestMethod.POST)
    public ModelAndView doEditUser(HttpServletRequest request) throws SQLException, ClassNotFoundException,
            InstantiationException, IllegalAccessException, UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        String user_id = request.getParameter("user_id");
        String user_name = request.getParameter("user_name");
        String user_password = request.getParameter("user_password");
        String user_role = request.getParameter("user_role");
        String user_enabled = request.getParameter("user_enabled");
        String user_sections = request.getParameter("user_sections");
        Users.editUser(user_id, user_name, user_password, user_role, user_enabled, user_sections);
        return new ModelAndView("redirect:" + "/system/users");
    }

    @RequestMapping(value = "/system/users/deletedata.do", method = RequestMethod.POST)
    public ModelAndView doDeleteUser(HttpServletRequest request) throws SQLException, ClassNotFoundException,
            InstantiationException, IllegalAccessException, UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        String user_id = request.getParameter("user_id");
        Users.deleteUser(user_id);
        return new ModelAndView("redirect:" + "/system/users");
    }
}