Baixe Aprenda a utilizar o Struts 1.2 para criar aplicações web com JSP e outras Notas de estudo em PDF para Cultura, somente na Docsity!
Slides © Marty Hall, http://www.coreservlets.com, books © Sun Microsystems Press
Jakarta Struts:
Processing Requests
with Action Objects
Struts 1.2 Version
Core Servlets & JSP book: www.coreservlets.com More Servlets & JSP book: www.moreservlets.com Servlet/JSP/Struts/JSF Training: courses.coreservlets.com
Slides © Marty Hall, http://www.coreservlets.com, books © Sun Microsystems Press
For live Struts training, please see
JSP/servlet/Struts/JSF training courses at
http://courses.coreservlets.com/.
Taught by the author of Core Servlets and JSP , More
Servlets and JSP , and this tutorial. Available at public
venues, or customized versions can be held on-site at
your organization.
5 Apache Struts: Actions www.coreservlets.com
Agenda
- Struts flow of control
- The six basic steps in using Struts
- To implement the flow of control
- Example: one result mapping
- Same output page in all cases
- Example: multiple result mappings
- Different output pages depending on the input
- Performing different logic based on a radio button, hidden field, or push button value
Struts Flow of Control
JSP
Form
Determine Action
Action
Choose JSP Page
JSP
request .../SomeForm.jsp submit form request .../ blah .do
invoke execute method
return condition
return final result forward to
struts-config.xml
9 Apache Struts: Actions www.coreservlets.com
The Six Basic Steps in Using
Struts
3. Create results beans.
- In the MVC architecture, the business-logic and data-access code create the results and the JSP pages present them. To transfer the results from one layer to the other, they are stored in beans. These beans differ from form beans in that they need extend no particular class, and they represent the output of the computational process, not the input to the process. Beans will be discussed in the next section.
4. Define an Action class to handle requests.
- The struts-config.xml file designates the Action classes that handle requests for various URLs. The Action objects themselves need to do the real work: invoke the appropriate business- and data-access- logic, store the results in beans, and designate the type of situation (missing data, database error, success category 1, success category 2, etc.) that is appropriate for the results. The struts-config.xml file then decides which JSP page should apply to that situation.
The Six Basic Steps in Using
Struts
5. Create form that invokes blah .do.
- Create an input form whose ACTION corresponds to one of the .do addresses listed in struts-config.xml.
- In a later lecture, we will discuss the advantages of using the Struts html:form tag to build this input form.
6. Display results in JSP.
- Since Struts is built around MVC, these JSP pages should avoid JSP scripting elements whenever possible. For basic Struts, these pages usually use the bean:write tag, but in JSP 2.0 the JSP 2. expression language is a viable alternative.
- In most cases, the JSP pages only make sense when the request is funneled through the Action, so the pages go in WEB-INF.
- If the JSP pages makes sense independently of the Action (e.g., if they display session data), then the JSP pages should be placed in a regular subdirectory of the Web application, and the forward entries in struts-config.xml should say .
11 Apache Struts: Actions www.coreservlets.com
Example 1: One Result Mapping
- URL
- http://hostname/struts-actions/register1.do
- Action Class
- RegisterAction
- RegisterAction1 extends Action and is in the coreservlets package.
- The execute method of RegisterAction1 always returns "success"
- Results page
- /WEB-INF/results/confirm.jsp
- But the URL shown will still be register1.do
Step 1A (Modify struts-config.xml)
- Map incoming .do addresses to Action classes
- In this case, we designate that RegisterAction1 should
handle requests for register1.do. To accomplish this, we
add an action entry to action-mappings, where action has
the following attributes.
- path : the relative path that should be mapped to the Action, minus the .do extension. Thus, path="/register1" refers to http://hostname/webAppName/register1.do.
- type : the fully qualified class name of the Action class that should be invoked when a request for the path is received.
15 Apache Struts: Actions www.coreservlets.com
Steps 2 and 3
- Define a form bean.
- Beans are postponed until the next section, so this step is
omitted for now.
- Create results beans.
- Beans are postponed until the next section, so this step is
omitted for now.
Step 4 (Define an Action Class to
Handle Requests)
- Action subclasses should… be in a package.
package coreservlets;
- This means that the class file should go in your_web_app /WEB- INF/classes/coreservlets/.
- Action subclasses should… add Struts-specific import statements to whatever imports are otherwise needed.
- In this case, we have
import javax.servlet.http.*;
import org.apache.struts.action.*;
17 Apache Struts: Actions www.coreservlets.com
Step 4 (Define an Action Class to
Handle Requests)
- Action subclasses should ... extend Action
- Action subclasses should ... override execute
public class RegisterAction1 extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ... } }
Step 4 (Define an Action Class to
Handle Requests)
- Action subclasses should ... return mapping.findForward. - The execute method should have one or more return
values.
- These values will then be mapped to specific JSP pages
by forward entries in struts-config.xml. In this case, we
simply return "success" in all situations.
public ActionForward
execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
return(mapping.findForward("success"));
21 Apache Struts: Actions www.coreservlets.com
Step 6 (Display results in JSP)
- In general, there can be several possible JSP pages - Corresponding to the various possible return values of the
execute method of the Action.
- In struts-config.xml, each JSP page is declared in a forward entry within the appropriate action. - In this simple case, the only return value is "success", so
/WEB-INF/results/confirm.jsp is used in all cases.
- This JSP page will just display a simple message (see next slide)
Step 6 (Display results in JSP) –
Final Code
Success
You have registered successfully.
Congratulations
23 Apache Struts: Actions www.coreservlets.com
Example 1: Results
- First, the HTML form is invoked with the URL
http://localhost/struts-actions/register1.jsp
Example 1: Results
- This form is then filled in and submitted,
- With the form's ACTION resulting in the URL
http://localhost/struts-actions/register1.do.
- This address is mapped by struts-config.xml
- To the RegisterAction1 class, whose execute method is
invoked.
- This method returns mapping.findForward
- With a value of "success"
- That value is mapped by struts-config.xml
- To /WEB-INF/results/confirm.jsp,
- Which is the final result displayed to the user.
- However, since the JSP page is invoked with RequestDispatcher.forward, not response.sendRedirect, the URL displayed to the user is register1.do, not confirm.jsp.
27 Apache Struts: Actions www.coreservlets.com
Step 1 (Modify struts-config.xml)
- Map incoming .do address to Action classes
- In this case, we use the action element to designate that
RegisterAction2 should handle requests for register2.do
(again, note that .do is implied, not listed explicitly).
- Map return conditions to JSP pages
- In this case, we use multiple forward elements, one for
each possible return value of the execute method of the
RegisterAction2 class.
- Declare any form beans that are being used.
- Beans are postponed until the next section, so this step is
omitted for now.
Step 1 (Modify struts-config.xml) –
Final Code
...
29 Apache Struts: Actions www.coreservlets.com
Steps 2 and 3
- Define a form bean.
- Beans are postponed until the next section, so this step is
omitted for now.
- Create results beans.
- Beans are postponed until the next section, so this step is
omitted for now.
Step 4 (Define an Action Class to
Handle Requests)
- Similar to the previous example except for multiple mapping.findForward entries - We return "bad-address" if the email address is missing,
is less then three characters long, or does not contain an
"@" sign.
- We return "bad-password" if the password is missing or
is less than six characters long.
- Otherwise we return "success".
- In this simple example we use request.getParameter explicitly.
- In later examples we let Struts automatically populate a
bean from the request data.
33 Apache Struts: Actions www.coreservlets.com
Step 6 (Display results in JSP)
First Possible Page
Illegal Email Address
Illegal Email Address Address must be of the form username@host. Please try again.
Step 6 (Display results in JSP)
Second Possible Page
Illegal Password
Illegal Password Password must contain at least six characters. Please try again.
35 Apache Struts: Actions www.coreservlets.com
Step 6 (Display results in JSP)
Same confirm.jsp Shown Earlier
Success
You have registered successfully. Congratulations
Example 2: Results (Initial Form)
39 Apache Struts: Actions www.coreservlets.com
Example 2: Results (Success)
Combining Shared Condition
(Forward) Mappings
- Idea
- If the same condition is mapped to the same JSP page in
multiple actions, you can move the forward to a global-
forwards section to avoid repetition
- Syntax
- The global-forwards section goes before
action-mappings, not within it
- The forward entries within global-forwards have the same
syntax and behavior as forward entries within action
41 Apache Struts: Actions www.coreservlets.com
Combining Shared Condition
(Forward) Mappings: Old
...
Combining Shared Condition
(Forward) Mappings: New
...