cn.dsgrp.field.stock.web.account.UserAdminController.java Source code

Java tutorial

Introduction

Here is the source code for cn.dsgrp.field.stock.web.account.UserAdminController.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.dsgrp.field.stock.web.account;

import java.math.BigInteger;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.servlet.mvc.support.RedirectAttributes;
import cn.dsgrp.field.stock.entity.User;
import cn.dsgrp.field.stock.service.account.AccountService;

/**
 * ??Controller.
 * 
 * @author calvin
 */
@Controller
@RequestMapping(value = "/admin/user")
public class UserAdminController {

    @Autowired
    private AccountService accountService;

    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {
        List<User> users = accountService.getAllUser();
        model.addAttribute("users", users);

        return "account/adminUserList";
    }

    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id") BigInteger id, Model model) {
        model.addAttribute("user", accountService.getUser(id));
        return "account/adminUserForm";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(@Valid @ModelAttribute("user") User user, RedirectAttributes redirectAttributes) {
        accountService.updateUser(user);
        redirectAttributes.addFlashAttribute("message", "" + user.getLoginName() + "?");
        return "redirect:/admin/user";
    }

    @RequestMapping(value = "delete/{id}")
    public String delete(@PathVariable("id") BigInteger id, RedirectAttributes redirectAttributes) {
        User user = accountService.getUser(id);
        accountService.deleteUser(id);
        redirectAttributes.addFlashAttribute("message", "" + user.getLoginName() + "?");
        return "redirect:/admin/user";
    }

    /**
     * RequestMapping?Model, Struts2 Preparable,?formid?User,?Form??
     * update()formidupdate.
     */
    @ModelAttribute
    public void getUser(@RequestParam(value = "id", defaultValue = "-1") BigInteger id, Model model) {
        if (id.longValue() != -1) {
            model.addAttribute("user", accountService.getUser(id));
        }
    }
}