com.test1.controller.UserSettingsController.java Source code

Java tutorial

Introduction

Here is the source code for com.test1.controller.UserSettingsController.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 com.test1.model.UserBean;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
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 UserSettingsController {

    @RequestMapping(value = "/userSettings/{urlUserId}/{actionId}", method = RequestMethod.GET)
    public ModelAndView userSettings(@PathVariable("urlUserId") int urlUserId,
            @PathVariable("actionId") int actionId, @ModelAttribute("userId") int userId) {
        ModelAndView mav = new ModelAndView("userSettings");
        try {
            UserDAO userDao = new UserDAO();
            switch (actionId) {
            case 0:
                userDao.deleteUser(urlUserId);
                mav.setViewName("redirect:/home");
                break;
            case 1:
                String username = userDao.getUser(userId).getUsername();
                if (urlUserId != -1) {
                    UserBean user = userDao.getUser(urlUserId);
                    mav.addObject("username_edit", user.getUsername());
                    mav.addObject("password_edit", user.getPassword());
                    mav.addObject("id_edit", user.getId());
                } else {
                    mav.addObject("id_edit", -1);
                }
                mav.addObject("username", username);
                break;
            default:
                ;
            }
        } catch (Exception ex) {
        }
        return mav;
    }

    @RequestMapping(value = "/submitUserUpdate", method = RequestMethod.POST)
    public ModelAndView submit(@RequestParam("userId") int userId, @RequestParam("username") String username,
            @RequestParam("password") String password) {
        System.out.println("hello");
        ModelAndView mav = new ModelAndView("redirect:home");
        try {

            UserBean user = new UserBean();
            UserDAO userDao = new UserDAO();
            if (userId == -1) {
                user.setUsername(username);
                user.setPassword(password);
                userDao.insUser(user);
            } else {
                user.setId(userId);
                user.setUsername(username);
                user.setPassword(password);
                userDao.updateUser(user);
            }
        } catch (Exception ex) {
        }
        return mav;
    }

}