To use this method, you also need to specify the action to be executed through the request parameter. The format of the request parameter name is
Action! Method. action
Note: Because Struts2 only requires the parameter name, the parameter value can be anything.
The following is an instance program to demonstrate how to handle multiple submit forms:
Home Page more_submit.jsp
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "GBK" %>
<% @ Taglib prefix = "s" uri = "/struts-tags" %>
<Html>
<Head>
<Title> My JSP 'hello. jsp 'starting page </title>
</Head>
<Body>
<S: form action = "submit. action">
<S: textfield name = "msg" label = "input content"/>
<S: submit name = "save" value = "save" align = "left" method = "save"/>
<S: submit name = "print" value = "print" align = "left" method = "print"/>
</S: form>
</Body>
</Html>
MoreSubmitAction. java
Copy codeThe Code is 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;
// Obtain the HttpServletRequest object
Public void setServletRequest (HttpServletRequest request ){
This. request = request;
}
// Process the save submit button action
Public String save () throws Exception {
Request. setAttribute ("result", "successfully saved [" + msg + "]");
Return "save ";
}
// Process the print submit button action
Public String print () throws Exception {
Request. setAttribute ("result", "successfully printed [" + msg + "]");
Return "print ";
}
Public String getMsg (){
Return msg;
}
Public void setMsg (String msg ){
This. msg = msg;
}
}
Structs. xml
Copy codeThe Code is 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 codeThe Code is 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">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = ISO-8859-1">
<Title> submission result </title>
</Head>
<Body>
<H1 >$ {result} </Body>
</Html>