Action:
package com.awp.test.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.awp.test.form.FirstForm;
import com.awp.test.form.SecondForm;
/**
* @version 1.0
* @author awp
* @date 2009-4-24 上午11:25:49
* @decription 一个Action中包含多个Form
*/
public class MultiFormTestAction extends DispatchAction {
/**
* 第一个Form对应方法
*/
public ActionForward firstForm(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
FirstForm beanform = (FirstForm)form;
request.setAttribute("name", beanform.getName());
return mapping.findForward("display");
}
/**
* 第二个Form对应方法
*/
public ActionForward secondForm(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
SecondForm beanform = (SecondForm)form;
request.setAttribute("name", beanform.getName());
return mapping.findForward("display");
}
}
Form:
package com.awp.test.form;
import org.apache.struts.action.ActionForm;
public class FirstForm extends ActionForm {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.awp.test.form;
import org.apache.struts.action.ActionForm;
public class SecondForm extends ActionForm {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
struts-cofing.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <form-beans> <form-bean name="firstform" type="com.awp.test.form.FirstForm"/> <form-bean name="secondform" type="com.awp.test.form.SecondForm"/> </form-beans> <global-forwards> <forward name="display" path="/display.jsp"/> </global-forwards> <action-mappings> <action path="/firstpath" input="/first.jsp" name="firstform" parameter="method" scope="request" type="com.awp.test.action.MultiFormTestAction"/> <action path="/secondpath" input="/second.jsp" name="secondform" parameter="method" scope="request" type="com.awp.test.action.MultiFormTestAction"/> </action-mappings> <message-resources parameter="MessageResources" /> </struts-config>
first.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>我是提交第一个FORM的页面</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/firstpath.do?method=firstForm" method="post">
<input name="name" value="" type="text"/>
<input name="ok" value="提交" type="submit"/>
</form>
</body>
</html>
second.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>我是提交第二个FORM的页面</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/secondpath.do?method=secondForm" method="post">
<input name="name" value="" type="text"/>
<input name="ok" value="提交" type="submit"/>
</form>
</body>
</html>
display.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>显示页面</title>
</head>
<body>
<br />
表单提交的内容为:${name} <br /><br />
<a href="first.jsp">打开表单1</a><br /><br />
<a href="second.jsp">打开表单2</a>
</body>
</html>
Action必须继承DispatchAction,感觉其实一个action还是只能对应一个form
这里的方法其实是利用action的path 多个path可以指向同一个action。
本文介绍如何在Struts框架中实现单Action处理多个表单的功能。通过配置struts-config.xml,定义不同的路径指向同一Action,并通过DispatchAction区分不同的表单处理方法。

被折叠的 条评论
为什么被折叠?



