










Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Esta página não é visível na pré-visualização
Não perca as partes importantes!











Slides © Marty Hall, http://www.coreservlets.com, books © Sun Microsystems Press
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
5 Apache Struts:Beans www.coreservlets.com
Agenda
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 Populate bean based on request parameters. Supply as second argument to execute.
Use bean:write to output bean properties.
9 Apache Struts:Beans www.coreservlets.com
The Six Basic Steps in Using
Struts: Updates for Bean Use
1. Modify struts-config.xml. Use WEB-INF/struts-config.xml to:
The Six Basic Steps in Using
Struts: Updates for Bean Use
3. Create results beans. - These are normal beans of the sort used in MVC when
4. Define an Action class to handle requests. - Rather than calling request.getParameter explicitly as in
11 Apache Struts:Beans www.coreservlets.com
The Six Basic Steps in Using
Struts: Updates for Bean Use
5. Create form that invokes blah .do. - For now, we will use static HTML - Later, we will use the html:form tag to guarantee that the
6. Display results in JSP. - The JSP page uses the bean:write tag to output
Example 1:
Form and Results Beans
15 Apache Struts:Beans www.coreservlets.com
Step 1 (Modify struts-
config.xml), Continued
Step 1 (Modify struts-
config.xml) -- Final Code
**
**
17 Apache Struts:Beans www.coreservlets.com
Step 2 (Define a Form Bean)
18 Apache Struts:Beans www.coreservlets.com
Step 2 (Define a Form Bean) --
Code Example
package coreservlets; import org.apache.struts.action.;*
public class UserFormBean extends ActionForm { private String email = "Missing address"; private String password = "Missing password";
public String getEmail() { return(email); }
public void setEmail(String email) { this.email = email; }
public String getPassword() { return(password); }
public void setPassword(String password) { this.password = password; } }
21 Apache Struts:Beans www.coreservlets.com
Step 3, Business Logic Code
(To Build SuggestionBean)
package coreservlets;
public class SuggestionUtils { private static String[] suggestedAddresses = { "[email protected]", "[email protected]", "[email protected]", "[email protected]" }; private static String chars = "abcdefghijklmnopqrstuvwxyz0123456789#@$%^&?!";*
public static SuggestionBean getSuggestionBean() { String address = randomString(suggestedAddresses); String password = randomString(chars, 8); return(new SuggestionBean(address, password)); } … }
Step 4 (Define an Action Class
to Handle Requests)
23 Apache Struts:Beans www.coreservlets.com
Step 4 (Define an Action Class
to Handle Requests) -- Code
public ActionForward execute(ActionMapping mapping, ActionForm form, ... request, ... response) throws Exception { UserFormBean userBean = (UserFormBean)form; String email = userBean.getEmail(); String password = userBean.getPassword(); if ((email == null) || (email.trim().length() < 3) || (email.indexOf("@") == -1)) { request.setAttribute("suggestionBean", SuggestionUtils.getSuggestionBean()); return(mapping.findForward("bad-address")); } else if ((password == null) || (password.trim().length() < 6)) { request.setAttribute("suggestionBean", SuggestionUtils.getSuggestionBean()); return(mapping.findForward("bad-password")); } else { return(mapping.findForward("success")); } }
Step 5 (Create Form that
Invokes blah .do)
**
New Account Registration
New Account Registration
Email address: Password:
**
27 Apache Struts:Beans www.coreservlets.com
Step 6 (Display Results in JSP)
First Possible Page
**
Illegal Email Address
Illegal Email Address <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> The address "" is not of the form username@hostname (e.g., ).
Please try again.
**Step 6 (Display Results in JSP)
Second Possible Page
**
Illegal Password
Illegal Password <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> The password "" is too short; it must contain at least six characters. Here is a possible password: .
Please try again.
**29 Apache Struts:Beans www.coreservlets.com
Step 6 (Display Results in JSP)
Third Possible Page
**
Success
You have registered successfully. <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
Email Address:
Password:
Congratulations
**
Example 1: Results (Initial Form)
33 Apache Struts:Beans www.coreservlets.com
Example 1: Results
(Legal Address and Password)
Using the JSP 2.0 Expression
Language with Struts
… **
35 Apache Struts:Beans www.coreservlets.com
Struts Example: Confirmation
Page (Classic Style)
**… Congratulations. You are now signed up for the Single Provider of Alert Memos network! <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
First name:
Last name:
Email address:
Fax number:
…**
Struts Example: Confirmation
Page (JSP 2.0 Style)
**… Congratulations. You are now signed up for the Single Provider of Alert Memos network!
First name: ${contactFormBean.firstName} Last name: ${contactFormBean.lastName} Email address: ${contactFormBean.email} Fax number: ${contactFormBean.faxNumber}
…**