|
валидация jsr 303
#38972865
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
|
Нужно сделать валидацию данных при помощи jsr 303
Сейчас при заполнении любых данных или пустых всегда result.hasErrors()== true. и перезагружается заново страница.
Я так понимаю надо както связать поля страницы с объектом createCourse.
Помогите !!!
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44.
public class CreateCourseDTO {
@NotEmpty
private String name;
@NotEmpty
private String category;
@NotEmpty
private String description;
@NotEmpty
private String links;
public CreateCourseDTO() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLinks() {
return links;
}
public void setLinks(String links) {
this.links = links;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24.
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String createCourse(Model model, HttpSession session,
HttpServletRequest request) {
model.addAttribute("eMail", session.getAttribute("eMail"));
if (session.getAttribute("name") == null) {
return "redirect:/login";
} else {
model.addAttribute("eMail", session.getAttribute("eMail"));
User user = userService.read(session.getAttribute("name")
.toString());
if (user.getRole().equals("Lector"))
{model.addAttribute("abilityCreate", "true");
model.addAttribute("ListCourses", courseService.getAll());
}else
model.addAttribute("abilityCreate", "false");
model.addAttribute("createCourse", new CreateCourseDTO());
model.addAttribute("listCategories", categoryService.getAll());
return "create";
}
}
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39.
@RequestMapping(value = "/create", method = RequestMethod.POST, params = {
"titleField", "descriptionField", "linksField", "categoryField" })
public String createCoursePost(Model model, HttpSession session,
HttpServletRequest request, @Valid CreateCourseDTO createCourse, BindingResult result ) {
model.addAttribute("eMail", session.getAttribute("eMail"));
String title = request.getParameter("titleField");
String description = request.getParameter("descriptionField");
String links = request.getParameter("linksField");
String category = request.getParameter("categoryField");
if (result.hasErrors()) {
model.addAttribute("eMail", session.getAttribute("eMail"));
User user = userService.read(session.getAttribute("name")
.toString());
model.addAttribute("listCategories", categoryService.getAll());
model.addAttribute("abilityCreate", "true");
return "create";
} else {
model.addAttribute("eMail", session.getAttribute("eMail"));
Course course = new Course();
course.setName(title);
course.setDescription(description);
course.setLinks(links);
course.setCategory(category);
course.setState("Draft");
course.setOwner(session.getAttribute("eMail").toString());
course = courseService.create(course);
return "redirect:/courses";
}
}
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="./css/style.css"/>
</head>
<body>
<div class="container">
<header>
<h1>
Create Course
<div class="logout">
<span id="currentUserLogin">
${eMail}
</span>
<a href="logout.html">
<i class="icon-off"></i>
</a>
</div>
</h1>
</header>
<#if abilityCreate=="false" >
<div class="alert alert-error">
<div>
<strong>Okay, Houston, we've had a problem here.</strong>
</div>
<ul>
<li>Action not allowed. Say again please.</li>
</ul>
</div>
<#elseif abilityCreate=="true">
<form class="form-horizontal" commandName="createCourse" method=POST>
<fieldset>
<div class="control-group">
<label class="control-label">Name</label>
<div class="controls">
<input id="titleField" name="titleField" class="span5" type="text"/>
</div>
</div>
<div class="control-group">
<label class="control-label">Category</label>
<div class="controls">
<select id="categoryField" name="categoryField" class="span5">
<option></option>
<#list listCategories as category>
<option>${category.category}</option>
</#list>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Description</label>
<div class="controls">
<textarea id="descriptionField" name="descriptionField" class="span5" rows="3"></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label">Links</label>
<div class="controls">
<textarea id="linksField" name="linksField" class="span5" rows="3"></textarea>
</div>
</div>
<div class="form-actions">
<button id="createButton" name="createButton" class="btn btn-primary" type="submit">Create</button>
</div>
</fieldset>
</form>
</#if>
<a class="btn" href="courses.html">Cancel</a>
</div>
</body>
</html>
|
|
|