strutsstruts-1struts-config

What is the use for form beans vs form action in struts1?


If form bean is used to store the variables of the JSP form connected via action="submitDetailForm" for example then what are ActionForms for?


Solution

  • Whats confusing about the app that I am working on is that it has forms that are actual beans but there's a bean folder that parses the request result.

    Forms are mapped to the action. It doesn't matter which folder they belong. Sometimes classes for the form beans are in the same folder as action beans, sometimes the form beans are in the separate folder. Which folder is used you can find in the struts-config.xml.

    Everything becomes clear if you read Struts struts-config.xml action-mapping explained:

    The type attribute of the <form-bean> is used to enter FQCN of the bean class that would probably extend the ActionForm. It's needed by Struts to be able to instantiate a bean when required.

    You can also read this article for quick overview of the framework Struts:

    The struts-config.xml file can have several sections. The first section we will look at is the <action-mappings> section. <action> tells Struts which class to invoke from the ActionServlet. Only the path and type are required entries. The type tells Struts which Action class to invoke when a URL with the model of the path is found.

    In order for a class to be entered in the <action-mappings> section and to be invoked by ActionServlet, it must extend the class org.apache.struts.action.Action (see Listing 3). The ActionServlet will execute the perform() method of the Action object. The perform method looks like this:

    public ActionForward perform(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
      throws java.io.IOException, javax.servlet.ServletException
    

    The ActionMapping and ActionForm objects will contain information found in the struts-config.xml <action-mappings> and <form-beans> sections. The HttpServletRequest and HttpServletResponse objects are from the servlet.