使用这种方式也需要通过请求参来来指定要执行的动作。请求参数名的格式为
action!method.action
注:由于Struts2只需要参数名,因此,参数值是什么都可以。
下面我就给出一个实例程序来演示如何处理有多个submit的form:
主页面more_submit.jsp
复制代码 代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
MoreSubmitAction.java
复制代码 代码如下:
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;
// 获得HttpServletRequest对象
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
// 处理save submit按钮的动作
public String save() throws Exception {
request.setAttribute("result", "成功保存[" + msg + "]");
return "save";
}
// 处理print submit按钮的动作
public String print() throws Exception {
request.setAttribute("result", "成功打印[" + msg + "]");
return "print";
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
structs.xml
复制代码 代码如下:
/result.jsp
/result.jsp
result.jsp
复制代码 代码如下:
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>