Upload
meljun-cortes-mbampa
View
222
Download
0
Embed Size (px)
Citation preview
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
1/38
Advanced Struts Topics
Web Programming
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
2/38
DynaActionForms
2Web Programming
Functions exactly like ActionForms, in that an instance of itcan be obtained and its methods called upon by the Actionhandlers that need its data.
The main difference is, each DynaActionForm is not definedor declared as a separate class but simply configured fromwithin the struts-config.xml file.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
3/38
3Web Programming
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
4/38
DynaActionForms
4Web Programming
Creating a DynaActionForm is simpler and faster comparedto writing an actual ActionForm instance.
No need to list down all of the form properties and create get and setmethods for each of them.
We simply declare the property name and type and it is theframework's responsibility to provide a working instance based onthis information.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
5/38
DynaActionForms
5Web Programming
The following are the Java types supported byDynaActionForm:
java.lang.BigDecimal java.lang.BigInteger boolean and java.lang.Boolean char and java.lang.Character double and java.lang.Double float and java.lang.Float int and java.lang.Integer long and java.lang.Long
short and java.lang.Short java.lang.String java.lang.Date java.lang.Time java.sql.TimeStamp
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
6/38
DynaActionForms
6Web Programming
More convenient, but are NOT always the best solution.
There are still cases where using ActionForms are moreappropriate:
DynaActionForms only support a limited set of Java types. DynaActionForms do not support the concept of inheritance.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
7/38
Validators
7Web Programming
Validation
Checking the correctness of the format and content of user-givenvalues.
An activity that should be performed for all cases of data input.
Framework provided by Struts to alleviate the developer'sburden of having to perform validation.
Advantages:
Provide several pre-defined validation rules.
format checking, length checking, checking for existence, etc.
Eliminate redundancy in validation code.
Provide a single point of maintenance.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
8/38
Validators
8Web Programming
Steps required in including Validator functionality within anexisting Struts application:
Configure the Validator Plug-in.
Declare the forms requiring validation and the type of validation theyrequire.
Create the messages that will be displayed in case of validationfailure.
Modify the struts-config file to enable automatic validation.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
9/38
Configuring the Validator
Plug-In
9Web Programming
Required to make Struts aware of our usage of the Validatorframework.
All that needs to be done is to add a few lines into our struts-config.xml file:
...
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
10/38
Configuring the Validator
Plug-In
10Web Programming
...
The pathnames property informs the framework where it can find thetwo configuration files needed:
validator-rules.xml
validator.xml
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
11/38
validator-rules.xml
11Web Programming
Defines the classes implementing the validation code.
The framework comes with a copy of this file with the pre-defined validator classes already configured.
Logical names of the validators that come shipped in with theframework:
required: Properties marked as required must have at least one character asidefrom any whitespace.
mask: Properties subjected to the mask validator must match the regularexpression that we submit using the mask parameter.
minlength: If applied to a property, the property must have a length equal to orgreater than the value of the min parameter that we pass.
maxlength: If applied to a property, the property must have a length equal to orless than the value of the max parameter that we pass.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
12/38
validator-rules.xml
12Web Programming
(Logical names continued) range: The property must fall in between the min and max values that we supply
using the min and max parameter.
byte, short, integer, long, float, double: The property must be convertible to thespecified primitive type.
date: Succeeds if the value of the property is a valid date.
creditCard: Succeeds if the value of the property can be a valid credit cardnumber.
email: Succeeds if the value of the property can be a valid email address.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
13/38
validation.xml
13Web Programming
Declares to the framework which forms requires validationand which validation rules it would like to implement.
The framework only provides for us the structure of the file;developers would have to configure this file themselves toavail of the framework's functionality.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
14/38
validation.xml
14Web Programming
Configuring the validation.xml file:
The root element of this configuration file.
Each form in our application that requires validation should have a corresponding element. The name attribute in this case should map to the name of aform-bean configured in struts-config.xml.
Can contain one or more elements.
Each field element represents a property in the form that requires validation. Has the following attributes:
property: The name of the property within the form that will be validated.
depends: The comma-separated list of the validators that will be applied to the property. The names ofthe validators are defined within the validator-rules.xml file.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
15/38
validation.xml
15Web Programming
(Configuring continued)
Defines the key to the error message that will be displayed in case the propertydoes not pass validation rules. The key and the actual error message that will be
displayed can be found in a resource bundle that will have to be created by thedeveloper.
Some validators require certain values to be passed to them as parameters sothat they can function properly. These parameters can be supplied by using oneor more var elements, with each var element corresponding to a parameter givento the validator. This element defines two child elements:
: Defines the name of the parameter to be supplied.
: Defines the value of the parameter.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
16/38
validation.xml
16Web Programming
Example:
min
4
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
17/38
Defining the Resource Bundle
17Web Programming
The element defined a key that needs to be matchedto an entry to a resource bundle. This entry is then used todetermine the message that will be displayed to the user.
The Validator framework makes use of the same resourcebundle that the Struts framework uses, which, by default,can be found in the WEB-INF/classes directory under thename ApplicationResources.properties. If such a resourcebundle does not currently exist in your application, create atext file with that name.
error.loginname.required=Please enter your login nameerror.password.required=You have supplied a blank password or a
password with less than 4 characters
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
18/38
18Web Programming
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
19/38
Tiles
19Web Programming
Another framework that works especially well with Struts.
Allows us to easily define templates and screen instanceswhich we can use with our application.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
20/38
What are templates?
20Web Programming
A template page is a page layout design that can be reusedby any of the pages in your application.
Using templates gives your application a more consistentlook and feel.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
21/38
21Web Programming
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
22/38
22Web Programming
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
23/38
Templates
23Web Programming
Several design elements in common for both pages:
Same set of navigational links on the left
Same set of images on the top of the page
Same set of images on the links on the bottom of the page Most of what has changed between the different pages is
the content presented in the middle of the page.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
24/38
Templates
24Web Programming
How we would be able to implement a set of similar pages?
Refactor into separate entities code that would be duplicated acrossthe application: the navigational links could be implemented as onepage, the header images as another, the footer yet another. We can
do this without the use of any framework, using the action we have discussed in the basic JSP lecture.
Better Solution: To make use of a "template" page that would definethe default formatting and layout of the pages and define a contentpage that the template would insert into the content placeholder
Advantage: Any changes that would need to be performed on one aspect of the
presentation layer would be applied to all of the pages while modifying only onepage.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
25/38
Preparing Tiles
25Web Programming
Steps
Add the following lines to struts-config.xml, just before the closing element:
This informs Struts that we want to make use of the Tiles framework, and that theconfiguration file that it would read can be found in the /WEB-INF directory, withthe name tiles-defs.xml.
Copy the struts-tiles.tld and tiles-config.dtd files from the Struts' libdirectory into the /WEB-INF directory of our application
struts-tiles.tld: Contains information on the custom tags that we will need to usefor Tiles.
tiles-config.dtd: Defines the structure of the tiles-defs configuration file.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
26/38
Preparing Tiles
26Web Programming
(Steps continued)
Create a blank configuration file that we can fill up later. Make anxml file, name it tiles-defs.xml and place it in the WEB-INF directory.Then place the following content:
After we have performed these steps, Tiles is ready for use.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
27/38
Building a Layout Template
27Web Programming
The first step in building a template is to identify thecomponents that that will be placed in it
Example
To create a template page implementingthis layout, follow these steps:
Create a new JSP page.
Import the Tiles tag library.
Create the HTML implementing the layout,leaving as blank the implementation for theactual components.
Use the tag to act as placeholdersfor the layout components.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
28/38
28Web Programming
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
29/38
Building a Layout Template
29Web Programming
(Example continued)
Has a number of uses in the Tiles framework.
In this case, it inserts a JSP fragment referred to by the name supplied in the
attribute. Which fragment corresponds to the name will be specified in the screendefinition that will make use of this template.
Retrieves as a String a value supplied in a screen definition using the name inthe name attribute.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
30/38
Creating Screen Definitions
30Web Programming
Once we have a template, we can make use of it to whollydefine a screen.
Creating screen definitions can be done in two ways withinthe Tiles framework:
Definition using JSP pages
Definition using an XML configuration file
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
31/38
Creating Screen Definitions
31Web Programming
Creating a definition using JSP pages:
Makes use of additional custom tags supplied by Tiles:
: Base tag with which to define a screen. Has the followingattributes:
id: Name by which this definition can be referred to by other components.page: Location of the template file it will use as its base.
scope: (Optional) The possible values are: page, request, session, and application. By default, adefinition is visible only within the JSP page containing the declaration.
- Used to place a value into a specified attribute inside a template.Has the following attributes:
name: Specifies the name of the target location inside the template.
value: Defines the location of the JSP fragment to be used.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
32/38
Creating Screen Definitions
32Web Programming
(Creating a definition using JSP pages continued)
Example:
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
33/38
Creating Screen Definitions
33Web Programming
Creating a definition using an XML configuration file
Done by placing a configuration entry in the tiles-defs.xmlconfiguration file that we initially created.
The syntax is very similar to the used earlier:
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
34/38
Creating Screen Definitions
34Web Programming
What is the difference between the two methods?
Both methods load the definition as a JavaBean into memory.However:
The JSP method loads the definition only after the JSP fragment containing it has
been accessed and, by default, makes it visible only in the same page. The XML method, on the other hand, makes the screen definition available to the
whole application immediately after startup.
Another benefit of using the XML method for creating the screendefinition is that the definitions themselves can be used asActionForwards by the Struts framework.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
35/38
Using the Screen Definitions
35Web Programming
To put a Definition into use, we can use the tagand supply it the name of the definition to display:
Example:
Problem:
It increases the number of JSPs required to display different screens to the user.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
36/38
Using the Screen Definitions
36Web Programming
Better approach: Make use of the definitions as the targetsof ActionForwards.
By including the Tiles framework as a plugin for Struts, Struts ismade aware of the screen definitions created using Tiles.
The screen names can be used in place of actual locations in tags.
Example
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
37/38
Extending Screen Definitions
37Web Programming
An advantage of using Tiles framework over othertemplating methods.Works in much the same way as classinheritance in Java: the extended screen inherits all theproperties and attributes of the parent Definition,
This allows us to create a base definition screen that declaresdefault values for attributes that can be extended by Definitionsspecialized for specific pages.
Advantage:
We avoid repetition of attribute value definition.
When a change needs to be made with regards to which component is placed inone of the base attributes, this change is immediately propagated to all of thescreens automatically by simply making the change on the base screendefinition.
8/3/2019 MELJUN_CORTES_JEDI Slides Web Programming Chapter08 Advanced Struts
38/38
Extending Screen Definitions
38Web Programming
Example