Over a million developers have joined DZone.

Dynamic Method Invocation Tutorial


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" "http://www.w3.org/TR/html4/loose.dtd">
<%@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

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}