Java Web Development with Struts and Tiles

Presentation Goal
Provide enough understanding for someone new to start leveraging Struts. Dig deep with several nuggets tips for attendees well versed in Struts.

Presentation Agenda
Struts Intro Struts Fundamentals Struts Tags / Working with Forms Struts Validation Tiles

Understanding the JSP Models
With a Quick Look at Java Web Apps

Java 2 web application options: – Servlets

Java 2 Web Applications
• + Great for Java people • - Difficult to manage graphical changes in HTML layout.

– JSP
• + Great for web developers • - Seductive tendency to write logic in the JSP page.

JSP Model 1
Web applications where JSP pages are used for every aspect of the development.
Option 1 Option 2

JSP Model 1 Observation
JSP1 JSP2 JSP3 JSP4

The Good – Easiest Solution The Bad – Presentation and Logic are mixed. The Ugly – No reuse possibilities

Presentation Logic Ctrl Presentation Presentation Logic Ctrl Present. Present. Logic

Web applications where JSP pages are used for the GUI aspect of the web development and the logic of the application is placed in the servlets it posts to.

JSP Model 2

Model 2
Logic Control

The Good
– Reuse opportunities
• Other application may be able to use the same code.

JSP1 JSP2

JSP3

The Bad
– There is no longer a one to one mapping from a view to a single source of code.

The Ugly
– Takes more forethought.

Introduction MVC

What Is a MVC?
MVC stands for model / view / controller. – A software pattern where logic is separated from the model and view in order to provide for better reuse possibilities. – A software pattern recognized in the early days of small talk. – Documented in the GoF book.

1

Web Application MVC Pattern Controller
(Servlet) 2 Model (Beans)
Model
– Information is provided in objects or beans

View
– The JSP provide the view

3 5

4 View View View (JSPs) View (JSPs) View (JSPs) (JSPs) (JSP)

Controller
– Servlet provides control logic and becomes the controller

MVC Collaboration Diagram
Controller
1: Post Browser Servlet 3: Establish bean state, then place in session or request object 2: Retrieve Data Data Res ource

4: redirect to appropriate view

5: Access beans JSP Beans

The View

The Model

Struts As a MVC Framework
We Can Rebuild It. We Have the Technology. Better… Stronger… Faster...

What Are Struts?
Apache’s open source web application model view controller framework project! Takes MVC to the next level for web applications.

2: Get Mapped Action 1: Pos t Browser

Struts Collaboration Diagram
strut-c onfig. xml 3: Invoke mapped Act ion Bean

Controller
4: Retrieve Data ActionBean Data Resource

ActionServlet

Front Controller
7: redirect to appropriate view 5: Establish bean state, t hen place in session or request object

6: Establish Form State

JSP 8: Get View Information

ActionForm

Beans

The View

The Model

<web-app> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml </param-value> … </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>

ActionServlet

ActionServlet is provided by the framework. The Servlet must be mapped in the web.xml file. – Must have configuration file mapped Lastly, Map the *.do URI to the Action Servlet

<struts-config> <!-- ========== Data Source Configuration === --> <!-- ========== Form Bean Definitions === --> <form-beans> <form-bean name="LogonForm" type="com.codementor.LogonForm"/> </form-beans> <!-- ========== Global Forward Definitions === --> <global-forwards> </global-forwards>

strut-config.xml
XML configuration file Allows for: – DataSource definition

<!-- ========== Action Mapping Definitions === --> <action-mappings> <action path="/logon" type="com.codementor.LogonAction" name="LogonForm" scope="request" input="/logon.jsp"> <forward name="success" path="/sucess.jsp"/> <forward name="failure" path="/failure.jsp"/> </action> </action-mappings> </struts-config>

– Logical name definitions for Forms – View mappings
• Local • Global

Strut-config.xml
<action path="/logon“ type=“com.codementor.LogonAction”

For requests that hit URL=“/logon” The frame work will invoke execute() on an instance of class com.codementor.LogonAction Store request parameters in form variable “LogonForm” which is defined in another location in the xml document.

name="LogonForm"

<forward name="failure" path="/failure.jsp" /> <forward name="success" path="/success.jsp" /> </action>

If the logical name returned by execute() is “failure” go to page “/failure.jsp” If the Logical name returned by execute() is “success” go to “/success.jsp”

Action Bean
import org.apache.struts.action.*; public class LogonAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { LogonForm theForm = (LogonForm)form; String forward="failure"; if(theForm.getUserName().equals("borcon")) { forward="success"; } return mapping.findForward(forward); } }

Action class’s perform is invoked by Action Servlet

Action Form
import org.apache.struts.action.ActionForm; public class LogonForm extends ActionForm { private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public void setPassword(String password) { this.password = password; } public String getPassword() { return password; } }

Action Form has properties which map to the HTML page. Additionally the form can: – Reset – Validate

Strut Powered JSP <html:html locale="true">
<head> <title>Logon Form</title> <html:base/> </head> <body bgcolor="white"> <html:errors/> <html:form action="logon" focus="userName"> User Name: <html:text property="userName" size="16" maxlength="16"/> password: <html:password property="password" size="16" maxlength="16" <html:submit property="submit" value="Submit"/> <html:reset/> </html:form> </body> </html:html>

Creating Your First Struts Application

Struts Installation
Downlad the zip – http://apache.mirrors.hoobly.com/jakarta/strut s/binaries/jakarta-struts-1.1.zip – Unzip and go! – There isn’t an “install”, however there are several files you’ll need. – Readme.txt has details Main Files of interest – *.jar , especially struts.jar – *.tld – struts-blank.war

Steps to Building
Step 1: Build your JSP in HTML format – It’s back to editing code Step 2: Convert to Struts format Step 3: Write the matching ActionForm – public class LogonForm extends ActionForm {} Step 4: Write the Action class – public class LogonAction extends Action {}

Steps to Building
Step 5: Register the entries in struts-config.xml
<action path="/logon" type="com.codementor.struts.LogonAction" name="logonForm" input="/Logon.jsp" > <forward name="success" path="/Success.jsp" /> <forward name="failure" path="/Failure.jsp" /> </action>

Step 6: Configure web.xml with the ActionServlet

