korean

Bean Validation - Modify Article

BbsController.java
@GetMapping("/modify")
public String modifyForm(@RequestParam(name="articleNo") 
    Integer articleNo, @RequestParam(name="boardCd") String boardCd, Model model) {
  
  Article article = boardService.getArticle(articleNo);
  String boardNm = boardService.getBoardNm(article.getBoardCd());

  model.addAttribute("article", article);
  model.addAttribute("boardNm", boardNm);
  
  return "bbs/modify";
}

@PostMapping("/modify")
public String modify(@Valid Article article, BindingResult bindingResult,
    Integer page,
    String search,
    Model model,
    MultipartHttpServletRequest mpRequest) throws Exception {

  if (bindingResult.hasErrors()) {
    String boardNm = boardService.getBoardNm(article.getBoardCd());
    model.addAttribute("boardNm", boardNm);
    return "bbs/modify";
  }

  String email = boardService.getArticle(article.getArticleNo()).getEmail();
  article.setEmail(email);

  boardService.modifyArticle(article);

  //..omit..

  //Modified setArticleNo(articleNo) to setArticleNo(article.getArticleNo()) in file information insert code

  search = URLEncoder.encode(search, "UTF-8");
    
  return "redirect:/bbs/view?articleNo=" + article.getArticleNo() +
    "&boardCd=" + article.getBoardCd() +
    "&page=" + page +
    "&search=" + search
}
modify.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<form:form id="modifyForm" action="modify" method="post" modelAttribute="article"
        enctype="multipart/form-data" onsubmit="return check()">
<input type="hidden" name="articleNo" value="${param.articleNo }" />
<input type="hidden" name="boardCd" value="${param.boardCd }" />
<input type="hidden" name="page" value="${param.page }" />
<input type="hidden" name="search" value="${param.search }" />
<form:errors path="*" cssClass="error" />
<table id="write-form">
<tr>
  <td>Title</td>
  <td>
    <form:input path="title" style="width: 90%" /><br />
    <form:errors path="title" cssClass="error" />
  </td>
</tr>
<tr>
  <td colspan="2">
    <form:textarea path="content" rows="17" cols="50" /><br />
    <form:errors path="content" cssClass="error" />
  </td>
</tr>
<tr>
  <td>Attach File</td>
  <td><input type="file" name="attachFile" /></td>
</tr>
</table>
<div style="text-align: center;padding-bottom: 15px;">
  <input type="submit" value="Submit" />
  <input type="button" value="Detailed view" onclick="goView()" />
</div>
</form:form>

On the post edit screen, clear all the input fields and click the Submit button.

Modify article test 1

Modify article test 2

References