controller
@Controller public class BoardController { @Autowired private BoardServiceimpl boardService; @GetMapping("/") public String registerForm(Board board,Model model) throws Exception{ List<Board> boardlist = boardService.listboard(board); model.addAttribute("boardlist",boardlist); return "register"; } @PostMapping("/register") public String register(Board board,Model model) throws Exception{ boardService.insertboard(board); List<Board> boardlist = boardService.listboard(board); model.addAttribute("boardlist",boardlist); return "register"; } @RequestMapping(value="/writer") public String writerForm(Board board) throws Exception{ return "writer"; } @GetMapping("/delete") public void deleteForm(Long boardNo, HttpServletRequest request) throws Exception{ System.out.println(boardNo); boardService.deleteboard(boardNo); System.out.println(boardNo); }
}
Service
@Service public class BoardServiceimpl { @Autowired private BoardRepository boardRepository; public List listboard(Board board) throws Exception{ List<Board> boardlist = boardRepository.findAll(); return boardlist; } public void insertboard(Board board) throws Exception{ boardRepository.save(board); } @Transactional public void deleteboard(Long boardNo) throws Exception{ boardRepository.deleteById(boardNo); } }
-board
@Getter
@Setter
@EqualsAndHashCode(of="boardNo")
@ToString
@Entity
@Table(name="board")
public class Board {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long boardNo;
@Column(length=300)
@NonNull
private String title;
@Column(length=300)
@NonNull
private String writer;
@Column(length=500)
@NonNull
private String content;
private LocalDateTime regDate;
}
html
<html xmlns:th = "http://www.thymeleaf.org"> <head> <meta charset="utf-8"> <title>Board</title> <script src = "/js/jQuery-3.6.0.min.js"></script> <script> $(document).ready(function(){ var objform = $("#inner"); console.log(objform); $("#remove").on("click",function(){ objform.attr("action","/delete"); objform.submit(); }); });`` </script> </head> <body> <form id = "inner" th:object="${board}" action="/delete"> <input type ="hidden" name="boardNo" th:field="*{boardNo}" /> <table align="center" border="1" width="80%"> <tr height="10" align="center" bgcolor="skyblue"> <th width="30">글번호</th> <th width="70">작성자</th> <th width="150">제목</th> <th width="200">내용</th> <th width="100">작성일</th> <th width="50">삭제</th> </tr> <tr align ="center" th:each="board : ${boardlist}"> <td th:text="${board.boardNo}"></td> <td th:text="${board.writer}"></td> <td th:text="${board.content}"></td> <td th:text="${board.title}"></td> <td th:text="${board.regDate}"></td> <td><button type="submit" id="remove">삭제</button></td> </tr> </table> </form> <a href = "writer.html" th:href="@{writer}">글쓰기</a> </body> </html>
Caused by: java.lang.IllegalArgumentException: The given id must not be null! at org.springframework.util.Assert.notNull(Assert.java:201) at org.springframework.data.jpa.repository.support.SimpleJpaRepository.deleteById(SimpleJpaRepository.java:166) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:289) at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137) at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121) at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:529) at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:285) at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:639) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:163) at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:138) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) ... 71 more
I don't know why it doesn't pass when I go through the html(Form).