com.test1.controller.ProfileController.java Source code

Java tutorial

Introduction

Here is the source code for com.test1.controller.ProfileController.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.*;
import com.test1.model.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
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 ProfileController {

    @RequestMapping(value = "/profile")
    public ModelAndView profile(@ModelAttribute("userId") int userId) {
        ModelAndView mav = new ModelAndView("profile");
        try {
            UserDAO userDao = new UserDAO();
            UserBean user = userDao.getUser(userId);
            mav.addObject("username", user.getUsername());
            mav.addObject("password", user.getPassword());
        } catch (Exception ex) {
        }
        return mav;
    }

    @RequestMapping(value = "/submitProfile", method = RequestMethod.POST)
    public ModelAndView submitProfile(@RequestParam("username") String username,
            @RequestParam("password") String password, @ModelAttribute("userId") int userId) {
        ModelAndView mav = new ModelAndView("profile");
        try {
            UserDAO userDao = new UserDAO();
            UserBean user = new UserBean();
            user.setId(userId);
            user.setUsername(username);
            user.setPassword(password);
            userDao.updateUser(user);
        } catch (Exception ex) {
        }
        return mav;
    }

}