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

Dynamic Method Invocation Tutorial

06.15.2012
| 19108 views |
  • 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.

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

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

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"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <s:form action="User" >
        <s:submit />
        <s:submit action="addUser" value="Add" />
        <s:submit action="updateUser" value="Update" />
        <s:submit action="deleteUser" value="Delete" />
    </s:form>
</body>
</html>

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.)

Tags: