Struts 2 Multi action tutorial


            As an application grows in size, so will the number of action mappings. Wildcards can be used to combine similar mappings into one more generic mapping.

            Use * to group the action and pass them to same controller

Step 1

Enable Dynamic method invocation to customize default execute method in controller

<constant name="struts.enable.DynamicMethodInvocation" value="true" />

Step 2

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

Step 3

Now you can write more than once method in Controller class

package com.candidjava.controller;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private String message;
	
	public String execute()
	{
		setMessage("From execute method");
		return  SUCCESS;		
	}
	
	public String addUser()
	{
		setMessage("From add method");
		return SUCCESS;		
	}
	
	public String updateUser()
	{
		setMessage("From update method");
		return SUCCESS;		
	}
	
	public String deleteUser()
	{
		setMessage("From delete method");
		return SUCCESS;		
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

Step 4

Now add an extra attribute "method"  in html to map your appropriate function.
	<s:form action="User">
		<s:submit value="def" />
		<s:submit method="addUser" value="Add"  />
		<s:submit method="updateUser" value="Update" />
		<s:submit  method="deleteUser" value="Delete" />
	</s:form>

Screenshot:
            


Download
        Struts 2 wildcard example war            
        Struts 2 wildcard example maven zip



Related Post

Comments


©candidjava.com