스프링

class UserController

package net.slipp.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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("/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){
		User user =userRepository.findOne(id);
		model.addAttribute("user",user);
		return "/user/updateform";
	}
	
	
	@PutMapping("/update")
	public String update(User updateUser){
		//save() 는 기존의 아이디값이 있으면 업데이트 없으면 인서트 한다.
		userRepository.save(updateUser);
		return "redirect:/users";
	}
	
	
	
}




 

 

	@PutMapping("/update")
	public String update(User updateUser){
		//save() 는 기존의 아이디값이 있으면 업데이트 없으면 인서트 한다.
		userRepository.save(updateUser);
		return "redirect:/users";
	}

 

updateform.html

{{> include/header}}
{{> include/navigation}}

<div class="container" id="main">
   <div class="col-md-6 col-md-offset-3">
      <div class="panel panel-default content-main">
          
          {{#user}}
          <form name="question" method="post" action="/users/update">
          	  <input type="hidden" name="_method"  value="put">
              <input type="hidden" name="id" value="{{id}}">
              <div class="form-group">
                  <label for="userId">사용자 아이디</label>
                  <input class="form-control" id="userId" name="userId" value="{{userId}}"   readonly="readonly">
              </div>
              <div class="form-group">
                  <label for="password">비밀번호</label>
                  <input type="password" class="form-control" id="password" name="password"value="{{password}}">
              </div>
              <div class="form-group">
                  <label for="name">이름</label>
                  <input class="form-control" id="name" name="name" value="{{name}}">
              </div>
              <div class="form-group">
                  <label for="email">이메일</label>
                  <input type="email" class="form-control" id="email" name="email" value="{{email}}">
              </div>
              <button type="submit" class="btn btn-success clearfix pull-right">회원정보 수정</button>
              <div class="clearfix" />
          </form>
          {{/user}}
        </div>
    </div>
</div>

{{> include/footer}}

 

 

          	  <input type="hidden" name="_method"  value="put">
              <input type="hidden" name="id" value="{{id}}">

 

 

 

 

 

spring

 

about author

PHRASE

Level 60  머나먼나라

벽까지 몰아 붙이지 말라. -임어당

댓글 ( 4)

댓글 남기기

작성