JSP Pages
Three Pages – Login Page – Success Page – Failure Page

Logon Page – HTML Version
<html> <head> <title>Login Form</title> </head> <body bgcolor="#ffffff"> <form action="logon.do"> User Name: <input type="text" name="userName" size="16" maxlength="16"/><br /> Password: <input type="text" name="password" size="16" maxlength="16"/><br /> <input type="submit" name="Submit" value="Submit">

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

Logon Page – Struts

<html:html> <head> <title>Login Form</title> Added a cool feature </head> <body bgcolor="#ffffff"> <html:form action="logon.do" focus="userName"> <br> User Name: <html:text maxlength="16" property="userName" size="16"/><br /> Password: <html:text maxlength="16" property="password" size="16"/><br /> <html:submit value="Submit" property="Submit"/>

Success and Failure Pages
Success.jsp <html> <head> <title> Success </title> </head> <body bgcolor="#ffffff"> <h1> Successful Login </h1> </body> </html> Failure.jsp <html> <head> <title> Failure </title> </head> <body bgcolor="#ffffff"> <h1> Failed Login </h1> </body> </html>

Create Form Bean Class
package com.codementor; import org.apache.struts.action.ActionForm; public class LogonForm extends ActionForm { private String password; private String userName; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; }

Form Bean Config
<struts-config> <form-beans> <form-bean name="logonForm" type="com.codementor.LogonForm" /> </form-beans> … </struts-config>

package com.codementor; import org.apache.struts.action.*; import javax.servlet.http.*;

Action Class

public class LogonAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { LogonForm logonForm = (LogonForm) form; String forward = "failure"; if(logonForm.getUserName().equals("mentor")) { forward = "success"; } return mapping.findForward(forward); } }

Action Class Config
… <action-mappings> <action path="/logon" type="com.codementor.LogonAction" name="logonForm" input="/Login.jsp" scope="request" /> </action-mappings> </struts-config>

… <action-mappings> <action path="/logon"

Map The Forwards

type="com.codementor.LogonAction" name="logonForm" input="/Login.jsp" scope="request" > <forward name="success" path="/Success.jsp" /> <forward name="failure" path="/Failure.jsp" /> </action> </action-mappings> </struts-config>

<web-app> <servlet> <servlet-name>action</servlet-name> <servletclass>org.apache.struts.action.ActionServlet</servletclass> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</paramvalue> </init-param> Sets the logging level for struts <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> Loads ActionServlet on startup ** important if there is a JSP page which could be referenced from the Client **

Struts in web.xml

Web.xml URI Mapping
<servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>

Configure Tags
… <taglib> <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri> <taglib-location>/WEB-INF/strutsbean.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> <taglib-location>/WEB-INF/strutshtml.tld</taglib-location> </taglib> </web-app>

Configure WebApp /sample
Logon.jsp Success.jsp Failure.jsp /WEB-INF web.xml struts-config.xml *.tld – all the struts tld files /lib *.jars - all the struts jars /classes LogonAction LogonForm

Run The WebApp

Demo
Example-Login

Summary
JSP / Servlet Models MVC Struts Basics Creating a Struts Application

Questions?

Struts Fundamentals

Section Agenda
Form Beans Action Classes Struts Configuration File

Form Beans

Form Bean Types
ActionForm Sub-class to create a standard form ValidatorActionForm Sub-class in order to use the validator framework DynaActionForm Generic Form Form is defined in xml DynaValidatorActionForm – DynaAction form which can be used with the validator framework

ActionForm Class
Basic Type Follows Rules for JavaBean – Collection of properties Methods – Reset() – Validate() Eg -ActionForm
public class LogonForm extends ActionForm {}

FormBean Reset() Method
public void reset(ActionMapping mapping, HttpServletRequest request) { } Provides way to reset the form bean between action requests Not needed if the form bean is at the Request Scope (Session is the default) Think of as the constructor or the init() of the bean.

FormBean Validate() Method
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) struts-config.xml { <action return null; path="/logon" }
type="com.codementor wUsed to provide validation …" wOnly called if configured in the struts-config.xml file. name="logonForm" wCalled prior to the action class input="/Login.jsp" wReturn null is the default, scope="request" meaning no errors validate="true“ />

