Struts 1 > Using WildCards in ActionMapping Tutorial

Using WildCards in ActionMapping Tutorial

In this tutorial you will learn see how to reduce the number of action mappings by using wildcard characters. Here is an example of struts configuration file using wildcard character.

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"


    <form-bean name="SampleReportForm" type="com.w3javaguide.reports.SampleReportForm"/>

    <action path="/*Action" type="com.w3javaguide.reports.{1}Action" name="{1}Form">
        <forward name="success" path="/{1}.jsp" />


Here the value of the path attribute contains a wildcard character. The value that matches the asterick(*) in the request URL will be substituted instead of '{1}' in the other attributes.

Let's see with an example. In the index.jsp page the request URL is "/SampleReportAction". So according to the configuration the "SampleReport" matches the asterick, so it will be substituted instead of {1}. For this requset URL the Action class will be SampleReportAction and the ActionForm will be SampleReportForm and when the ActionForward value is success the user will be forwarded to SampleReport.jsp page.

The index.jsp page contains a Generate Report button, which when clicked forwards the request to the SampleReportAction URL, then invokes the SampleReportAction class and displays the SampleReport.jsp page back to the user.

<%@taglib uri="" prefix="html"%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<html:form action="/SampleReportAction">
<html:submit value="Generate Report" />

Here is the sample report.

You can download the source code of this example by clicking on the Download link below.

Source :Download
War :Download
Contact Us  |  Copyright © 2012