簡體   English   中英

我不知道為什么 null 值

[英]I don't know why the null value

  • 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 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); } }

-木板

@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)在org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker:8821 3246945888.137) 在 org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker:java.121) 在 org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.4g8289) orspringComposition:882138889 .data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition:java.285) 在 org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport:java.Reflectiveframework.Reflectaframework.ReflectaframeInvocation.639) 在 org .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.springframe work.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)在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation:java.186) 在 org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInte 受體:java.137)... 71 更多

我不知道為什么當我通過 html(Form) go 時它沒有通過。

您的 html 中存在一些結構問題。您正在迭代boardList ,但在刪除 object 期間沒有使用其中一個boardList對象作為參數。此外,您不需要 onClick function。您可以通過<a th:href>觸發 Controller 方法<a th:href>

你應該有這種形式結構。

HTML:

<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><a th:href="@{/delete/{boardNo}(id=${board.boardNo})}"</a></td>

</tr>

CONTROLLER:

    @GetMapping("/delete/{boardNo}")
    public void deleteForm(@PathVariable(value = "boardNo") Long boardNo, Model model) throws Exception{
        System.out.println(boardNo);
         boardService.deleteboard(boardNo);
         System.out.println(boardNo);
    
    }

你應該看看這個討論——How to delete objects on a Thymeleaf th:each iteration passing an ID to the controller?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM