Java tutorial
/******************************************************************************* * 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 javax.validation.Valid; import org.apache.shiro.SecurityUtils; 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.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import cn.dsgrp.field.stock.entity.User; import cn.dsgrp.field.stock.service.account.AccountService; import cn.dsgrp.field.stock.service.account.ShiroDbRealm.ShiroUser; import java.math.BigInteger; /** * Controller. * * @author calvin */ @Controller @RequestMapping(value = "/profile") public class ProfileController { @Autowired private AccountService accountService; @RequestMapping(method = RequestMethod.GET) public String updateForm(Model model) { BigInteger id = getCurrentUserId(); model.addAttribute("user", accountService.getUser(id)); return "account/profile"; } @RequestMapping(method = RequestMethod.POST) public String update(@Valid @ModelAttribute("user") User user) { accountService.updateUser(user); updateCurrentUserName(user.getName()); return "redirect:/"; } /** * 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)); } } /** * ?Shiro?Id. */ private BigInteger getCurrentUserId() { ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal(); return user.id; } /** * Shiro???. */ private void updateCurrentUserName(String userName) { ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal(); user.name = userName; } }