DispatchAction functionality in Struts 2?

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

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

In each view, it is redirected to the action class, as indicated in my struts.xml file, like:

 <!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 action class method:

 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 the submit button, it goes to the execute method, which is fine. But when I click the Add button or any other button again, it redirects to the execute method instead of the add method. What am I doing wrong? Waiting for your answers. thanks in advance

+6
source share
2 answers

Assuming you are using 2.3.15.3 version 2.3.15.3 or higher, you need to set the struts.mapper.action.prefix.enabled constant to true to enable support for the action: prefix.

Put this in your struts.xml file:

 <constant name="struts.mapper.action.prefix.enabled" value="true" /> 
+7
source

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

struts.mapper.action.prefix.enabled = true

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

+1
source

Source: https://habr.com/ru/post/957567/


All Articles