Beruflich Dokumente
Kultur Dokumente
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
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
JSP3
JSP4
Presentation Logic Ctrl Presentation Presentation Logic Ctrl Present. Present. Logic
The Good Easiest Solution The Bad Presentation and Logic are mixed. The Ugly No reuse possibilities
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
JSP3
The Bad
There is no longer a one to one mapping from a view to a single source of code.
JSP2
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.
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
The View
The Model
Controller
4: Retrieve Data ActionBean Data Resource
ActionServlet
Front Controller
7: redirect to appropriate view 5: Establish bean state, then place in session or request object
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 Logical name definitions for Forms View mappings
Local Global
<!-- ========== 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>
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"
If the logical name returned by perform() is failure go to page /failure.jsp If the Logical name returned by perform() 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 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
Struts Installation
Downlad the zip http://apache.mirrors.hoobly.com/jakarta/strut s/binaries/jakarta-struts-1.1.zip Unzip and go! There isnt an install, however there are several files youll 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 Its 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>
JSP Pages
Three Pages Login Page Success Page Failure Page
<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"/>
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); } }
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
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>
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
ActionForm Class
Basic Type Follows Rules for JavaBean Collection of properties Methods Reset() Validate() Eg -ActionForm
public class LogonForm extends ActionForm {}
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.DynaActionF orm"> <form-property name="userName" type="java.lang.String" /> <form-property name="password" type="java.lang.String" /> </form-bean> </form-beans>
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
Lets 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" /> </action> Defines the parameter
used to get the action The name action is arbitrary, could be anything
LookupDispatchAction
It is a DispatchAction and carries all its 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, its configuration and method names
protected Map getKeyMethodMap() { Map map = new HashMap(); map.put(login.button", login"); map.put(send.password.button", sendPassword"); return map; } 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
Local 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
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" />
Forward Redirects
Eg-Redirect By default forwards are forwards and not redirects. To redirect the client <forward name="success" path="/Success.jsp" redirect="true />
Struts-Config.xml
DataSources FormBeans Global Exceptions Actions Controller Message Resources PlugIns
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 isnt a configured local exception handler.
Global Exceptions
<global-exceptions> <exception
type="app.ExpiredPasswordExcepti
on"
path="/changePassword.jsp"/> </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?
Section Agenda
Common Tags Working with Forms Useful Struts Java Classes
Common 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 Dont 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>
Parameterized Links
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 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
Bean:message
Used to retrieve a property from a configured property file. Well 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.
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 well 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 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>
Struts Forms
<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
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
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
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 isnt an option that matches the select choice then its up to the browser to decide
On Submit
Gets the option selected Sets the value of the like named property in the form bean
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>
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
<input type=radio name=status value=Full Time >Full Time</input> <input type=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 dont need a default value you dont 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" value=Dental">Dental
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>
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
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
Server-Side Validation
Server-Side Validation
Common Validation Classes and &Configuration Form Bean Validation Action Bean Validation
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:
A Look at MessageFormat
Its 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.
Server-Side Validation
Common Validation Classes and Configuration Form Bean Validation Action Bean Validation
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
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.
Server-Side Validation
Common Validation Classes and Configuration Form Bean Validation Action Bean Validation
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,
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.
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
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.
Range Configuration
<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> ApplicationResources.properties file: <var-value>4</var-value> range {1} through {2}. errors.range={0} is not in the </var>
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})?$
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
TilesPlugin Parameters
definitions-parser-details
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.
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
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
Tile Layouts
Body
Footer
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>
Abstract Definitions
A definition which by itself is very used. It is not concrete.
Meaning it doesnt 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
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>
<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
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>
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;
LogonAction
Demo Results
Summary
Requirements for Tiles How Tiles Works Tiles Best Practices Full Tiles Example
Questions?
Thank You