Guest User

Untitled

a guest
Jul 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. package testcommandobjects
  2.  
  3. class TestController {
  4.  
  5. static final int VALID = 5
  6. static final int INVALID = 50
  7.  
  8. def resetToValid = { TestCommand cmd ->
  9. rebindAgeAndRenderResponse(cmd, true)
  10. }
  11.  
  12. def resetToInvalid = { TestCommand cmd ->
  13. rebindAgeAndRenderResponse(cmd, false)
  14. }
  15.  
  16. def gatherCommandData(cmd){
  17. [value: cmd as String,
  18. validates: cmd.validate()]
  19. }
  20.  
  21. def rebindAgeAndRenderResponse(cmd, boolean changeToValid) {
  22. def value = changeToValid ? VALID : INVALID
  23. def before = gatherCommandData(cmd)
  24. bindData(cmd, [age: value])
  25. def after = gatherCommandData(cmd)
  26. render buildOutput(before, after)
  27. }
  28.  
  29. def buildOutput(before, after) {
  30. def writer = new StringWriter()
  31. def builder = new groovy.xml.MarkupBuilder(writer)
  32. builder.html {
  33. head {
  34. title 'dataBind demo'
  35. }
  36. body {
  37. h1 'dataBind demo'
  38. h3 'valid data for age: 5..10'
  39. p "$before"
  40. p "$after"
  41. }
  42. }
  43. writer.toString()
  44. }
  45.  
  46. }
  47.  
  48. class TestCommand {
  49.  
  50. static constraints = {
  51. age(range: 5..10)
  52. }
  53.  
  54. int age
  55.  
  56. String toString() {
  57. "age:[$age]"
  58. }
  59. }
Add Comment
Please, Sign In to add comment