在基本Struts框架下建立一张JSP页面,一个Action类,并且在Struts.xml文件中配置这个Action的路径。
测试页面:Test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<font color="red">${result}</font>
<s:form action="test.do">
<s:submit name="method1" method="method1" value="方法1"></s:submit>
<s:submit name="method2" method="method2" value="方法2"></s:submit>
</s:form>
</body>
</html>
Action类:TestAction.java
package Struts.action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class TestAction extends ActionSupport implements ServletRequestAware{
private HttpServletRequest request;
public void setServletRequest(HttpServletRequest request){
this.request = request;
}
public String method1() throws Exception{
request.setAttribute("result","method1");
return "test";
}
public String method2() throws Exception{
request.setAttribute("result", "method2");
return "test";
}
}
Struts.xml文件中的Action配置为:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd "> <struts> <package name="action" extends="struts-default"> <action name="test" class="Struts.action.TestAction"> <result name="test">Test.jsp</result> </action> </package> </struts>
通过不同的按钮对应不同的method,访问同一个Action中不同的方法