3 글 보임 - 1 에서 3 까지 (총 3 중에서)
-
글쓴이글
-
2024년 7월 14일 02:25 #128117
22참가자에러상황: http://localhost:8080/edit/1까지는 조회 잘됨. 전송 누르는 순간 아래와 같은 에러가 나옵니다. 에러페이지임 404
Not Found
/edit/@%7B'/edit/'+$%7BItem.id%7D%7D
No static resource edit/@%7B'/edit/'+$%7BItem.id%7D%7D. 질문1. db에서 뽑아오는 방법을 사용해서 시도 1,2.를 서비스 단에서 작성했는데 2개 다 작동되지 않습니다. 어느 부분이 부족한지 알고싶습니다. gpt한테 물어봐도 틀린게 아니라고 하는데요.. ItemService 클래스 중 public void updatateItem 함수 중 시도1.item.id=item_temp.get().id; () 시도2.item=item_temp.get();() 질문2. 강의 6분 33초를 보면 PostMapping("/edit/{id}")에서 model.addAttribute("Item",update_id.get());를 안쓰시는데요. 화면에 보여줄 때 model.addAttribute필요하기 때문에 PostMapping에서는 항상 안사용하는건가요? 질문3. 코드 아래에 첨부합니다.
-
이 게시글은
22에 의해 11 월, 2 주 전에 수정됐습니다.
2024년 7월 14일 02:27 #128118
22참가자package com.example.shop;
import jakarta.persistence.*; import lombok.Data;
import java.time.LocalDateTime;
@Data @Entity public class Item { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) Long id;
@Column(columnDefinition="TEXT")//글자 길게 입력함. private String title; private Integer price;
public String home_title; public String home_data;
} ------------------------
package com.example.shop;
import lombok.RequiredArgsConstructor; import org.springframework.data.annotation.Id; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.Optional;
@Controller @RequiredArgsConstructor public class ItemController {
private final ItemRepository itemRepository; private final ItemService itemService;
@GetMapping("/list") // @ResponseBody return값에 데이터를 그대로 보내주세요. html사용시 없애야 함. String list(Model model){ List<Item> result=itemRepository.findAll(); model.addAttribute("items",result); Item item=new Item(); String home_title = item.home_title; System.out.println("home_title = " + home_title); System.out.println("ssssssssss = " );
return "list"; }
@GetMapping("/write") String write(){ return "write"; }
@PostMapping("/add") String addPost(@RequestParam String title, @RequestParam Integer price){ itemService.saveItem(title,price); return "redirect:/list"; }
@GetMapping("/detail/{id}") String detail(@PathVariable Long id,Model model) {
Optional<Item> result=itemRepository.findById(id); if(result.isPresent()){ model.addAttribute("data",result.get()); return "detail.html"; }else{ return "redirect:/list"; }
}
@GetMapping("/edit/{id}") String update(@PathVariable Long id,Model model) {
Optional<Item> update_id=itemRepository.findById(id); if (update_id.isPresent()){ model.addAttribute("data",update_id.get()); return "update.html"; }else{ return "redirect:/list"; } }
@PostMapping("/edit/{id}") String postupdate(@PathVariable Long id,Model model, @RequestParam String title, @RequestParam Integer price) {
Optional<Item> update_id=itemRepository.findById(id); if (update_id.isPresent()){ model.addAttribute("Item",update_id.get()); itemService.updatateItem(title,price,id); return "update.html"; }else{ return "redirect:/list"; } }
} ------
package com.example.shop;
import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service;
import java.util.Optional;
@Service @RequiredArgsConstructor public class ItemService { private final ItemRepository itemRepository;
public void saveItem(String title,Integer price){
Item item =new Item(); item.setTitle(title); item.setPrice(price); itemRepository.save(item);}
public void updatateItem(String title,Integer price,Long id){
Optional<Item> item_temp=itemRepository.findById(id);
Item item =new Item(); // item.id=item_temp.get().id; item=item_temp.get(); item.setTitle(title); item.setPrice(price); itemRepository.save(item);}
}
-------------
<!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link href="./main.css" rel="stylesheet"> </head> <body> <div th:replace="~{nav.html::navbar}"></div> <div class="card" th:each="i:${items}"> < img src="https://placehold.co/300"> <div> <h4 th:text="${i.title}">바지</h4> <a th:href="@{'/detail/'+${i.id}}">링크</a> <p th:text="${i.price}">7억</p> <a th:href="@{'/edit/'+${i.id}}">✏️</a> </div> </div>
</body> </html> ----------------------
<!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link href="./main.css" rel="stylesheet"> </head> <body> <div th:replace="~{nav.html::navbar}"></div> <!--<form action="@{"editpost"}" method="POST">--> <form action="@{'/edit/'+${Item.id}}" method="POST"> <input name="title" th:value="${data.title}"> <input name="price" th:value="${data.price}"> <button type="submit">전송</button> </form>
</body> </html>
2024년 7월 14일 09:58 #128128
codingapple키 마스터action=""안에 타임리프변수넣으려면 th:action 이런거 씁시다 model.addAttribute는 서버에서 html 보내줄 때 변수도 넣어주고 싶은경우 씁시다
-
이 게시글은
-
글쓴이글
3 글 보임 - 1 에서 3 까지 (총 3 중에서)
- 답변은 로그인 후 가능합니다.