ValidatorActionForm
Supports the Validator Framework Discussed in the Validation Section of the course – There is a lot of detail in working with the validator frame work. Eg-ValidatorActionForm
public class LogonForm extends ValidatorActionForm {

A form without a Form There is no Java class to create The form is configured in the struts-config file.

DynaForms

Example LogonForm <struts-config> <form-beans> <form-bean name="LoginForm" type="org.apache.struts.action.DynaActionForm "> <form-property name="userName" type="java.lang.String" /> <form-property name="password" type="java.lang.String" /> </form-bean> </form-beans>

Working with DyanForms
Handling DynaForms in an Action Class is a little different. – There are no strongly type properties. – Consider a HashMap where the configured name is the key Example: DynaActionForm form = (DynaActionForm)actionForm; form.get("userName"); Eg- DynaActionForm

DynaValidatorActionForm
Work the same as DynaActionForms Provide ability to use the validator framework.

Actions

Action Types
Actions DispatchActions LookupDispatchActions ForwardActions TilesActions

Action Characteristics
Typically have one method, the execute method. – Models after the command pattern Dissimilar from a Servlet – Lacks the doGet / doPost characteristics – Requires a different design as compared to Servlets
• May require an Action for setting up display and an Action for retrieving POSTs • May programmatically determine the action in the Action class • May use one of the DispatchActions

Action Class
Super-class to all Actions public class LogonAction extends Action { Required method for Actions public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {

Action Configuration
<action-mappings> <action input="/Login.jsp" name="logonForm" path="/logon" scope="request" type="com.codementor.LogonAction" > <forward name="success" path="/Success.jsp" /> <forward name="failure" path="/Failure.jsp" /> </action> </action-mappings>

DispatchAction
Super-class for all Dispatch type actions public class LogonAction extends DispatchAction { Provides ability to have multiple functions per Action – It has an execute method, because it is an Action, but do NOT override it. Create methods with arbitrary names with signature similar to the execute method Eg-DispatchAction

DispatchAction
public ActionForward <method.name>(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { Must have a parameter type configured – Defined in the struts-config file, determines the “dispatched” method

Example DispatchAction
Let’s change our LogonAction to be able to login and to sendPassword. Methods defined in the LogonAction public ActionForward login(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { public ActionForward sendPassword(ActionMapping mapping, …

DispatchAction Config
<action name="logonForm" path="/logon" input="/Login.jsp" scope="request" type="com.codementor.LogonAction" parameter="action"> <forward name="success" path="/Success.jsp" /> <forward name="failure" path="/Failure.jsp" /> Defines the parameter </action>
used to get the action The name “action” is arbitrary, could be anything

Invoking the dispatched method: localhost:8080/sample/logon.do?action=login localhost:8080/sample/logon.do?action=sendPassword

When to use DispatchActions
Multiple cohesive actions – CRUD Consolidation of the logical model – If you have multiple Actions:
• With the same backend relationships • Working with the same page • Combination of these

LookupDispatchAction
It is a DispatchAction and carries all it’s characteristics and configuration requirements. DispatchAction exposes in the URI the method name of the action localhost:8080/sample/logon.do?action =login Eg-LookUpDispatchAction

LookupDispatchAction
LookupDispatchAction provides a configurable way to define logical name mappings to physical method names, which are not exposed to the public. – Requires no extra configuration in the strutsconfig Required method to implement in the LookupDispatchAction protected Map getKeyMethodMap() {

LookupDispatchAction Ex.
Using the previous DispatchAction, it’s configuration and method names protected Map getKeyMethodMap() { Maps to hashmap Map map = new HashMap(); map.put(“login.button", “login"); which maps to method to invoke map.put(“send.password.button", “sendPassword"); return map; This mapping occurs in the } property file. Property File: login.button=Login send.password.button =Send Password

Invoke LookupDispatch
http://localhost:8080/sample/login.do?actio n=Login Internal to the framework the parameter is reverse mapped to the key. The key then is used to invoke the method.

ForwardAction
Used to forward to another resource – Useful in Tiles examples Two approaches, provide the same result – Define the predefined ForwardAction <action path="/home" type="org.apache.struts.actions.ForwardAction" parameter="index.jsp"/> – Short-Hand XML <action path="/home" forward="index.jsp"/> Eg-ForwardAction

TilesAction
Used to provide controller logical to tiles. – Explained in the Tiles Section of the course. Has a slightly different execute signature public ActionForward execute(ComponentContext context, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

Forwards

Refers to the forwards defined per action <action-mappings> <action input="/Login.jsp" name="logonForm" path="/logon" scope="request" type="com.codementor.LogonAction" > <forward name="success" path="/Success.jsp" /> <forward name="failure" path="/Failure.jsp" /> </action> </action-mappings> Name is a logical String Path is a view element is – Relative Resource (such as Success.jsp) – Tiles definition – Can be any URL

Local Forwards

Global Forwards
Global Forwards are forwards which are declared in the global section of the struts-config file. Reduces multiply declared local forwards Great to use with the <html:link> tag (discussed later) <struts-config> … <global-forwards> <forward name="Home" path="/viewHome.do" /> <forward name="Logon" path="/Logon.jsp" /> </global-forwards>

Forward to Actions
It is common to have a forward go to another action class. <forward name="failure" path="/failure.do" />

Eg-Redirect By default forwards are forwards and not redirects. To redirect the client <forward name="success" path="/Success.jsp" redirect="true“ />

Forward Redirects

Struts Configuration File

Struts Configuration File
Defines the configuration for the Struts Application Module Configured in the web.xml file for the ActionServlet By convention usually named strutsconfig.xml This section will walk through all the XML elements in the struts configuration file.

Struts-Config.xml
DataSources FormBeans Global Exceptions Actions Controller Message Resources PlugIns

Don’t use it, but here’s how 

Data Sources

<data-sources> <data-source> <set-property property="driverClass“ value="org.postgresql.Driver"/> <set-property property="password“ value="mypassword"/> <set-property property="url“ value="jdbc:postgresql://host/mydatabase"/> <set-property property="user“ value="myusername"/> </data-source> </data-sources>

Form Beans
Location where are form beans are defined. Provides a logical name mapped to either: – Physical ActionForm Class – DynaActionForm Class DynaActionForms define there form beans structures. <form-beans> <form-bean name="logonForm" type="com.codementor.LogonForm" /> </form-beans>

Struts defines a way to do declarative exception handling – Exceptions are mapped to forward paths – Can be in combination with exception keys. Global exceptions are leverage if there isn’t a configured local exception handler. <global-exceptions> <exception type="app.ExpiredPasswordException" path="/changePassword.jsp"/> </global-exceptions>

Global Exceptions

Global Forwards
Location of forwards defined to be global. Provides default forward. <global-forwards> <forward name="Home" path="/viewHome.do" /> <forward name="Logon" path="/Logon.jsp" /> </global-forwards>

Action Mappings
Action mappings is where the actions are configured. Maps the action to the form bean. Provides local forwards. <action name="logonForm" path="/logon" input="/Login.jsp" scope="request" type="com.codementor.LogonAction" parameter="action"> <forward name="success" path="/Success.jsp" /> <forward name="failure" path="/Failure.jsp" /> </action>

Controller
Configuration of the RequestProcessor for this struts application module. Provides customization to requests. Commonly used for Tiles. <controller processorClass="org.apache.struts.tiles. TilesRequestProcessor" />

RequestProcessor
Provides access to preprocessing to all requests – No need to extend the ActionServlet Provides hooks into – ActionCreate – ActionForm – ActionForward – Processing:
• • • • • Exceptions Forwards Locale Path Populate

Message Resources
Location to Configure application resources. Used for Internationalization <message-resources parameter="com.codementor.Application Resources" />

Plugins
Used to configure Plugins for the application Plugins provide startup and shutdown functionality through – Init() – Destroy Commonly used for Tiles and Validation Framework <plug-in className="org.apache.struts.tiles.TilesPlugin"> <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" /> </plug-in>

Summary
Form Beans Action Classes Struts Configuration File

Questions?

Working Struts Tags

Section Agenda
Common Tags Working with Forms Useful Struts Java Classes

Common Tags

html:form html:img html:image html:errors html:link html:multibox html:radio html:checkbox html:text html:select html:options html:password

Most Common Struts Tags
bean:write bean:define logic:iterator logic:match logic:equal logic:notEqual Nested versions of all these tags tiles:insert tiles:put

Common Non-Form Tags
Define Links Images Write Message Iterator Nested Tags

Bean:Define
Use for: – Access to bean not in the scope of an html form – To provide clarity when working with multiple nested properties – To provide script variable for included JSP pages. Does NOT create an instance of a bean – It provides access to a bean in scope Don’t use to define a form bean to a new script variable name

Bean:Define
<bean:define id=“employeeList“ name=“selectEmployeesForm“ property=“employeeList“ type="java.util.Collection"/> Defines a referenceable variable named employeeList which is assigned that value Of the property employeeList in the form Bean.
Collection employeeList = selectEmployeesForm.getEmployeeList();

Struts Links
Preferred order to provide a link in Struts – Forward
• <html:link forward=“Home”> • Uses the defined global forwards from the struts configuration file.

– Page
• <html:link page=“/someAction.do”>

– HREF
• <html:link href=“”> • Required for javascript • <html:link href=“javascript:history.back(1)”>Back </html:link>

– Action
• <html:link action=“someAction”

Links Example
Text Link
<html:link forward=“Home">Home Page</html:link>

Image Link
<html:link page="/viewReport.do"> <html:img src="/images/emp_button_view_report.gif" /> </html:link>

Links which depend on information available through the form bean <html:link page="/viewReport.do" paramId="model“ paramName="viewModelForm" paramProperty="model"> <html:img src="/images/emp_button_view_report.gif" /> </html:link>

Parameterized Links

Parameterized Link
Will create a link on the image to: /viewReport.do?model=<%=viewModelF orm.getModel() %> Which may look like: /viewReport.do?model=detail

Images
There are 2 images tags – html:img
• Used for normal images • Used with links

– html:image
• Used for images which function as submit buttons • Must be in an struts form

<html:img>
<html:link page="/viewReport.do"> <html:img src="/images/emp_button_view_report.gif" /> </html:link> Or Simply <html:img src="/images/emp_button_view_report.gif" />

<html:image>
<html:form action=“/action.do”> <html:image src="/images/emp_button_view_report.gif " /> </html:form>

Used to retrieve a property from: – a form bean – a defined scripted variable (by bean:define) <bean:write name=“modelForm” property=“clientName”/>

Bean:write

Bean:Write
If the named object is an object you do not have to define a property Ex: <bean:write name=“id” /> This commonly happens with a bean define or an id assigned in an iterator

JSP Equivalent: <%= modelForm.getClientName() %>

Displaying HTML on HTML
Struts will not display the source of html of an html page by default Use the bean:write with the filter set to false.

Bean:message
Used to retrieve a property from a configured property file. We’ll look at this in detail in the Internationalization section. <bean:message key=“ user.message”/>

Bean:message
Can be used in combination with a bean (such as a form bean) – In this case the property clientInfo, of the modelForm bean has the key to the resource bundle. – The value of clientInfo is NOT displayed – It is used to lookup the value in the resource bundle which is displayed. <bean:write name=“modelForm” property=“clientInfo”/>

Logic:iterate
Used to iterate – An array – A Collection All tags between the <logic:iterate> open and close tag will execute for each element.

Logic:iterate Example
This would go through all the employees of the employeesForm The employeesForm is the string from the struts config for the form bean that is assigned to the action that forwarded to this JSP. In a few slides we’ll evolve this example to something useful. <logic:iterate id=“employeesForm" property=“employees"> </logic:iterate>

Nested Tags
Most of the tags have a nested version. – Usually reducing the required attributes for the extended tag. Use Often!!! Great for working through an iterator Great for removing form bean dependencies in the JSP page.

Nested Tags Example
Without nested in a form <bean:write name=“modelSummaryForm” property=“clientName”/> With nested <nested:write property=“clientName”/> It assumes the form bean! (When in a form tag)

Nested Iterators
Revisit the Iterator Example: – This time the form bean is assumed – Also works great for writing the employees to the screen. – Assume we have an Employee Class with a FullName property in a Collection which is returned from the getEmployees() method of the form bean <nested:iterate property=“employees"> <nested:write property=“fullName” /> <br/> </nested:iterate>

Working with HTML / Forms

Working with Struts Forms
Struts Forms Submit Buttons Text Input Combo Boxes Radio Checkbox Multi-Box Tables Advance Forms – Dispatch Options – Javascripted submits

<html:form> creates a <form> tag in html. It is a required tag for many of the struts tags – Specifically tags which are used with forms
• Text • Dropdowns

Struts Forms

– JSP pages will NOT compile if these tags are used outside a form tag. Example: – In this case there is special attribute named focus which creates javascript for the client.
• On load, the field with the name userName will have focus

<html:form action=“/logon.do" focus="userName">

Submit Buttons
<html:submit value=“Submit”/> Must be in between html:form tags May have multiple submits. – There are several ways to deal with multiple submits
• Checking different values in an action • Javascript magic • Using the DispatchAction with the property attribute of the submit button

– We’ll address some of these in the advance forms handling section.

Text Fields
<html:text property=“userName” /> This tag performs the following: – On JSP forward
• Gets the value of the form beans getUserName() method • Creates an html input tag • Defaults the value of the input to the value retrieved.

– On Submit
• Gets the value of the input tag • Sets the value of the like named property in the form bean

Combo Boxes
<html:select property=“employee”> Used to create an html select in order to create a combo box

Combo Box
This tag performs the following: – On JSP forward
• Gets the value of the form beans getEmployee() method • Creates an html select tag • Defaults the value of the select tag to the value retrieved. • If there isn’t an option that matches the select choice then it’s up to the browser to decide

– On Submit
• Gets the option selected • Sets the value of the like named property in the form bean

HTML Selects Observations for the example below
– The name of the select is employees – The default value is Ken, Ken will be displayed in the drop down – The list will have Ken, Tammy and Amanda – The value of Tammy is 102 – The value of Amanda is Amanda <select name="employees"> <option value="101" selected="selected">Ken</option> <option value=“102”>Tammy</option> <option >Amanda</option> </select>

Options for the Drop Down
Hard-Coded String – Struts – Html Array of Strings Collections
•You can hard code the value of the dropdown as well. For example:

<html:select property=“employee” value=“Ken”> In this case the value of employee is ignored.

Hard-Coded Options
In this example the options of the select are hard coded in the JSP – The value and name are the same – Produces: <option value=“Ken”>Ken</option> <html:select property=“employee”> <html:options value=“Ken” /> <html:options value=“Amanda” /> <html:options value=“Amelia” /> </html:select>

String Array Options
In this example, there is an employeeList property in the form bean which returns a String array. Each elements of the array will be a name and value of an option. <html:select property=“employee”> <html:options name=“employeeList” /> </html:select>

Collections Options
When working with a collection of Java objects which are not Strings Collection: Identifies the collection property name of the form bean Property: is a property on the object in the collection – Used for the value of the option. – In this case the objects in the collection have a property getEmployeeID() labelProperty: is a property on the object in the collection – Used for the label of the option – In this case the object is getName()

Collections Options
Example: <html:select property=“employee”> <html:options collection=“employeeList” property=“employeeID” labelProperty=“name” /> </html:select>

public class Employee { private String name; private String employeeID; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmployeeID() { return employeeID; } public void setEmployeeID(String employeeID) { this.employeeID = employeeID; } }

Employee Class

Collections Output
<html:select property=“employee”> <html:options collection=“employeeList” property=“employeeID” labelProperty=“name” /> </html:select> Results in: <select name="employee"> <option value="101" >Ken</option> <option value=“102”>Tammy</option> <option value=“102”>Amanda</option> </select> Where employeeID for the Ken object is 101 and Ken is the name

<html:radio property=“status” value=“Full Time” /> <html:radio property=“status” value=“Part Time” /> Used to create an html set of input type radio buttons This tag performs the following: – On JSP forward
• Gets the value of the form beans getStatus() method • Creates an html input tag of type radio • Defaults the value to the radio of the same value

Radio Button

– On Submit
• Gets the value of the radio • Sets the value of the like named property in the form bean

pe=“radio” name=“status” value=“Full Time” >Full Time</input>

pe=“radio” name=“status” value=“Part Time” > Part Time</input>

Check Box
<html:checkbox property=“isUSCitizen” value=“wasUSCitizen” /> Used to create an html input check boxes Properties must be of type booleans

Check Box
This tag performs the following: – On JSP forward
• Gets the value of the form beans wasUSCitizen() method • Creates an html input tag of type checkbox, named isUSCitizen • Defaults the value to the checkbox to the value of wasUSCitizen()

– On Submit
• Gets the value of the checkbox • Sets the value of the isUSCitizen property in the form bean

If you don’t need a default value you don’t need to use a value attribute.

Multibox
Use almost every time you have a SET of checkboxes This provides an easy solution for working with multiple selections. Works with String Arrays NOT booleans. Typically used in an iterator. – Use the multibox tag with the same property name for all checkboxes which are apart of the multibox. Reference: http://husted.com/struts/tips/007.html

Multibox Tag
<html:multibox property="benefits">401K</html:multibox>401K <html:multibox property="benefits">Health</html:multibox>Health <html:multibox property="benefits">Dental</html:multibox>Dental The property benefits is a String array It will have a String element for each property checked. It can also be used to default the values on form display <input type="checkbox" name=" benefits" value="401K">401K <input type="checkbox" name=" benefits" value=“Health">Health <input type="checkbox" name=" benefits"

Multi-Box Example
<logic:iterate id=“employee" property=“employees"> <html:multibox property=“selectedEmployees"> <bean:write name=“employee“ property=“employeeID”/> </html:multibox> <bean:write name=“employee“ property=“name”/> </logic:iterate>

Multi-Box Example
Using the Employee Object previously discussed The iterate tag will go through all Elements of the collection provided by getEmployees() Each element will be referenced by the name “employee” There will be a checkbox created for each element – Value = employeeID – Text = name

This is more about working with rows of data. Displaying our employee information in a table. <table> <tr><th>Employee ID</th><th>Employee Name</th></tr> <logic:iterate id=“employee" property=“employees"> <tr> <td><bean:write name=“employee“ property=“employeeID”/></td> <td><bean:write name=“employee“ property=“name”/> </td> </tr> </logic:iterate> </table>

Working with Tables

Tables With Nested Tags
<table> <tr><th>Employee ID</th><th>Employee Name</th></tr> <nested:iterate property=“employees"> <tr> <td><nested:write property=“employeeID”/></td> <td><nested:write property=“name”/> </td> </tr> Produces the same results </nested:iterate> Nested Iterate tag, creates a </table> temporary ID for us. The nested writes, use the temporary ID.

Advanced Forms

DispatchActions
Using the submit buttons to select which dispatch action to use: Assume: – Struts-Config.xml <action name=“Employee" type="com.codementor.EmployeeAction" parameter="action" scope="request" path="/employee"> – Action Class has the following methods:
• add() • save()

Dispatch JSP
<html:form action="/employee.do"> <html:submit value="add" property="action"/> <html:submit value="save" property="action"/> </html:form>
Produces a /employee.do?action=add For the add submit button

JavaScript Action Switch
Example: <html:image src=“images/emp_button_run_model.gif” onclick=“document.forms[0].action=‘runMod el.do” border=“0” />

Struts Classes

Struts Classes
ImageButtonBean LabelValueBean RequestUtil Jakarta Commons – BeanUtil

ImageButtonBean
Provides the X and Y coordinates of where on an image the mouse was clicked. Properties: –X –Y – isSelected A Property of this type in the form bean can provide this information

LabelValueBean
Utility Class to provide a different display name from the selection name. How do we produce the following html: <select name=“listType”> <option value=“ST”>Standard <option value=“SL”>Select </select>

LabelValueBean Ex
JSP Code <html:select property=“type”> <html:options collection=“listTypes” property=“value” labelProperty=“label” /> </html:select> Java Code Form.addListTypes(new LabelValueBean(“Standard”,”ST”)); Form.addListTypes(new LabelValueBean(“Select”,”SL”));

RequestUtil
Utility for working with the request object. Provides: – actionURL – absoluteURL – createActionForm – URL encoding – Access to ActionErrors – Access to Action Mappings – Printable URL

BeanUtil
Was part of Struts 1.0 Now part of Jakarta Commons – Which is included with Struts 1.1 Great Utility to copy values of the properties of one bean to the values of the properties of the same name of another bean.

Summary
Common Tags Working with Forms – Most data input tags were discussed. Useful Struts Java Classes

Questions?

Struts Validation

Section Agenda
Simple Bean Validation Understanding Validation Framework Business Level Validation

Validation Opportunities
Client-Side – javascript Server-Side – Form Bean – Action Class – Validator Framework

Client Side Validation
In addition to your own javascript, Struts provides the ability to generate javascript based on configuration. – Discussed further in validator framework Javascripts Pros: – Convenience to the client – Provides a network throttle Javascript cons: – Multi-Browser support

Server-Side Validation

Server-Side Validation
Common Validation Classes and &Configuration Form Bean Validation Action Bean Validation

&Validation Framework Classes
ActionErrors – Used in struts to contain all the errors from a validation point ActionError – A specific validation error, which will be put in the ActionErrors – Contains an error key (which may be used with the resourse bundle)

Resource Messages
Application.properties file configured with the message-resources tag in the struts-config.xml
Struts-config.xml file snip: <message-resources parameter="com.codementor.ApplicationResources" /> ApplicationResources.properties file:

error.username.required=Username is require error.password.required=Password is required

A Look at MessageFormat
It’s required for the validation framework to under the java.text.MessageFormat class. Provides a way to have parameterized message – Single digit values from 0-9 are used as place holders in a String text.
• {0},{1},{2},…

– These provides the ability to have a generic message where specifics can be inserted in the message at a latter time.

Message Format Ex.
Example Message: String message = "The disk named {0} contains {1} files."; String[] args = {"Foo","300"}; System.out.println(MessageFormat.forma t(message,args)); Output: The disk named Foo contains 300 files.

Server-Side Validation
Common Validation Classes and Configuration Form Bean Validation Action Bean Validation

Form Bean Validation
In order to validate a form bean the following conditions must exist: – The validate method of the form bean must be implemented. – The form bean must be configured in the configuration file to use validation
• This is configured by action class.

The validation occurs prior to the action class being invoked. – If there is an error the client is redirected to the configured input page.

Validate() Method public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if ( (userName == null) || (userName.length() < 1)) { errors.add("userName", new ActionError("error.username.required")); } return errors; }
ApplicationResources.properties file:

error.username.required=Username is requ error.password.required=Password is requir

Validate Method
Return ActionErrors.size() == 0 or return null if there are no errors Add Errors to the ActionErrors for each error The keys map to the properties in the configured property file.

<action input="/Login.jsp" name="logonForm" path="/logon" scope="request" type="com.codementor.LogonAction" validate="true“ > <forward name="success" path="/Success.jsp" redirect="true" /> <forward name="failure" path="/Failure.jsp" /> </action> The validate = true will cause the validate() method to be invoked on the form bean that is configured for this action. This occurs when this action is invoked from the client side, but just prior to the actual execution of the action class.

Bean Validation Config

Server-Side Validation
Common Validation Classes and Configuration Form Bean Validation Action Bean Validation

Action Bean Validation
Validation in the Action class Requires a little more handling on the developers part Typical Reasons: – Business logic validation – Navigation dependencies
• Forwards needs to go somewhere other than the input.

Example Action Validation
public ActionForward execute( …) { LogonForm form = (LogonForm) actionForm; ActionErrors errors = new ActionErrors(); if ( form.getUserName().length() < 1) errors.add("userName", new ActionError("error.username.required")); if ( form.getPassword().length() < 1) errors.add("password", new ActionError("error.password.required")); saveErrors(request,errors); return mapping.findForward(forward); // the forward would need to be set }

Validation Framework

Validation Framework
Configured in XML Rules defined in XML Pre-defined validators User-defined validators Can be configured to provide javascript to the client Must use the ValidatorForm Works with Resource Bundle

XML Configuration
Although there can be any number of files used for validation configuration, there are typically 2: – Validation-rules.xml
• Contains the xml defined rules • Typically generic / Non application specific • Provided with the struts download

– Validation.xml
• Configuration of the rules from the first xml file, to the forms and fields defined in the struts-config file. • Typically application specific.

Validation-rules.xml
<validator name="required" classname="org.apache.struts.validator.FieldChecks " 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"> <javascript><![CDATA[ …

Configure Validation Example
Create the Validation.xml Configure the struts application to use the validation framework Create or Copy the applications.properties file Configure the application.properties file in the struts configuration file Configure the output location to show the error

Validation.xml Username Ex
<form-validation > <formset> <form name="logonForm" > <field property="userName" depends="required" /> ApplicationResources.properties file: </form> errors.required={0} is required </formset> </form-validation >

Validation Config
Entry in the struts-config.xml file. Configured with the ValidatorPlugin. The pathnames properties is just a comma separated list of all the xml files that are needed for this applications validation purposes. <plug-in className="org.apache.struts.validator.Validat orPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEBINF/validation.xml" /> </plug-in>

Application.properties File
#Struts Validator Basic Error Message 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.

Property File Configuration
Configure the property file in the strutsconfig.xml file This example assumes that it is the WEBINF/classes/com/codementor directory of the web application. <message-resources parameter="com.codementor.Application" />

Displaying Strut Errors
<html:errors/> – Errors are displayed where this tag is placed in the JSP file. <html:errors property=“userName”/> – Placing errors close to the property of choice – Displays only the errors for this property

Pre-Defined Validators

Pre-Defined Validators
Required minLength maxLength Range Date Basic types (integer, long, double, float ) Email – Validates that the email could be valid creditCard – Validates that the number could be valid Mask – Used to check regular expression

Default Property Keys
#Struts Validator Basic Error Message 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.

Default Property Keys
errors.date={0} is not a date. errors.range={0} is not in the range {1} through {2}. errors.creditcard={0} is not a valid credit card number. errors.email={0} is an invalid e-mail address.

MinLength Configuration
<field property=“userName“ depends="minlength"> <arg0 key=“userName“ resource=“false”/> <arg1 name="minlength" key="${var:minlength}" resource="false"/> <var> <var-name>minlength</var-name> <var-value>5</var-value> </var> </field> ApplicationResources.properties file:
errors.minlength={0} can not be less than {1} characters.

MaxLength Configuration
<field property=“userName“ depends="maxlength"> <arg0 key=“userName“ resource=“false”/> <arg1 name="maxlength" key="${var:maxlength}" resource="false"/> <var> <var-name>maxlength</var-name> <var-value>5</var-value> </var> </field> ApplicationResources.properties file:
errors.maxlength={0} can not be greater than {1} characters.

<field property=“priorty“ depends=“range"> <arg0 key=“responseForm.priority.displayname“ /> <arg1 name=“range" key=“${var:min}” resource="false"/> <arg2 name=“range" key=“${var:max}” resource="false"/>
<var> <var-name>min</var-name> <var-value>1</var-value> </var> <var> <var-name>max</var-name> <var-value>4</var-value> </var>

Range Configuration

</field> errors.range={0}

ApplicationResources.properties file:
is not in the range {1} through {2}.

Date Configuration
<field property=“date“ depends=“date"> <arg0 key=“responseForm.date.displayname“ /> <var> <var-name>datePattern</var-name> <var-value>MM/dd/yyyy</var-value> </var> </field> ApplicationResources.properties file:
errors.date={0} is not a date.

Double Configuration
<field property=“amount“ depends=“double"> <arg0 key=“responseForm.amount.displaynam e“ /> </field>
ApplicationResources.properties file:
errors.double={0} must be an double.

CreditCard Configuration
<field property=“creditCard“ depends=“creditCard"> <arg0 key=“responseForm.creditCard.displayname “ /> </field>
ApplicationResources.properties file:
errors.creditcard={0} is not a valid credit card number.

Mask Configuration
<field property=“postalCode“ depends=“mask"> <arg0 key=“responseForm.postal.displayname“ /> <var> <var-name>mask</var-name> <var-value>^\d{5}\*$</var-value> </var> </field> ApplicationResources.properties file:
errors.invalid={0} is invalid.

Common Masks
Phone – ^\(?(\d{3})\)?[-| ]? (\d{3})[-| ]?(\d{4})$ Zip – ^\d{5}\d*$ – ^\d{5}(-\d{4})?$

Additional Validation Configuration Options

Using Global Constants
It is possible to define global constants in the validation configuration file. <form-validation> <global> <constant> <constant-name>zip</constant-name> <constantvalue>^\d{5}\d*$</constant-value> </constant> <var> </global>
<var-name>mask</var-name> <var-value>${zip}</var-value> </var>

Configure Multiple Rules
<form-validation > <formset> <form name="logonForm" > <field property="userName“ depends="required,mask" > <arg0 key=“logon.username.displayname” /> <var> <var-name>mask</var-name> <var-value> ^[a-zA-Z0-9]*$</var-value> </var> …

Configure a Message
<form-validation > <formset> <form name="logonForm" > <field property="userName“ depends="required,mask" > <msg name=“mask” key=“logon.username.maskmsg”/> <arg0 key=“logon.username.displayname” /> <var> <var-name>mask</var-name> <var-value> ^[a-zA-Z0-9]*$</var-value> </var> …

Summary
Different levels of validation Leveraging the validation framework Pre-Defined Validators

Questions?

Struts Tiles

Presentation Agenda
Tiles Configuration Tile Definitions and Extensions

Why Tiles
Frame look without frames Consistent look across application Reduce maintainance A better way to “include” a JSP file

Eg-LoginDemo

What is a Tile?
An area or region with in a web page.
Header Left Nav Body Footer

Configuration Requirements
Struts Configuration – TilesRequestProcessor – TilesPlugin Tiles Definition XML File Required Jars – tiles.jar – commons-digester.jar – commons-beanutils.jar – commons-collections.jar – commons-logging.jar

TilesRequestProcessor
Extends the RequestProcessor to provide per request processing of the forwards. – Evaluates the forward to see if it is a tile forward. Required for Tiles to work Struts-config.xml
<struts-config> <controller processorClass="org.apache.struts.tiles.TilesReques tProcessor" /> </struts-config>

TilesPlugin
Checks to see that the configured controller is an instance of the TilesRequestProcessor. If it is not it will remove the request processor and replace it with the TilesRequestProcessor. Struts-Config.xml File
<plug-in className="org.apache.struts.tiles.TilesPlugin"> <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" /> </plug-in> Definitions-config is a required property for the TilesPlugin

TilesPlugin Parameters
definitions-config

– Used to configure the tiles xml files. – Example: “/WEB-INF/tiles-defs.xml“
definitions-debug

– Sets logging level for debug informion
• 0 = No Debug (default) • 1 = Partial Debug • 2 = Full Debug

TilesPlugin Parameters
definitions-parser-details

– Logging levels for the commons digestor
definitions-parser-validate

– Validates the configured Tiles XML file with the DTD if set to “true”
definitions-factory-class" value="com.codementor.TilesFactory“

– Allows you to specify a custom factory – It must implement the ComponetDefinitionsFactory Interface.

Creating A Tiles Definition File

Tiles Definition File
Typical naming convention: tiles-def.xml Template:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/tilesconfig_1_1.dtd"> <tiles-definitions> … </tiles-definitions>

Tile Definition Tag Details
There is only one tag used under the tilesdefinition node. <definition name=“” … /> Attributes of the definition tag – Name – defines the name of the tile – Path – defines the layout to be used for this tile – Extends – defines a tile to “inherit” properties – controllerClass – class name of a TilesAction, called prior to the page include. – controllerUrl – url of a controller – Roles – if the user is in the specific role it is included, otherwise it is ignored

Definition Tag – Sub Tags
description display-name Icon put

– Used to assign a value to defined attribute in the Tile.
putList

– Used to create a list of assignments for a Tile.

Put Tag
The put tag has no children tags Attributes – name – name of the attribute to provide a value for. – Direct – determines how content is handled, true means content is printed directly
• Another way of specifying the content type

– Type – specify the content type
• String: content is printed directly • Page: content is included from specified URL. Name is URL • Definition: value is the name of a tile definition

– value – value to assign to the attribute

Layouts

A JSP file created to represent the layout of the pages – Usually without content
Layout.jsp defines Header Left Nav Body Footer Definition in the Tile has a JSP page included for it at run time. Header Left Nav Body Footer

Tile Layouts

<html:html> <head></head> <body bgcolor="#ffffff"> <!--Header page information --> <tiles:insert attribute="header"/> <br /> <!--Main body page information --> <tiles:insert attribute="body"/> <br /> <!--Footer page information --> <tiles:insert attribute="footer"/> <br /> </body> </html:html>

Define A Simple Layout

Layout Design header body footer

Understanding the Value of Extends

Closer Look at the Tiles Definition Extensions
The next set of slides demonstration an evolution of thought. The conclusion of which will be a model of how to configure the tile-def.xml file. Steps: – Configuration of a definition – Configuration of a 2nd definition – 1st Example using extends – The creation of an abstract definition – Examples 1 and 2 refactored

Starting Definitions
Example1 Definition <definition name="example1“ path="/Layout.jsp"> <put name="header" value="/header.jsp"/> <put name="footer" value="/footer.jsp" /> <put name="body" value="/example1.jsp" /> </definition> Example1 Definition <definition name="example2“ path="/Layout.jsp"> <put name="header" value="/header.jsp"/> <put name="footer" value="/footer.jsp" /> <put name="body" value="/example2.jsp" /> </definition>

Tiles Definition Extension
By using the Extends attribute, a definition can “inherit” all the “puts” of the inherited definition. <tiles-definitions> <definition name="example1" path="/Layout.jsp"> <put name="header" value="/header.jsp" /> <put name="footer" value="/footer.jsp" /> <put name="body" value="/example1.jsp" /> </definition> <definition name="example2" extends="example1"> <put name="body" value="/example2.jsp" /> </definition> </tiles-definitions>

Abstract Definitions
A definition which by itself is very used. It is not concrete.
– Meaning it doesn’t define everything necessary to display a page.

Useful to refer to as one of the layout of the site Notice there is no body defined.
<tiles-definitions> <definition name="main" path="/Layout.jsp"> <put name="header" value="/header.jsp" /> <put name="footer" value="/footer.jsp" /> </definition>

Abstraction Use
<definition name="example1" extends="main"> <put name="body" value="/example1.jsp" /> </definition> <definition name="example2" extends="main"> <put name="body" value="/example2.jsp" /> </definition> </tiles-definitions>

Additional Configurations

Adding a Title to the Layout
<html:html> <head> <title> <tiles:getAsString name=“title”/></title> </head> <body bgcolor="#ffffff"> <!--Header page information --> <tiles:insert attribute="header"/> <br /> <!--Main body page information --> <tiles:insert attribute="body"/> <br /> …

Title Configuration
<definition name="example1" extends="main"> <put name=“title” value=“Example 1 Page”/> <put name="body" value="/example1.jsp" /> </definition> <definition name="example2" extends="main"> <put name=“title” value=“Example 2 Page”/> <put name="body" value="/example2.jsp" /> </definition>

Action Forward to Tiles
Struts-config.xml file <action input="/Login.jsp" name="logonForm" path="/logon" scope="request“ type="com.codementor.LogonAction" validate="true"> <forward name="success" path="example1" /> <forward name="failure" path="example2" /> </action> Standard forwards where the path is the name of the tile definition. The TilesRequestProcessor determines on the forward that it is a tile definition.

Logon Tiles Example

<html:html> <head><title><tiles:getAsString name="title"/></title></head> <body bgcolor="#ffffff"> <!--Header page information --> <tiles:insert attribute="header"/> <br /> <!--Main body page information --> <tiles:insert attribute="body"/> <br /> <!--Footer page information --> <tiles:insert attribute="footer"/> <br /> </body> </html:html>

Layout.jsp

Header / Footer Pages
header.jsp Welcome to CodeMentor copyright.jsp copyright (c) 2000-2003 Code Mentor, Inc.

Login.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <html:form action="logon.do" focus="userName"> <br> User Name: <html:text maxlength="16" property="userName“ size="16"/><br /> Password: <html:text maxlength="16" property="password“ size="16"/><br /> <html:submit value="Submit" property="Submit"/> <html:reset value="Reset"/> </html:form>

Success / Failure Pages
Success.jsp <h1> Successful Login </h1> Failure.jsp <h1> Failed Login </h1>

Tiles Configuration
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd"> <tiles-definitions> <definition name="main" path="/Layout.jsp"> <put name="footer" value="/copyright.jsp" /> <put name="header" value="/header.jsp" /> <put name="body" /> </definition>

Tiles Config
<definition extends="main" name="login.page"> <put name="body" value="/Login.jsp" /> <put name="title" value="Logon Page" /> </definition> <definition extends="main" name="success.page"> <put name="body" value="/Success.jsp" /> <put name="title" value="Success Page" /> </definition>

<struts-config> <form-beans> <form-bean name="formBean" type="com.codementor.LogonForm" /> </form-beans> <action-mappings> <action path="/loginPage" forward="login.page" /> <action input="/loginPage.do" name="formBean" path="/logon" type="com.codementor.LogonAction"> <forward name="success" path="success.page" /> <forward name="failure" path="failure.page" /> </action> </action-mappings>

Struts-Config.xml

package com.codementor;

LogonForm

import org.apache.struts.action.*; public class LogonForm extends ActionForm { private String password; private String userName; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUserName() { return userName; }

package com.codementor; import javax.servlet.http.*; import org.apache.struts.action.*;

LogonAction

public class LogonAction extends Action { public ActionForward execute( …) { LogonForm logonForm = (LogonForm) actionForm; String forward = "failure"; if(logonForm.getUserName().equals("mentor")) { forward = "success"; } return mapping.findForward(forward); } }

Demo Results

Summary
Requirements for Tiles How Tiles Works Tiles Best Practices Full Tiles Example

Questions?

Thank You

Sign up to vote on this title
UsefulNot useful