Recipe7.8.Using Wildcards in Action Paths


Recipe 7.8. Using Wildcards in Action Paths

Problem

You want to reduce the number of action mappings by combining similar action mappings into a single generic mapping.

Solution

<action             path="/Edit*"         type="com.oreilly.strutsckbk.ch07.Edit{1}Action"         name="{1}Form"         scope="request"         validate="false">     <forward          name="success"          path="/edit_{1}.jsp"/> </action> <action             path="/Save*"         type="com.oreilly.strutsckbk.ch07.Save{1}Action"         name="{1}Form"         scope="request"         validate="true"         input="edit_{1}.jsp">     <forward          name="success"          path="/saved_{1}.jsp"/> </action>

Discussion

Many developers find that their action mappings start to follow similar patterns. In fact, many applications use standard conventions for naming action paths, Action classes, and ActionForms, making their application easier to organize and maintain. Struts 1.2 allows you to leverage these conventions in your struts-config.xml file. Create your action elements using an asterisk (*) as a wildcard in the path attribute. When Struts finds the action mapping for a given request path, it attempts to find an exact match. If an exact match is not found, it attempts a match using the wildcards.

In the Solution, for example, when a request comes in for /EditEmployee.do, the action mapping with the path of /Edit* will match. The {1} notation represents the part of the request URL value that matches the wildcard, minus any extension. In this case, {1} has the value of Employee.

Wildcard mappings reduce the number of action elements you have to write and can enforce a workflow of your own design. Figure 7-1 illustrates the workflow for the Solution when applied to editing employee data.

Figure 7-1. Common workflow applied to editing employee data


Suppose you need to edit and save different information, such as vendor data. Without wildcard mappings, you would create action mappings such as the following:

<action             path="/EditVendor"         type="com.oreilly.strutsckbk.ch07.EditVendorAction"         name="VendorForm"         scope="request"         validate="false">     <forward          name="success"          path="/edit_Vendor.jsp"/> </action> <action             path="/SaveVendor"         type="com.oreilly.strutsckbk.ch07.SaveVendorAction"         name="VendorForm"         scope="request"         validate="true"         input="edit_Vendor.jsp">     <forward          name="success"          path="/saved_Vendor.jsp"/> </action>

But with the wildcard mappings shown in the Solution, you can use one common set of action elements for employee and vendor data. Of course, you need to create the JSPs, Actions, and ActionForms specific to vendor data.

The action element attributes that can use wildcard-matched strings via the {n} notation are the following:

  • type

  • name

  • roles

  • parameter

  • attribute

  • forward

  • include

  • input

You can use the placeholders in the path attribute of nested local forward elements.

If the mappings for a particular application feature no longer fit the pattern, create standard action mappings without wildcards that exactly match the request path. An exact-match mapping takes precedence over a wildcard mapping.

The algorithm and code used for wildcard matching was derived from similar features in the Apache Cocoon project.


You can use other wildcard characters in addition to the "*" in your path attribute. Table 7-3 shows the complete set of supported characters.

Table 7-3. Supported wildcard characters

*

Matches zero or more characters, excluding the slash (/) character.

**

Matches zero or more characters, including the slash (/) character.

\character

The backslash character is used as an escape sequence. Thus, \* matches the character asterisk (*), and \\ matches the character backslash (\).


As was shown in the Solution, the wildcard-matched values can be accessed with the {n} notation. n is a number from 1 to 9 that indicates the position of the wildcard-matched value to substitute. The entire request URI can be accessed with the {0} token.

See Also

The Struts User's guide discusses wildcard mapping. The specific section can be found at http://struts.apache.org/userGuide/building_controller.html#action_mapping_wildcards. If you're using Struts 1.1 and want to use wildcard mapping, earlier versions are available. Consult http://www.twdata.org/struts-wildcard/ for details, documentation, and downloads.

As mentioned, the DispatchAction can be used to provide a single action that serves multiple purposes. The DispatchAction and its related subclasses are discussed in Chapter 6.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net