Tuesday, August 6, 2013

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.
01.<?xml version="1.0" encoding="ISO-8859-1" ?>
02. 
03.<!DOCTYPE struts-config PUBLIC
04."-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
06. 
07.<struts-config>
08. 
09.<form-beans>
10.<form-bean name="SampleReportForm"type="com.vaannila.reports.SampleReportForm"/>
11.</form-beans>
12. 
13.<action-mappings>
14.<action path="/*Action" type="com.vaannila.reports.{1}Action" name="{1}Form">
15.<forward name="success" path="/{1}.jsp" />
16.</action>
17.</action-mappings>
18. 
19.</struts-config>
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 ActionFormwill be SampleReportForm and when the ActionForward value is success the user will be forwarded toSampleReport.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 theSampleReport.jsp page back to the user.
01.<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
02.<html>
03.<head>
04.<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
05.<title>Insert title here</title>
06.</head>
07.<body>
08.<html:form action="/SampleReportAction">
09.<html:submit value="Generate Report" />
10.</html:form>
11.</body>
12.</html>
Here is the sample report.

No comments:

Post a Comment