The Spring Framework

org.springframework.web.struts
Class SpringBindingActionForm

java.lang.Object
  extended by org.apache.struts.action.ActionForm
      extended by org.springframework.web.struts.SpringBindingActionForm
All Implemented Interfaces:
Serializable

public class SpringBindingActionForm
extends ActionForm

A thin Struts ActionForm adapter that delegates to Spring's more complete and advanced data binder and Errors object underneath the covers to bind to POJOs and manage rejected values.

Exposes Spring-managed errors to the standard Struts view tags, through exposing a corresponding Struts ActionMessages object as request attribute. Also exposes current field values in a Struts-compliant fashion, including rejected values (which Spring's binding keeps even for non-String fields).

Consequently, Struts views can be written in a completely traditional fashion (with standard html:form, html:errors, etc), seamlessly accessing a Spring-bound POJO form object underneath.

Note this ActionForm is designed explicitly for use in request scope. It expects to receive an expose call from the Action, passing in the Errors object to expose plus the current HttpServletRequest.

Example definition in struts-config.xml:

 <form-beans>
   <form-bean name="actionForm" type="org.springframework.web.struts.SpringBindingActionForm"/>
 </form-beans>
Example code in a custom Struts Action:
 public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
   SpringBindingActionForm form = (SpringBindingActionForm) actionForm;
   MyPojoBean bean = ...;
   ServletRequestDataBinder binder = new ServletRequestDataBinder(bean, "myPojo");
   binder.bind(request);
   form.expose(binder.getBindingResult(), request);
   return actionMapping.findForward("success");
 }
This class is compatible with both Struts 1.2.x and Struts 1.1. On Struts 1.2, default messages registered with Spring binding errors are exposed when none of the error codes could be resolved. On Struts 1.1, this is not possible due to a limitation in the Struts message facility; hence, we expose the plain default error code there.

Since:
1.2.2
Author:
Keith Donald, Juergen Hoeller
See Also:
expose(org.springframework.validation.Errors, javax.servlet.http.HttpServletRequest), Serialized Form

Field Summary
 
Fields inherited from class org.apache.struts.action.ActionForm
multipartRequestHandler, servlet
 
Constructor Summary
SpringBindingActionForm()
           
 
Method Summary
 void expose(Errors errors, HttpServletRequest request)
          Set the Errors object that this SpringBindingActionForm is supposed to wrap.
 
Methods inherited from class org.apache.struts.action.ActionForm
getMultipartRequestHandler, getServlet, getServletWrapper, reset, reset, setMultipartRequestHandler, setServlet, validate, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SpringBindingActionForm

public SpringBindingActionForm()
Method Detail

expose

public void expose(Errors errors,
                   HttpServletRequest request)
Set the Errors object that this SpringBindingActionForm is supposed to wrap. The contained field values and errors will be exposed to the view, accessible through Struts standard tags.

Parameters:
errors - the Spring Errors object to wrap, usually taken from a DataBinder that has been used for populating a POJO form object
request - the HttpServletRequest to retrieve the attributes from
See Also:
DataBinder.getBindingResult()

The Spring Framework

Copyright © 2002-2007 The Spring Framework.