Struts 2

Part 1 — Getting Started

  1. Install Tomcat Server
  2. Download Struts 2
  3. Create a dynamic web project in Eclipse
  4. Include struts jar files that are downloaded
  5. Create struts.xml file
  6. Write action classes
  7. Inside the struts.xml file, mention struts dtd and in <struts> tag mention package tag
  8. Inside the package tag, mention mapping between action and request URL
  9. Write the business logic within execute() method that returns a string
  10. The return string is used to route between JSPs
  11. Create JSP file
  12. Inside mapping between action and request URL in struts.xml file, mention the JSP files to be displayed based on the return string by action class
  13. We need not restart the server if changes are made in the action class or JSP
  14. Restart the server if we have an XML change
  15. Modify web.xml file
  16. Mention filter and filter-mapping
  17. For nested endpoints, use namespace in package tag
  18. For JSP and action communication, declare taglib in the JSP file. Make the variables as member variables in the action class (not as local variables). Write getter and setter for it
  19. taglib has a prefix prefix="s” . We can access the member variables using s:property tag with value attribute as a member variable
  20. These tags are called struts tags
  21. Accessing member variables is not thread-safe

Part 2 — Core Concepts

Consider servlet analogy (different from struts way of handling)

  1. To make it thread-safe, remove the member variables and create a separate space for each request called request object which contains the member variables
  2. Request objects are available in JSPs as well
  3. Request object vs session — pretty obvious
  4. This problem is not seen in struts
  5. For each request, a new action object is created. Struts handle this.
  6. Action object for every request is saved inside the valuestack
  7. JSP accesses this member variable by VALUESTACK
  8. Valuestack acts as a virtual object. ie. Member variables are isolated from objects. We don't access the variables with obj.var but with valuestack.var
  9. Since the members do not have object association there may be conflicts. This is simply handled by looking top-down, and whatever matches first is returned

10. Accessing input parameters — Get query parameter — Define member variable and use it. It is understood by struts. Interceptors work behind the scenes

11. Post request — Write form, input tags with the action attribute to endpoint

12. Instead of form, input tags, use struts 2 tags. s:form, s:textfield, s:submit

13. We can split the struts.xml file into multiple XML files, but the main point is struts.xml. Use include tag

14. We can use wildcards, dummy classes (i.e. no action class)

15. Use extends action support to override validate() function and addFieldError()

16. For 1 action for every operation, we have to create 1 action class if we use default execute(). We can use someOtherFunction() as well by mentioning method attribute in <action> tag so that we can use multiple operations within a single action class

17. Use Models — as usual

18. ModelDriven — implements ModelDriven — override getModel() function

Part 2 — Advanced Concepts




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store