struts
DESCRIPTION
basic in strutsTRANSCRIPT
![Page 1: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/1.jpg)
Jakarta Struts 1.1Ready for Prime Time
Atlanta Java Users Group (AJUG)August 20 2002Chuck Cavaness
![Page 2: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/2.jpg)
Speaker Introductions
Senior Technologist (S1 Corporation)Co-Author of Special Edition EJB 2.0 Co-Author of Special Edition Java 2Technical Editor on Various J2EE Books including Special Edition JSP and Servlets, Special Edition J2EE, WebServices and WebLogic 7.0Articles for JavaWorld, InformIT, etc.Struts developer and user since the beginningBuilt several large J2EE Struts-based appsAuthor of Jakarta Struts from O’Reilly
![Page 3: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/3.jpg)
Presentation Goals
Introduce Struts from 5,280 ftIntroduce the Struts 1.1 featuresHighlight the steps for developing a Struts applicationSubliminally emphasize the importance and benefit of using a framework like Struts
![Page 4: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/4.jpg)
Presentation Goals (continued)No software to pushNo financial ties to the frameworkPurely Altruistic purpose
Buy my book!
![Page 5: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/5.jpg)
What is Struts?An open source development framework for building web applicationsBased on Model-View-Controller (MVC) design paradigmImplementation of JSP Model 2 ArchitectureCreated by Craig McClanahan and donated to Apache Software Foundation (ASF) in 20002nd release candidate of version 1.1 released
![Page 6: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/6.jpg)
What is Struts? (continued)
Consists of 8 Top-Level PackagesApprox 250 Classes and Interfaces
![Page 7: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/7.jpg)
Case Study
Let’s create a dot com companyNeed a solid business modelSomething that many consumers are interested inStrong sells regardless of the economy or market
![Page 9: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/9.jpg)
Beer4All Web Application
![Page 10: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/10.jpg)
Beer4All Application (continued)
![Page 11: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/11.jpg)
Purpose of Case Study
A context for our Struts discussionWe’ll use this case study off and on throughout the presentationPromote better beer drinking through technologyAnd yes I do own www.Beer4All.com!
![Page 12: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/12.jpg)
Selecting a UI Framework for Beer4All
No framework (use straight JSP)Build our own frameworkWebworkExpressoBarracudaCocoonSiteMeshFreemarker, Velocity andWebMacroXML/XSLT???
Highly Paid Beer4All Architects Meet!
![Page 13: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/13.jpg)
Why not use Struts?
Smart, but extremely underpaid developers who attended the AJUG Struts presentation ask…
![Page 14: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/14.jpg)
Why consider Struts? (Manager Version)
Developed by Industry ExpertsStable & MatureManageable learning CurveOpen Source1700 member User Community (50-100 new members each month)It’s probably similar to what you would build if not using StrutsGood documentation – 5 books available soon!
![Page 15: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/15.jpg)
Why consider Struts? (Developer Version)
Feature-richFree to develop & deployMany supported third-party toolsFlexible & Extendable
J2EE TechnologiesExpert Developers and CommittersLarge User CommunityPerformant
![Page 16: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/16.jpg)
Struts Framework FeaturesModel 2 - MVC ImplementationInternationalization(I18N) SupportRich JSP Tag LibrariesBased on JSP, Servlet, XML, and JavaSupports Java’s Write Once, Run Anywhere PhilosophySupports different model implementations (JavaBeans, EJB, etc.)
Supports different presentation implementations( JSP, XML/XSLT, etc)
![Page 17: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/17.jpg)
Struts Dependencies
Java 1.2 or newerServlet 2.2 and JSP 1.1 containerXML parser compliant with JAXP 1.1 or newer (e.g. Xerces)Jakarta Commons packagesJDBC 2.0 optional package
![Page 18: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/18.jpg)
Decision: Let’s go with Struts!
Beer4All Chief Architect proclaims!
![Page 19: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/19.jpg)
Beer4All Logical Architecture
![Page 20: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/20.jpg)
Let’s Talk about the Framework
ControllerModelViewConfiguration1.1 Features
![Page 21: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/21.jpg)
The Controller Components
![Page 22: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/22.jpg)
Controller Components
ActionServlet – (Framework provided)
RequestProcessor – (Framework provided)
Action Classes – (You have to build these)
![Page 23: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/23.jpg)
ActionServlet and RequestProcessor(What Do They Really Do?)
Receive the HttpServletRequestAutomatically populate a JavaBean from the request parametersHandle Locale and Content Type IssuesDetermine which Action to invoke based on URIProvide extension points
![Page 24: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/24.jpg)
ActionServlet Facts
Extends javax.servlet.http.HttpServletReceives all framework requestsSelects proper application moduleDelegates request handling to theRequestProcessor instanceOne ActionServlet instance per web applicationDefault implementation provided by framework (can extend if necessary)May go away in future versions
![Page 25: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/25.jpg)
The RequestProcessor Class
One instance per application moduleProcesses all requests for moduleInvokes proper Action instanceDefault implementation provided by framework (can extend if necessary)
![Page 26: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/26.jpg)
ActionServlet and RequestProcessor Diagram
![Page 27: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/27.jpg)
What’s an Action Class?
Extends org.apache.struts.action.ActionOverrides the execute() methodActs as a bridge between user-invoked URI and a business method (Command pattern)Returns information about which view should be rendered nextPart of the Controller, not the Model
![Page 28: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/28.jpg)
Action Class Diagram
![Page 29: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/29.jpg)
Action Sequence DiagramactionServlet requestProccessor loginAction
process()
execute()
Beer4AllService
authenticate()
storeCredentials
return ActionForward
perform forward
post/get
![Page 30: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/30.jpg)
Example Action execute() Methodpublic ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequestrequest, HttpServletResponse response )
throws Exception{
String username = ((LoginForm)form).getUsername();
String password = ((LoginForm)form).getPassword();
// Obtain the service
Beer4AllService serviceImpl = getBeer4AllService();
// Authenticate the user
UserView userView = serviceImpl.authenticate(username, password);
// Store the user’s credentials
UserContainer existingContainer = getUserContainer(request);
existingContainer.setUserView( userView );
// Return an ActionForward for the next page
return mapping.findForward(Constants.SUCCESS_KEY);
}
![Page 31: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/31.jpg)
Beer4AllBaseAction Classpackage com.cavaness.beer4all.framework;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import javax.servlet.ServletContext;
import com.cavaness.beer4all.service.*;
import com.cavaness.beer4all.util.Constants;
abstract public class Beer4AllBaseAction extends Action {
public Beer4AllService getBeer4AllService(){
ServletContext ctx = getServlet().getServletContext();
ServiceFactory factory =
(ServiceFactory)ctx.getAttribute(Constants.SERVICE_FACTORY_KEY);
return (Beer4AllService)factory.createService();
}
}
![Page 32: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/32.jpg)
Struts Includes Pre-built Action Classes
ForwardActionDispatchActionLookupDispatchActionIncludeActionSwitchAction
![Page 33: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/33.jpg)
The Model Components
![Page 34: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/34.jpg)
Struts Model Components
No built-in support for the modelNo model components providedFramework supports any component model (JavaBeans, EJB, Corba, JDO, etc.)Should always decouple the application from a specific model implementation.
![Page 35: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/35.jpg)
Patterns De JourModel-View-ControllerFront ControllerSession FaçadeService LocatorData Transfer Object (a.k.a ValueObject)Command Business DelegateFactoryData Access ObjectService to Worker
![Page 36: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/36.jpg)
Business Delegate Pattern
![Page 37: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/37.jpg)
Benefits of the Business Delegate Pattern
Reduces coupling, increases manageabilityHides complexity of remote servicesExposes a simple uniform interface of the business tier to the clientMore complex features such as failure recovery are made easier
![Page 38: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/38.jpg)
Beer4All Business Interfacepackage com.cavaness.beer4all.service;
import java.util.List;
import com.cavaness.beer4all.catalog.view.CatalogView;
import com.cavaness.beer4all.catalog.view.ItemView;
import com.cavaness.beer4all.common.view.UserView;
import com.cavaness.beer4all.common.exceptions.InvalidLoginException;
import com.cavaness.beer4all.common.exceptions.DatabaseException;
public interface Beer4AllService {
public List getFeaturedItems() throws DatabaseException;
public List getFeaturedCatalogs() throws DatabaseException;
public ItemView getItemById( String itemId ) throws DatabaseException;
public List getItemsInCatalog( String catalogId ) throws DatabaseException;
public void logout(String email) throws DatabaseException;
public UserView authenticate(String username, String password)
throws DatabaseException, InvalidLoginException;
}
![Page 39: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/39.jpg)
Beer4All Service Implementations
Beer4AllDebugService
+authenticate()+getFeaturedCatalogs()+getFeaturedItems()+getItemById()+getItemsInCatalog()+logout()
«interface»Beer4AllService
Beer4AllEJBService Beer4AllOJBService
![Page 40: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/40.jpg)
Beer4All Service FactoryServiceFactory Beer4AllOJBService
getServiceClass
newInstance
createService
framework servletContext
setAttribute(service)
![Page 41: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/41.jpg)
Struts with Enterprise JavaBeans
+authenticate()
Business Delegate
+execute()
LoginAction
+authenticate()
SessionBean
1
uses delegates
![Page 42: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/42.jpg)
The View Components
![Page 43: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/43.jpg)
View ComponentsJavaServer PagesHTMLJavaScript and StylesheetsMultimedia FilesResource BundlesJavaBeans (Part of model used by views)JSP Custom TagsActionForms
![Page 44: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/44.jpg)
Struts JSP Tag Libraries
HTMLBeanLogicNestedTilesTemplate
![Page 45: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/45.jpg)
The HTML Tag Library
Tags used to create Struts input formsExamples include (checkbox, image, link, submit, text, textarea)
![Page 46: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/46.jpg)
HTML Tag Example<table>
<tr><b>Shipping Address</b></tr>
<tr><td>Address:</td>
<td><html:text name="checkoutForm" property="shippingAddress"/>
</td></tr>
<tr><td>City:</td>
<td><html:text name="checkoutForm" property="shippingCity"/>
</td></tr>
<tr><td>State:</td>
<td><html:text name="checkoutForm" property="shippingState"/>
</td></tr>
<tr><td>Postal:</td>
<td><html:text name="checkoutForm" property="shippingPostal"/>
</td></tr>
</table>
![Page 47: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/47.jpg)
HTML Tag Example (continued)
html:text
![Page 48: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/48.jpg)
The Bean Tag Library
Tags used for accessing JavaBeans and their propertiesExamples include (define, message, write)
![Page 49: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/49.jpg)
Bean Tag Library Example<b>
<bean:write name="UserContainer" property="cart.size“ scope="session"/>
</b>
<br>Current Total:
<b>
$<bean:write name="UserContainer" format="#,##0.00" property="cart.totalPrice“ scope="session"/>
</b>
![Page 50: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/50.jpg)
Bean Tag Example (continued)
bean:writebean:message
![Page 51: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/51.jpg)
The Logic Tag Library
Managing conditional generation of output textLooping over object collections for repetitive generation of output textApplication flow management.Examples include (empty, lessThan, greaterThan, redirect, iterate)
![Page 52: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/52.jpg)
Logic Tag Library Example<logic:iterate id="cartItem" scope="session" name="UserContainer" property="cart.items">
…
<html:link
page="/action/viewitemdetail?method=viewItem"
paramName="cartItem"
paramId="id"
paramProperty="id">
<bean:write name="cartItem" property="name"/>
</html:link>
…
</logic:iterate>
![Page 53: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/53.jpg)
Logic Tag Library Example(continued)
logic:iterate
![Page 54: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/54.jpg)
The Nested Tag Library
Extend the base struts tags to allow them to relate to each other in a nested natureCreated by Arron BatesAdded to core beginning of 2002
![Page 55: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/55.jpg)
Nested Tag Example
-firstName-lastName-dateOfBirth-email
UserView
-shippingAddress-billingAddress-creditCartInfo
UserProfileView
-profile1
![Page 56: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/56.jpg)
Nested Tag Example (continued)<html:form action="/some-action.do" >
User View... <br>
First: <html:text name=“user" property=“firstName" /><br>
Last: <html:text name=“user" property=“lastName" /><br>
DOB: <html:text name=“user" property=“dateOfBirth" /><br>
<hr> User Profile... <br>
Shipping Address: <html:text name=“user" property=“profile.shippingAddress" /><br>
Billing Address: <html:text name=“user" property=“profile.billingAddress" /><br>
<hr> </html:form>
![Page 57: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/57.jpg)
Nested Tag Example (continued)<html:form action="/some-action.do" >
User View... <br>
First: <nested:text property=“firstName" /><br>
Last: < nested :text property=“lastName" /><br>
DOB: < nested :text property=“dateOfBirth" /><br>
<nested:nest property=“profile">
User Profile... <br>
Shipping Address: <nested:text property=“shippingAddress" /><br>
Billing Address: <nested:text property=“billingAddress" /><br>
</nested:nest>
<hr> </html:form>
![Page 58: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/58.jpg)
Nested Tag Benefits
Tags can have a relationshipFewer attributes must be definedCan work against a single levelChange is more manageable
![Page 59: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/59.jpg)
What is an ActionForm?
Java class that extends the org.apache.struts.action.ActionFormCaptures user data from the Http requestStores the data temporarilyActs as a “firewall” between the presentation tier and the applicationProvides the ability to validate the user input
![Page 60: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/60.jpg)
Beer4All LoginForm Examplepublic class LoginForm extends ActionForm {
private String password = null;
private String username = null;
// … some code not shown
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
// Get access to the message resources for this application
MessageResources resources =
(MessageResources)request.getAttribute( Action.MESSAGES_KEY );
if(getUsername() == null || getUsername().length() < 1) {
String usernameLabel = resources.getMessage( "label.username" );
errors.add( ActionErrors.GLOBAL_ERROR, new ActionError("errors.required", usernameLabel ));
}
if(getPassword() == null || getPassword().length() < 1) {
String passwordLabel = resources.getMessage( "label.password" );
errors.add( ActionErrors.GLOBAL_ERROR, new ActionError("errors.required", passwordLabel ));
}
return errors;
}
![Page 61: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/61.jpg)
ActionForm Sequence Diagram
![Page 62: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/62.jpg)
ActionMessage and ActionError
Used to signify general purpose informational and error messagesRely on the Resource BundlesJSP Tags can access them
![Page 63: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/63.jpg)
ActionMessage Class Hierarchy
![Page 64: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/64.jpg)
Beer4All Signin Error Messages
<tr class="RED"><td></td><td>
<html:messages id="error"><li><bean:write name="error"/></li>
</html:messages></td>
</tr>
![Page 65: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/65.jpg)
Beer4All Signin Error Messages(continued)
html:messages
![Page 66: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/66.jpg)
Putting it together
![Page 67: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/67.jpg)
Configuring a Struts Application
Create and edit the web app deployment descriptor (web.xml)Create and edit the struts-config.xml fileOther configuration files might be necessary for Validator and tiles
![Page 68: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/68.jpg)
Configuring web.xml for Struts
Add servlet elementConfigure servlet-mapping elementAdd taglib elements
![Page 69: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/69.jpg)
Beer4All web app Descriptor<web-app>
<servlet>
<servlet-name>beer4all</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>beer4all</servlet-name>
<url-pattern>/action/*</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
… other tag library descriptors here
<</web-app>
![Page 70: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/70.jpg)
Struts Configuration File
Uses XMLDefines the set of “rules” for a Struts applicationStarting with 1.1, can define multipleGets parsed and loaded into memory at startup
![Page 71: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/71.jpg)
Beer 4 All Struts Config File
Let’s look at some source!
![Page 72: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/72.jpg)
Internationalization Support
Much of the framework’s functionality based on java.util.LocaleStruts Uses Java Resource Bundles
![Page 73: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/73.jpg)
Using Java Resource Bundlesglobal.title=Beer For All
label.featuredcatalogs=Featured Catalogs
label.featuredbeers=Featured Beers
label.username=Username
label.password=Password
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters.
errors.maxlength={0} can not be greater than {1} characters.
errors.invalid={0} is invalid.
errors.byte={0} must be an byte.
errors.short={0} must be an short.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.float={0} must be an float.
errors.double={0} must be an double.
errors.date={0} is not a date.
![Page 74: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/74.jpg)
Locale-based Message Bundles Web Container
Web AppClassLoader
WEB-INF/classes
Beer4AllMessageResources_en_US.properties
Beer4AllMessageResources_de_DE.properties
ClassFiles
ClassFilesClass
Files
![Page 75: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/75.jpg)
Accessing messages from JSP Tags<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html locale="true">
<html:form action="main">
<head>
<html:base/>
<link rel="stylesheet" href="stylesheets/format_win_nav_main.css" type="text/css">
<title><bean:message key="global.title"/></title>
</head>
![Page 76: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/76.jpg)
I18N is more than Resource Bundles
Date and Time FormattingCurrency FormattingCurrency ConversionText directionProper Input ControllersColor Conventionsetc…
![Page 77: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/77.jpg)
Overview of Struts 1.1 Features
Multi-Module SupportDeclarative Exception-HandlingDynamic ActionFormsNested Tag LibraryNew Config PackageMore Extension Points
Validator integrated with CoreTiles integrated with CorePlugInsUses Commons Components
![Page 78: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/78.jpg)
Multi-Module Support
Separate, independent application modules (sub-applications)Supports parallel developmentSeparate Struts configuration filesUse the SwitchAction to move between application modules
![Page 79: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/79.jpg)
Multi-Application Support
![Page 80: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/80.jpg)
Exception Handling Capabilities
Declarative or/and Programmatic SupportDeclarative added to 1.1Define which exceptions can be thrown for which actionsCreate your own custom ExceptionHandler, per action if necessary
![Page 81: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/81.jpg)
How Declarative Exception Handling works
public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception {
String username = ((LoginForm)form).getUsername();
String password = ((LoginForm)form).getPassword();
ServletContext context = getServlet().getServletContext();
// Login through the security service
Beer4AllService serviceImpl = this.getBeer4AllService();
// Authenticate the user
UserView userView = serviceImpl.authenticate(username, password);
UserContainer existingContainer = getUserContainer(request);
existingContainer.setUserView( userView );
return mapping.findForward(Constants.SUCCESS_KEY);
}
![Page 82: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/82.jpg)
Declarative Exception Handling
![Page 83: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/83.jpg)
Override the ExceptionHandler
+execute()+storeException()
org.apache.struts.action.ExceptionHandler
+execute()
CustomExceptionHandler
![Page 84: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/84.jpg)
Programmatic Exception Handlingpublic ActionForward execute( ActionMapping mapping, ActionForm form,HttpServletRequest request,
HttpServletResponse response ) throws Exception{UserView userView = null;String username = ((LoginForm)form).getUsername();String password = ((LoginForm)form).getPassword();Beer4AllService serviceImpl = this.getBeer4AllService();try{
// Attempt to authenticate the useruserView = serviceImpl.authenticate(username, password);
}catch( InvalidLoginException ex ){ActionErrors errors = new ActionErrors();ActionError newError = new ActionError( "" );errors.add( ActionErrors.GLOBAL_ERROR, newError );saveErrors( request errors);return mapping.findForward( Constants.FAILURE_KEY );
}UserContainer existingContainer = getUserContainer(request);existingContainer.setUserView( userView );return mapping.findForward(Constants.SUCCESS_KEY);
}
![Page 85: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/85.jpg)
Programmatic or Declarative?
Use Declarative Exception Handling if possible. Customize the ExceptionHandler for customized behaviorUse programmatic only when necessary
![Page 86: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/86.jpg)
Dynamic ActionForms
Define ActionForms declarativelyBehave like regular ActionFormsthroughout the applicationNo need to define ActionForm classes
![Page 87: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/87.jpg)
DynaActionForm Example
<form-bean
name="checkoutForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="shippingAddress" type="java.lang.String"/>
<form-property name="shippingCity" type="java.lang.String"/>
<form-property name="shippingState" type="java.lang.String"/>
<form-property name="shippingPostal" type="java.lang.String"/>
</form-bean>
![Page 88: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/88.jpg)
DynaActionForm Example (continued)
<table>
<tr><b>Shipping Address</b></tr>
<tr> <td>Address:</td>
<td><html:text name="checkoutForm" property="shippingAddress"/></td></tr>
<tr><td>City:</td>
<td><html:text name="checkoutForm" property="shippingCity"/></td></tr>
<tr><td>State:</td>
<td><html:text name="checkoutForm" property="shippingState"/></td></tr>
<tr><td>Postal:</td>
<td><html:text name="checkoutForm" property="shippingPostal"/></td></tr>
</table>
![Page 89: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/89.jpg)
The Struts Validator
Open source Validation frameworkDeveloped by David WinterfeldtIntegrated into Struts core during 1.1Extendable and FlexibleCan be used outside of StrutsUtilizes Regular ExpressionsUses the Jakarta ORO Package
![Page 90: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/90.jpg)
Validator Features
Allows for regular expressions to be usedComes with many pre-built validation routinesSupports Client-Side (JavaScript) and Server-Side (Java)
![Page 91: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/91.jpg)
Adding Validator to a Struts Application
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
![Page 92: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/92.jpg)
Validator configuration Files
validator-rules.xmlvalidation.xml
![Page 93: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/93.jpg)
Validator-rules.xml File<validator name="required"
classname="org.apache.struts.util.StrutsValidator"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.required">
//…some text deleted
![Page 94: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/94.jpg)
Validator-rules.xml (continued)<javascript><![CDATA[
function validateRequired(form) {
var bValid = true;
var focusField = null;
var i = 0;
var fields = new Array();
oRequired = new required();
for (x in oRequired) {
if ((form[oRequired[x][0]].type == 'text' || form[oRequired[x][0]].type == 'textarea' ||
form[oRequired[x][0]].type == 'select-one' || form[oRequired[x][0]].type == 'radio' ||
form[oRequired[x][0]].type == 'password') && (form[oRequired[x][0]].value == '')) {
if (i == 0) { focusField = form[oRequired[x][0]]; }
fields[i++] = oRequired[x][1];
bValid = false;
}
}
</javascript>
![Page 95: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/95.jpg)
Validator.xml File<form name="checkoutForm">
<field
property="shippingAddress"
depends="required,mask">
<arg0 key="label.address"/>
<var>
<var-name>mask</var-name>
<var-value>^\w+$</var-value>
</var>
</field>
…
![Page 96: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/96.jpg)
Message Bundle …
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters.
errors.maxlength={0} can not be greater than {1} characters.
errors.invalid={0} is invalid.
errors.byte={0} must be an byte.
errors.short={0} must be an short.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.float={0} must be an float.
errors.double={0} must be an double.
errors.date={0} is not a date.
…
![Page 97: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/97.jpg)
Validator Failures
Validation failures
![Page 98: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/98.jpg)
Struts Plug-in Capabilities
Declarative means for “Startup Classes”Can declare multiple Plug-ins per sub-applicationJust need to implement the PlugIn interfaceFramework will call init() on startup and destroy() on shutdown
![Page 99: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/99.jpg)
Using the Plug-in Functionality
![Page 100: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/100.jpg)
Plug-in Example
<plug-in className="com.cavaness.beer4all.service.ServiceFactory">
<set-property
property="serviceClassName"
value="com.cavaness.beer4all.service.Beer4AllOBJService"/>
</plug-in>
![Page 101: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/101.jpg)
Beer4All ServiceFactory Plug-inpublic class ServiceFactory implements PlugIn {
private String serviceClassName = null;
public String getServiceClassName(){
return serviceClassName;
}
public void setServiceClassName( String className ){
this.serviceClassName = className;
}
public void init(ActionServlet servlet, ApplicationConfig config){
// Perform initialization functionality here
}
public void destroy(){
// Perform shutdown functionality here
}
}
![Page 102: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/102.jpg)
Tiles Library FeaturesAdvanced templating mechanismSet of JSP TagsSupports the idea of Layouts (known as Tiles)Layouts based on Locale/ChannelTiles can be reusedCreated by Cedric Dumoulin (while working at S1 coincidently )Added to core in Struts 1.1
![Page 103: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/103.jpg)
Beer4All Layout
Header Region/Tile
Menu Bar Region/Tile
Body Content Region/Tile
Copyright Region/Tile
![Page 104: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/104.jpg)
Beer4All Layout Tile<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles"%>
<html:html>
<head><title><bean:message key="global.title"/></title> </head>
<body topmargin="0" leftmargin="0" bgcolor="#FFFFFF">
<tiles:insert attribute="header" />
<tiles:insert attribute=“banner"/>
<tiles:insert attribute="body-content"/>
<tiles:insert attribute="copyright"/>
</body>
</html:html>
![Page 105: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/105.jpg)
Beer4All JSPs Using Tiles<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>
<tiles:insert page="/layouts/beer4AllDefaultLayout.jsp" flush="true">
<tiles:put name="header" value="/common/header.jsp" />
<tiles:put name=“banner" value="/common/banner.jsp" />
<tiles:put name="body-content" value="/main.jsp" />
<tiles:put name="copyright" value="/common/copyright.jsp" />
</tiles:insert>
<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>
<tiles:insert page="/layouts/beer4AllDefaultLayout.jsp" flush="true">
<tiles:put name="header" value="/common/header.jsp" />
<tiles:put name=“banner" value="/common/banner.jsp" />
<tiles:put name="body-content" value="/beer.jsp" />
<tiles:put name="copyright" value="/common/copyright.jsp" />
</tiles:insert>
![Page 106: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/106.jpg)
Tiles XML Definition<!DOCTYPE tiles-definitions PUBLIC “-//Apache Software Foundation//DTD Tiles Configuration//EN" “http://jakarta.apache.org/struts/dtds/tiles-config.dtd">
<tiles-definitions>
<definition name=“beer4All.default" path="/layouts/beer4allDefaultLayout.jsp">
<put name="header" value="/common/header.jsp" />
<put name=“banner" value="/common/banner.jsp" />
<put name="copyright" value="/common/copyright.jsp" />
</definition>
</tiles-definitions>
![Page 107: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/107.jpg)
Extending Tiles Definitions
<tiles-definitions>
<definition name=“beer4All.custom" extends=”beer4All.default”>
<put name="copyright" value="/common/new-copyright.jsp" />
</definition>
</tiles-definitions>
![Page 108: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/108.jpg)
Other Tiles Resources
Cedric’s Site (http://www.lifl.fr/~dumoulin/tiles/)My Chapter on Tiles (http://www.theserverside.com/resources/strutsreview.jsp)
![Page 109: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/109.jpg)
Jakarta Commons Packages BeanUtilsCollectionsDigesterDBCPLoggingLangResources
![Page 110: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/110.jpg)
Logging in a Struts Application
Struts utilizes Commons Logging (jakarta.apache.org/Commons)Various logging implementations supported (JDK1.4, log4J, Console, etc.)
![Page 111: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/111.jpg)
Logging Example
Log logger = LogFactory.getLog( getClass() );
logger.debug( "LoginAction entered" );
try{
// Attempt to authenticate the user
userView = serviceImpl.authenticate(username, password);
}catch( InvalidLoginException ex ){
logger.error( "Exception in the LoginAction class", ex );
//…
![Page 112: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/112.jpg)
Packaging and Deployment
Package as a Web ARchive File (WAR)Deploys into any Servlet 2.2, JSP 1.1 compliant web containerYou can also leave it explodedUse Ant to build and deploy
![Page 113: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/113.jpg)
Beer4All Directory Structure
![Page 114: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/114.jpg)
Beer4All Is a Success!
![Page 115: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/115.jpg)
Beer Drinkers Everywhere Rejoice
The world unites by using Beer4All.com
![Page 116: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/116.jpg)
Resources
Jakarta Struts Home Site (jakarta.apache.org/struts)Ted Husted’s Site (http://www.husted.com/struts)TheServerSide Struts Book Review (http://www.theserverside.com/resources/strutsreview.jsp)Jakarta Struts from O’Reilly (Available for order on Amazon right now!) Struts user and dev mailing listsAtlanta Struts user group (http://www.open-tools.org/struts-atlanta/index.jsp)
![Page 117: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/117.jpg)
Third-Party Tools for Struts
Struts Console (http://www.jamesholmes.com/struts)
Easy Struts Project (http://easystruts.sourceforge.net)
Adalon (http://www.synthis.com)
More (jakarta.apache.org/struts/resources)
![Page 118: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/118.jpg)
When is 1.1 GA?
I Don’t Know!
![Page 119: Struts](https://reader034.vdocuments.mx/reader034/viewer/2022051618/55cf9ac0550346d033a33c13/html5/thumbnails/119.jpg)
Q & A
What did I miss?