Using this method also requires the request parameter to specify the action to be performed. The request parameter name is in the format
Action!method.action
Note: Since Struts2 only requires a parameter name, the parameter value is anything.
Here's an example program that shows how to handle a form with multiple submit:
Main Page more_submit.jsp
Copy Code code as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%>
<%@ taglib prefix= "s" uri= "/struts-tags"%>
<title>my JSP ' hello.jsp ' starting page</title>
<body>
<s:form action= "Submit.action" >
<s:textfield name= "msg" label= "Input"/>
<s:submit name= "Save" value= "saved" align= "left" method= "save"/>
<s:submit name= "print" value= "Printing" align= "left" method= "print"/>
</s:form>
</body>
Moresubmitaction.java
Copy Code code as follows:
Package action;
Import javax.servlet.http.*;
Import Com.opensymphony.xwork2.ActionSupport;
Import org.apache.struts2.interceptor.*;
public class Moresubmitaction extends Actionsupport implements
Servletrequestaware {
Private String msg;
private Javax.servlet.http.HttpServletRequest request;
Get HttpServletRequest Object
public void Setservletrequest (HttpServletRequest request) {
This.request = Request;
}
Action to handle the Save Submit button
Public String Save () throws Exception {
Request.setattribute ("Result", "successfully saved [" + msg + "]");
return "Save";
}
Action to handle Print submit button
Public String print () throws Exception {
Request.setattribute ("Result", "successful print [+ msg +]");
return "print";
}
Public String getmsg () {
return msg;
}
public void Setmsg (String msg) {
this.msg = msg;
}
}
Structs.xml
Copy Code code as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd ">
<struts>
<package name= "Demo" extends= "Struts-default" >
<action name= "Submit" class= "action. Moresubmitaction ">
<result name= "Save" >
/result.jsp
</result>
<result name= "Print" >
/result.jsp
</result>
</action>
</package>
</struts>
result.jsp
Copy Code code as follows:
<%@ page language= "java" contenttype= "text/html; CHARSET=GBK "
pageencoding= "GBK"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=iso-8859-1 ">
<title> Submit Results </title>
<body>
</body>