1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.bind;
  17. import javax.servlet.ServletException;
  18. import javax.servlet.ServletRequest;
  19. import org.springframework.validation.BindException;
  20. import org.springframework.validation.ValidationUtils;
  21. import org.springframework.validation.Validator;
  22. /**
  23. * Offers convenience methods for binding servlet request parameters
  24. * to objects, including optional validation.
  25. * @author Juergen Hoeller
  26. * @author Jean-Pierre Pawlak
  27. * @since 10.03.2003
  28. */
  29. public abstract class BindUtils {
  30. /**
  31. * Bind the parameters from the given request to the given object.
  32. * @param request request containing the parameters
  33. * @param object object to bind the parameters to
  34. * @param objectName name of the bind object
  35. * @return the binder used (can be treated as BindException or Errors instance)
  36. */
  37. public static BindException bind(ServletRequest request, Object object, String objectName) {
  38. ServletRequestDataBinder binder = new ServletRequestDataBinder(object, objectName);
  39. binder.bind(request);
  40. return binder.getErrors();
  41. }
  42. /**
  43. * Bind the parameters from the given request to the given object,
  44. * allowing for optional custom editors set in an bind initializer.
  45. * @param request request containing the parameters
  46. * @param object object to bind the parameters to
  47. * @param objectName name of the bind object
  48. * @param initializer implementation of the BindInitializer interface
  49. * which will be able to set custom editors
  50. * @return the binder used (can be treated as BindException or Errors instance)
  51. * @throws ServletException if thrown by the BindInitializer
  52. */
  53. public static BindException bind(ServletRequest request, Object object, String objectName,
  54. BindInitializer initializer) throws ServletException {
  55. ServletRequestDataBinder binder = new ServletRequestDataBinder(object, objectName);
  56. if (initializer != null) {
  57. initializer.initBinder(request, binder);
  58. }
  59. binder.bind(request);
  60. return binder.getErrors();
  61. }
  62. /**
  63. * Bind the parameters from the given request to the given object,
  64. * invoking the given validator.
  65. * @param request request containing the parameters
  66. * @param object object to bind the parameters to
  67. * @param objectName name of the bind object
  68. * @param validator validator to be invoked, or null if no validation
  69. * @return the binder used (can be treated as Errors instance)
  70. */
  71. public static BindException bindAndValidate(ServletRequest request, Object object, String objectName,
  72. Validator validator) {
  73. BindException binder = bind(request, object, objectName);
  74. ValidationUtils.invokeValidator(validator, object, binder);
  75. return binder;
  76. }
  77. /**
  78. * Bind the parameters from the given request to the given object,
  79. * invoking the given validator, and allowing for optional custom editors
  80. * set in an bind initializer.
  81. * @param request request containing the parameters
  82. * @param object object to bind the parameters to
  83. * @param objectName name of the bind object
  84. * @param validator validator to be invoked, or null if no validation
  85. * @param initializer Implementation of the BindInitializer interface which will be able to set custom editors
  86. * @return the binder used (can be treated as Errors instance)
  87. * @throws ServletException if thrown by the BindInitializer
  88. */
  89. public static BindException bindAndValidate(ServletRequest request, Object object, String objectName,
  90. Validator validator, BindInitializer initializer)
  91. throws ServletException {
  92. BindException binder = bind(request, object, objectName, initializer);
  93. ValidationUtils.invokeValidator(validator, object, binder);
  94. return binder;
  95. }
  96. }