Bean Validation - New Article
Modify Article.java as shown below.
Article.java
private Integer articleNo;//Change int to Integer @Size(min=2,max=10,message="The board code must be between 2 and 10 characters.") private String boardCd; @Size(min=1,max=60,message="The title must be between 1 and 100 characters.") private String title; @Size(min=1,message="The content must be at least two characters.") private String content; //omit.. public Integer getArticleNo() { return articleNo; } public void setArticleNo(Integer articleNo) { this.articleNo = articleNo; }
BbsController.java
@RequestMapping(value="/write", method=RequestMethod.GET) public String writeForm(String boardCd, Model model) { String boardNm = boardService.getBoardNm(boardCd); model.addAttribute("boardNm", boardNm); model.addAttribute("article", new Article()); return "bbs/write"; } @RequestMapping(value="/write", method=RequestMethod.POST) public String write(@Valid Article article, BindingResult bindingResult, Model model, MultipartHttpServletRequest mpRequest, Principal principal) throws Exception { if (bindingResult.hasErrors()) { String boardNm = boardService.getBoardNm(article.getBoardCd()); model.addAttribute("boardNm", boardNm); return "bbs/write"; } //article.setEmail(principal.getName()); boardService.addArticle(article); //..omit.. return "redirect:/bbs/list?page=1&boardCd=" + article.getBoardCd(); }
write.jsp
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %> <sf:form id="writeForm" action="write" method="post" enctype="multipart/form-data" modelAttribute="article" 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="searchWord" value="${param.searchWord }" /> <sf:errors path="*" cssClass="error" /> <table id="write-form"> <tr> <td>Title</td> <td> <sf:input path="title" style="width: 90%" /><br /> <sf:errors path="title" cssClass="error" /> </td> </tr> <tr> <td colspan="2"> <sf:textarea path="content" rows="17" cols="50" /><br /> <sf: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="List" onclick="goList()" /> <c:if test="${not empty param.articleNo }"> <input type="button" value="Detailed view" onclick="goView()" /> </c:if> </div> </sf:form>
Test
On the new post screen, click the Submit button without typing anything.
References
- Getting started with Hibernate Validator
- Validation - Empty int field
- http://stackoverflow.com/questions/14715248/simple-springmvc-3-login-doesnt-work
- http://stackoverflow.com/questions/6227547/spring-3-validation-not-working
- http://stackoverflow.com/questions/8909482/spring-mvc-3-ambiguous-mapping-found
- spring-framework-reference.pdf
- MyBatis Getting started