Struts
- Uses the Servlet MVC framework
- Tailored for web appilcations
- action: eg: Actionform, action messages- Controller classes
- actions: -Out of box Action classes eg: Dispatcher action
- config-configuration file
- taglib
- Tiles
- upload- uploading/downloading from a browser
- util-General purpose utility classes
- validator: Struts specific extension classes when deploying the validator
- Receive a client request,invoke a business operation, coordinate view to return to client
- Many struts controller components example: org.apache. struts. action. ActionServlet
- extends javax.servlet.http:HttpServlet
- not abstract
- Configured in deployment descriptor
- All controller requests processed by helper classes- decendants of Action class
ActionServlet
- Request Processor
- org.apache. struts. action. RequestProcessor
- Version 1.1 onwards
- Together with ActionServlet called Controller
- Struts Action Classes
- org.apache. struts. action. Action
- Not abstract
- Extension of Controller component
- bridge or decoupler-client-user action, business operation
- Allows more than One to one action between user action and business action
- execute() menthod-
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpRequest req, HttpResponse res) throws Exception - Called by controller upon client request
- One instance of each action class in an application
- Developer must ensure thread safety of action class
- Developer must overide method execute()
- Each action class extends Action and overrides method execute().
- No try/catch or InvalidLoginException
- Struts uses declarative exception handling
- Exceptions for actions are specfied in Struts configuration file
- Action instances are invoked by the Struts framework from mappings specified in Action Mapping files that use URIs with a path attribute.
- Returned View from an Action is specified by ActionForward return object.
- Specifies destination after an Action has completed.
- Declaritively specify action mapping with JSP and ActionForward in Application
- Specify if it is a redirect with a flag
- Course-grained view of remote fine-grained data.
- Only data, no business logic
- reduces network bandwidth and improves response times
- Decouples business logic from presentation
- maintenance and making enhancements is easier.
- Collocation- Objects deployed in the same JVM
- HTML, DTOs, ActionForms, JSPs, Custom Tags, Java Resource Bundles
- Check mapping to see if ActionForm is configured
- If present, use action name to look up bean configuration
- Check if action form created
- Reuse if action form is created
- Otherwise create new instance, store in appropriate scope as specified in action element.
- Call reset()
- Iterate request parameters , populate every param with Action form with action parameter
- If validate is 'true' invoke validate.
- Two important methods of Action form: reset() and validate()
- reset() called by controller before populating ActionForm with request values.
- Action form can reset its values back to default state.
- Probably not to be used with multiple pages as values are not reset until an instance is destroyed.
- Validate called after request values are inserted into ActionForm.
- Must not be used for business operations- These are done in business objects only.
- Validation is used for Presentation Validation only.
- All ActionForms of application configued in form-beans section on Config File.
- Configure Action forms
- use form-bean names with one or more action elements.
- DynaActionForm
- New in Struts 1.1
- org.apache.struts.action.DynaAction
- JSP disadvantages
- Developers may embed business logic in JSP
- Learning curve
- Container must recompile JSP when a change is made to a page.
- Using Tag Libraries
- Need to be registered in web.xml
- Include taglib elements in JSP
- <%@ taglib uri ="/...." prefix="bean">
- No Java code, only HTML
- Resource Bundles
- Set on java.util.ResourceBundle
- org.apache.struts.utils.MessageResources
- Multiple App Support
- Pre 1.1 versions- Only one struts-config.xml
- 1.1 Many config files possible- developers may work in parallel.
- Now called Application Modules