Course Code: GCS-P125
STRUTS FRAMEWORK
CONTENTS Configuring a Struts application The Controller The Views Managing Errors Struts Validator Internationalization Struts JDBC Connection Pool Struts Tools References
Confidential
2
Configuring a Struts application
The Web Application Directory Structure The WEB-INF directory is where the deployment descriptor for the web application should be placed.
Confidential
3
Configuring a Struts application (cont.)
Configuring the web.xml file for Struts
Struts Blank Application action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml action *.do
Confidential
4
Configuring a Struts application (cont.)
Configuring the Tag Libraries The taglib-uri element specifies a URI identifying a tab library that is used by the web application. The taglib-location element specifies the location (as a resource) of the tag library descriptor file. /tags/struts-bean /WEB-INF/struts-bean.tld /tags/struts-html /WEB-INF/struts-html.tld /tags/struts-logic /WEB-INF/struts-logic.tld /tags/struts-tiles /WEB-INF/struts-tiles.tld
Confidential
5
Configuring a Struts application (cont.) Setting up the Welcome File List index.jsp Configuring Error Handling in web.xml 404 /common/404.jsp 500 /common/500.jsp
Confidential
6
Configuring a Struts application (cont.)
Using the exception-type element instead of the error-code
javax.servlet.ServletException /common/system_error.jsp
Confidential
7
Configuring a Struts application (cont.) The Struts Configuration File The data-sources Element
Confidential
8
Configuring a Struts application (cont.) The form-beans Element
The global-forwards Element
The action-mappings Element The element configures the mappings from submitted request paths to the corresponding Action classes for a particular sub-application.
Confidential
9
Configuring a Struts application (cont.) The message-resources Element The element specifies characteristics of the message resource bundles that contain the localized messages for an application. The plug-in Element
Confidential
10
Configuring a Struts application (cont.)
Download: - Start at http://jakarta.apache.org/site/binindex.cgi or follow link from http://jakarta.apache.org/struts/ Unzip into a directory of your choice - E.g., C:\jakarta-struts-1.1 - Here after referred to as struts_install_dir Update your CLASSPATH - Add struts_install_dir/lib/struts.jar Copy the .tld files into WEB-INF, copy struts.jar (and all of the common*.jar files) into WEB-INF/lib Tomcat / JDK 1.4
Confidential
11
The Controller
Confidential
12
The Controller(cont.)
The ActionServlet Class The org.apache.struts.action.ActionServlet acts as the primary controller for the Struts framework. All requests from the client tier must pass through this component before proceeding anywhere else in the application. The RequestProcessor Class process()method of the org.apache.struts.action.RequestProcessor is called by the ActionServlet instance and passed the current request and response objects.
Confidential
13
The Controller (cont.)
request
doGet()
call process() ActionServlet RequestProcessor.process() doPost() find FormBean and Action from strutsconfig.xml
determined target
return an ActionForward to RequestProcessor Action.execute() forward to
Confidential
ActionForm.validate()
14
The Views
Using Views within the Struts Framework HTML Documents JSP Custom Tag Libraries JavaScript and Style Sheets Multimedia Files Message Bundles ActionForm Classes
Confidential
15
The Views (cont.)
ActionForm
Confidential
16
The Views (cont.) import org.apache.struts.action.*; public class MyForm extends ActionForm { protected String name; protected String address; public String getName(){ return this.name; }; public String getAddress(){ return this.address; }; public void setName(String name) { this.name = name; }; public void setAddress(String address){ this.address = address; }; };
Confidential
17
The Views (cont.) public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest resquest) { /** @todo: Override this org.apache.struts.action.ActionForm method*/ return super.validate(actionMapping, resquest); }
Using in Edit Form Set the form’s data members back to their default values
public void reset(ActionMapping actionMapping, HttpServletRequest request) { /** @todo: Override this org.apache.struts.action.ActionForm method*/ super.reset(actionMapping, request); UserBean userBean = (UserBean ) request.getSession().getAttribute( " userBean"); if (userBean != null) { this.address = userBean .getAddress(); this.name = userBean .getName(); } }
Confidential
18
The Views (cont.) public String toString() { StringBuffer buf = new StringBuffer(); buf.append("address =").append(address).append(" "); buf.append(“name=").append(name).append(" "); return buf.toString(); }
Using an ActionForm in an Action public ActionForward execute( ActionMapping mapping,ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception{ String address = ((UserForm)form).getAddress(); String name = ((UserForm)form).getName();
Confidential
19
Managing Errors
ActionErrors In Action ActionErrors errors = new ActionErrors(); errors.add("propertyname", new ActionError("key"); saveErrors(request, errors); Output error messages in JSP :
Confidential
20
Managing Errors (cont.)
In ApplicationResources
errors.header=Validation Error
You must correct the following error(s) before proceeding:
errors.prefix=- errors.suffix=
errors.footer=
errors.required={0} is required. errors.minlength={0} cannot be less than {1} characters. errors.maxlength={0} cannot be greater than {1} characters. errors.invalid={0} is invalid. errors.byte={0} must be a byte. errors.short={0} must be a short. errors.integer={0} must be an integer. errors.date={0} is not a date. errors.range={0} is not in the range {1} through {2}. errors.creditcard={0} is an invalid credit card number. errors.email={0} is an invalid e-mail address. error.session.expired=Session expired.
Confidential
21
Struts Validator
Required Packages commons-validator.jar and jakarta-oro.jar Add validator-rules.xml ,validate.xml to struts-config.xml
Client-side validations
Confidential
22
Struts Validator (cont.)
Client-side validations
Confidential
23
Struts Validator (cont.)
Server-side validations public class LogonForm extends ValidatorForm {
Basic validators Declare in validation.xml
Confidential
24
Struts Validator (cont.)
Server-side validations
Confidential
25
Internationalization
Add Message Resources into struts-config.xml
parameter="fr.paris.dasco.pvp.ApplicationResources"/>
ApplicationResources_xx.properties Ex: ApplicationResources_fr.properties for French ApplicationResources_ja.properties for Japannese
Using the Tag With : app.title = Struts Ex Defined in ApplicationResources. properties
Confidential
26
Struts JDBC connection pool What Is a DataSource? A JDBC DataSource is an object described by the JDBC 2.0 extensions package that is used to generically represent a database management system (DBMS). The DataSource is defined by the interface javax.sql.DataSource; it is described as a factory containing JDBC Connection objects.
Confidential
27
Struts JDBC connection pool (cont.)
Using a DataSource in Your Struts Application Add jdbc2_0−stdext.jar in lib In struts-config.xml
Confidential
28
Struts JDBC connection pool (cont.) Using in Action //import datasource import javax.sql.DataSource; import javax.servlet.ServletContext; import java.sql.Connection; …………………………
ServletContext context = servlet.getServletContext(); DataSource dataSource = (DataSource) context.getAttribute("jdbc/oracle_pvp"); Connection conn = dataSource.getConnection();
………………………………
Confidential
29
Struts Tools
Camino Pro 3.1.1 (http://www.scioworks.com/) Create the struts skeleton. Convert JSPs to struts format. Generate Action and ActionForm Generate web.xml and struts-config.xml ...... Struts Studio
Confidential
30
References
http://jakarta.apache.org/struts/ Ebook : Mastering Jakarta Struts, Struts in Action, Jakarta Struts...
Confidential
31
The end
Thank you for your attention!
Confidential
32