class UserController
package net.slipp.web;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import net.slipp.domain.User;
import net.slipp.domain.UserRepository;
@Controller
@RequestMapping("/users")
public class UserController {
private static final Logger log = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserRepository userRepository;
@GetMapping("/loginForm")
public String loginForm(){
return "/user/login";
}
@PostMapping("/login")
public String login(String userId, String password, HttpSession session){
User user =userRepository.findByUserId(userId);
if(user==null){
log.info("login - user.null");
return "redirect:/users/loginForm";
}
if(!password.equals(user.getPassword())){
log.info("login - !password");
return "redirect:/users/loginForm";
}
log.info("login - success");
session.setAttribute("sessiondUser", user);
return "redirect:/";
}
@GetMapping("/logout")
public String logout(HttpSession session){
session.removeAttribute("sessiondUser");
return "redirect:/";
}
@GetMapping("/form")
public String form(){
return "/user/form";
}
@PostMapping("")
public String create(User user){
log.info("create : {}", user.toString());
// jpa h2 로 자동으로 인클로드 된다.
userRepository.save(user);
return "redirect:/users";
}
@GetMapping("")
public String list(Model model){
// userRepository.findAll() 목록 가져오기
model.addAttribute("users", userRepository.findAll());
return "/user/list";
}
@GetMapping("/{id}/form")
public String updateForm(@PathVariable Long id, Model model, HttpSession session){
User sessiondUser=(User)session.getAttribute("sessiondUser");
if(sessiondUser==null){
return "redirect:/";
}
if(!sessiondUser.getId().equals(id)){
new IllegalStateException("You can't update the anther user");
return "redirect:/";
}
log.info("update form {} ", id);
User user =userRepository.findOne(id);
model.addAttribute("user",user);
return "/user/updateform";
}
@PutMapping("/update")
public String update(User updateUser, HttpSession session){
User sessiondUser=(User)session.getAttribute("sessiondUser");
if(sessiondUser==null){
return "redirect:/";
}
if(!sessiondUser.getId().equals(updateUser.getId())){
new IllegalStateException("You can't update the anther user");
return "redirect:/";
}
//save() 는 기존의 아이디값이 있으면 업데이트 없으면 인서트 한다.
userRepository.save(updateUser);
return "redirect:/users";
}
}
@GetMapping("/{id}/form")
public String updateForm(@PathVariable Long id, Model model, HttpSession session){
User sessiondUser=(User)session.getAttribute("sessiondUser");
if(sessiondUser==null){
return "redirect:/";
}
if(!sessiondUser.getId().equals(id)){
new IllegalStateException("You can't update the anther user");
return "redirect:/";
}
log.info("update form {} ", id);
User user =userRepository.findOne(id);
model.addAttribute("user",user);
return "/user/updateform";
}
@PutMapping("/update")
public String update(User updateUser, HttpSession session){
User sessiondUser=(User)session.getAttribute("sessiondUser");
if(sessiondUser==null){
return "redirect:/";
}
if(!sessiondUser.getId().equals(updateUser.getId())){
new IllegalStateException("You can't update the anther user");
return "redirect:/";
}
//save() 는 기존의 아이디값이 있으면 업데이트 없으면 인서트 한다.
userRepository.save(updateUser);
return "redirect:/users";
}
navigation.html
<div class="collapse navbar-collapse" id="navbar-collapse2">
<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="../index.html">Posts</a></li>
{{^sessiondUser}}
<li><a href="/users/loginForm" role="button">로그인</a></li>
<li><a href="/users/form" role="button">회원가입</a></li>
{{/sessiondUser}}
{{#sessiondUser}}
<li><a href="/users/logout" role="button">로그아웃</a></li>
<li><a href="/users/{{id}}/form" role="button">개인정보수정</a></li>
{{/sessiondUser}}
</ul>
</div>
댓글 ( 4)
댓글 남기기