스프링

Git-HUB : https://github.com/braverokmc79/spring_boot_demo2

 

class QuestionController

 

package net.slipp.web;

import javax.servlet.http.HttpSession;

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.DeleteMapping;
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.Question;
import net.slipp.domain.QuestionRepository;
import net.slipp.domain.User;

@Controller
@RequestMapping("/questions")
public class QuestionController {

	
	private static final Logger log = LoggerFactory.getLogger(QuestionController.class);

	@Autowired
	private QuestionRepository questionRepository;
	
	@GetMapping("/form")
	public String form(HttpSession session){
		if(!HttpSessionUtils.isLoginUser(session)){
			return "/users/loginForm";
		}
		return "/qna/form";
	}
	
	@PostMapping("")
	public String create(HttpSession session, String title, String contents){
		if(!HttpSessionUtils.isLoginUser(session)){
			return "/users/loginForm";
		}
		log.info("create(String title, String contents) {} , {} ", title, contents);
		User sessionUser =HttpSessionUtils.getUserFromSession(session);
		Question newQuestion =new Question(sessionUser, title, contents);
		
		questionRepository.save(newQuestion);
		
		return "redirect:/";
	}
	
	@GetMapping("/{id}")
	public String show(@PathVariable Long id, Model model){
		model.addAttribute("question", questionRepository.findOne(id));
		return "/qna/show";
	}
	
	@GetMapping("/{id}/form")
	public String updateForm(@PathVariable Long id, Model model){
		model.addAttribute("question", questionRepository.findOne(id));
		return "/qna/updateForm";
	}
	
	@PutMapping("/{id}")
	public String update(@PathVariable Long id, String title, String contents){
		Question question=questionRepository.findOne(id);
		question.update(title, contents);
		questionRepository.save(question);
		return String.format("redirect:/questions/%d", id);
	}
	
	@DeleteMapping("/{id}")
	public String delete(@PathVariable Long id){
		questionRepository.delete(id);
		return "redirect:/";
	}
	
	
}




 

 

updateform.html

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

<div class="container" id="main">
   <div class="col-md-12 col-sm-12 col-lg-10 col-lg-offset-1">
      <div class="panel panel-default content-main">
          {{#question}}
              <form name="question" method="post" action="/questions/{{id}}">
              <input type="hidden" name="_method"  value="PUT" >
              <div class="form-group">
                  <label for="title">제목</label>
                  <input type="text" class="form-control" id="title" name="title" value="{{title}}"/>
              </div>
              <div class="form-group">
                  <label for="contents">내용</label>
                  <textarea name="contents" id="contents" rows="5" class="form-control">{{contents}}</textarea>
              </div>
              
              <button type="submit" class="btn btn-success clearfix pull-right">수정하기</button>
              <div class="clearfix" />
          </form>
          
         
          {{/question}}
        </div>
    </div>
</div>


{{> include/footer}}

 

 

 

 

 

 

spring

 

about author

PHRASE

Level 60  머나먼나라

감사를 받기 위해서 먼저 고마움을 표시하라. -그라시안

댓글 ( 4)

댓글 남기기

작성