Meyyappan has posted 101 posts at DZone. View Full User Profile

Dynamic Method Invocation Tutorial

  • submit to reddit

This is a continuation of the previous example ( DispatchAction functionality in Struts 2 ). In this example you will see how you can avoid configuring a seperate action mapping for each method in the Action class by using the wildcard method. Look at the following action mapping.

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    <package name="default" extends="struts-default">
        <action name="*User" method="{1}" class="vaannila.UserAction">
            <result name="success">/success.jsp</result>

Note the similarity between the action mapping and the following request URLs in this page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<%@taglib uri="/struts-tags" prefix="s" %>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
    <s:form action="User" >
        <s:submit />
        <s:submit action="addUser" value="Add" />
        <s:submit action="updateUser" value="Update" />
        <s:submit action="deleteUser" value="Delete" />

As you can see we have replaced all the method names with an asterisk symbol. The word that matches for the first asterisk will be substituted for the method attribute. So when the request URL is "addUser" the add() method in the UserAction class will be invoked.

You can download the Dynamic Method Invocation example by clicking the Download link below.

Source :Download
War :Download
Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)