Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DispatchAction Functionality in Struts 2?

Tags:

jsp

struts2

I have a .jsp page in which i have four buttons named submit,add,update and delete as:

<%@ 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>

On each submit it is being redirected to action class as mentioned in my struts.xml file as:

<!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" class="vaannila.UserAction">
            <result name="success">/success.jsp</result>
        </action>
        <action name="addUser" method="add" class="vaannila.UserAction">
            <result name="success">/success.jsp</result>
        </action>
        <action name="updateUser" method="update" class="vaannila.UserAction">
            <result name="success">/success.jsp</result>
        </action>
        <action name="deleteUser" method="delete" class="vaannila.UserAction">
            <result name="success">/success.jsp</result>
        </action>
    </package>
</struts>

and finally to the corresponding method of the action class as :

package vaannila;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{

    private String message;

    public String execute()
    {
          System.out.println("Inside execute method");
        message = "Inside execute method";
        return SUCCESS;
    }

    public String add()
    {
          System.out.println("Inside add method");
        message = "Inside add method";
        return SUCCESS;
    }

    public String update()
    {
        message = "Inside update method";
        return SUCCESS;
    }

    public String delete()
    {
        message = "Inside delete method";
        return SUCCESS;
    }

    public String getMessage() {
        return message;
    }

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

}

When i click on Submit button it goes to execute method which is fine. But when i click on Add button or any other button again it get redirected to execute method instead of add method. What am i doing wrong? Looking forward to your answers. Thanks in advance

like image 395
Roy Avatar asked Nov 07 '13 09:11

Roy


People also ask

What is the use of interceptors in Struts 2?

Interceptor is an object that is invoked at the preprocessing and postprocessing of a request. In Struts 2, interceptor is used to perform operations such as validation, exception handling, internationalization, displaying intermediate result etc.

What is the difference between struts1 and Struts 2?

Struts 1 supports separate Request Processors (lifecycles) for each module, but all the Actions in the module must share the same lifecycle. Struts 2 supports creating different lifecycles on a per Action basis via Interceptor Stacks. Custom stacks can be created and used with different Actions, as needed.

What is Execute method in Struts 2?

The method execute is where we placed what we want this controller to do in response to the hello. action . Method execute of HelloWorldAction. public String execute() throws Exception { messageStore = new MessageStore() ; helloCount++; return SUCCESS; }

What is dispatch action in Struts?

DispatchAction is one of the Struts built-in action that provides a mechanism that facilitates having a set of related functionality in a single action instead of creating separate independent actions for each function.


2 Answers

Assuming you are using Struts2 version 2.3.15.3 or above you need to set struts.mapper.action.prefix.enabled constant to true in order to enable support for action: prefix.

Put that in your struts.xml file:

<constant name="struts.mapper.action.prefix.enabled" value="true" />
like image 116
Aleksandr M Avatar answered Sep 20 '22 11:09

Aleksandr M


you can also specify this in the properties file : struts.properties

struts.mapper.action.prefix.enabled=true

For more info : http://struts.apache.org/development/2.x/docs/constant-configuration.html

like image 43
tharindu_DG Avatar answered Sep 20 '22 11:09

tharindu_DG