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
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.
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.
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; }
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.
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" />
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With