一个Action对应多个Form DispatchAction

一个Action对应多个Form

关键字: action form

  Action:

 

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://awp.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=package%20com.awp.test.action%3B%0A%0Aimport%20javax.servlet.http.HttpServletRequest%3B%0Aimport%20javax.servlet.http.HttpServletResponse%3B%0A%0Aimport%20org.apache.struts.action.ActionForm%3B%0Aimport%20org.apache.struts.action.ActionForward%3B%0Aimport%20org.apache.struts.action.ActionMapping%3B%0Aimport%20org.apache.struts.actions.DispatchAction%3B%0A%0Aimport%20com.awp.test.form.FirstForm%3B%0Aimport%20com.awp.test.form.SecondForm%3B%0A%0A%2F**%0A%20*%20%40version%201.0%0A%20*%20%40author%20awp%0A%20*%20%40date%202009-4-24%20%E4%B8%8A%E5%8D%8811%3A25%3A49%0A%20*%20%40decription%20%E4%B8%80%E4%B8%AAAction%E4%B8%AD%E5%8C%85%E5%90%AB%E5%A4%9A%E4%B8%AAForm%20%0A%20*%2F%0Apublic%20class%20MultiFormTestAction%20extends%20DispatchAction%20%7B%0A%0A%09%2F**%0A%09%20*%20%E7%AC%AC%E4%B8%80%E4%B8%AAForm%E5%AF%B9%E5%BA%94%E6%96%B9%E6%B3%95%0A%09%20*%2F%20%0A%09public%20ActionForward%20firstForm(ActionMapping%20mapping%2C%20ActionForm%20form%2C%0A%09%09%09HttpServletRequest%20request%2C%20HttpServletResponse%20response)%7B%0A%09%09%09FirstForm%09beanform%09%3D%09(FirstForm)form%3B%0A%09%09%09request.setAttribute(%22name%22%2C%20beanform.getName())%3B%0A%09%09%09return%20mapping.findForward(%22display%22)%3B%0A%09%7D%0A%09%2F**%0A%09%20*%20%20%E7%AC%AC%E4%BA%8C%E4%B8%AAForm%E5%AF%B9%E5%BA%94%E6%96%B9%E6%B3%95%0A%09%20*%2F%20%0A%09public%20ActionForward%20secondForm(ActionMapping%20mapping%2C%20ActionForm%20form%2C%0A%09%09%09HttpServletRequest%20request%2C%20HttpServletResponse%20response)%7B%0A%09%09%09SecondForm%09beanform%09%3D%09(SecondForm)form%3B%0A%09%09%09request.setAttribute(%22name%22%2C%20beanform.getName())%3B%0A%09%09%09return%20mapping.findForward(%22display%22)%3B%0A%09%7D%0A%7D%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. package  com.awp.test.action;  
  2.   
  3. import  javax.servlet.http.HttpServletRequest;  
  4. import  javax.servlet.http.HttpServletResponse;  
  5.   
  6. import  org.apache.struts.action.ActionForm;  
  7. import  org.apache.struts.action.ActionForward;  
  8. import  org.apache.struts.action.ActionMapping;  
  9. import  org.apache.struts.actions.DispatchAction;  
  10.   
  11. import  com.awp.test.form.FirstForm;  
  12. import  com.awp.test.form.SecondForm;  
  13.   
  14. /**  
  15.  * @version 1.0  
  16.  * @author awp  
  17.  * @date 2009-4-24 上午11:25:49  
  18.  * @decription 一个Action中包含多个Form   
  19.  */   
  20. public   class  MultiFormTestAction  extends  DispatchAction {  
  21.   
  22.     /**  
  23.      * 第一个Form对应方法  
  24.      */    
  25.     public  ActionForward firstForm(ActionMapping mapping, ActionForm form,  
  26.             HttpServletRequest request, HttpServletResponse response){  
  27.             FirstForm   beanform    =   (FirstForm)form;  
  28.             request.setAttribute("name" , beanform.getName());  
  29.             return  mapping.findForward( "display" );  
  30.     }  
  31.     /**  
  32.      *  第二个Form对应方法  
  33.      */    
  34.     public  ActionForward secondForm(ActionMapping mapping, ActionForm form,  
  35.             HttpServletRequest request, HttpServletResponse response){  
  36.             SecondForm  beanform    =   (SecondForm)form;  
  37.             request.setAttribute("name" , beanform.getName());  
  38.             return  mapping.findForward( "display" );  
  39.     }  
  40. }  
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:

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://awp.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=package%20com.awp.test.form%3B%0A%0Aimport%20org.apache.struts.action.ActionForm%3B%0A%0Apublic%20class%20FirstForm%20extends%20ActionForm%20%7B%0A%0A%09private%20String%09name%3B%0A%0A%09public%20String%20getName()%20%7B%0A%09%09return%20name%3B%0A%09%7D%0A%0A%09public%20void%20setName(String%20name)%20%7B%0A%09%09this.name%20%3D%20name%3B%0A%09%7D%0A%0A%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. package  com.awp.test.form;  
  2.   
  3. import  org.apache.struts.action.ActionForm;  
  4.   
  5. public   class  FirstForm  extends  ActionForm {  
  6.   
  7.     private  String  name;  
  8.   
  9.     public  String getName() {  
  10.         return  name;  
  11.     }  
  12.   
  13.     public   void  setName(String name) {  
  14.         this .name = name;  
  15.     }  
  16.   
  17. }  
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;
	}

}

 

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://awp.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=package%20com.awp.test.form%3B%0A%0Aimport%20org.apache.struts.action.ActionForm%3B%0A%0Apublic%20class%20SecondForm%20extends%20ActionForm%20%7B%0A%0A%09private%20String%09name%3B%0A%0A%09public%20String%20getName()%20%7B%0A%09%09return%20name%3B%0A%09%7D%0A%0A%09public%20void%20setName(String%20name)%20%7B%0A%09%09this.name%20%3D%20name%3B%0A%09%7D%0A%0A%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. package  com.awp.test.form;  
  2.   
  3. import  org.apache.struts.action.ActionForm;  
  4.   
  5. public   class  SecondForm  extends  ActionForm {  
  6.   
  7.     private  String  name;  
  8.   
  9.     public  String getName() {  
  10.         return  name;  
  11.     }  
  12.   
  13.     public   void  setName(String name) {  
  14.         this .name = name;  
  15.     }  
  16.   
  17. }  
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代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://awp.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3C!DOCTYPE%20struts-config%20PUBLIC%20%22-%2F%2FApache%20Software%20Foundation%2F%2FDTD%20Struts%20Configuration%201.1%2F%2FEN%22%20%0A%22http%3A%2F%2Fjakarta.apache.org%2Fstruts%2Fdtds%2Fstruts-config_1_1.dtd%22%3E%0A%3Cstruts-config%3E%0A%09%3Cform-beans%3E%0A%09%09%3Cform-bean%20name%3D%22firstform%22%20type%3D%22com.awp.test.form.FirstForm%22%2F%3E%0A%09%09%3Cform-bean%20name%3D%22secondform%22%20type%3D%22com.awp.test.form.SecondForm%22%2F%3E%0A%09%3C%2Fform-beans%3E%0A%09%3Cglobal-forwards%3E%0A%09%09%3Cforward%20name%3D%22display%22%20path%3D%22%2Fdisplay.jsp%22%2F%3E%0A%09%3C%2Fglobal-forwards%3E%0A%09%3Caction-mappings%3E%0A%09%09%3Caction%20path%3D%22%2Ffirstpath%22%0A%09%09%09%09input%3D%22%2Ffirst.jsp%22%0A%09%09%09%09name%3D%22firstform%22%0A%09%09%09%09parameter%3D%22method%22%0A%09%09%09%09scope%3D%22request%22%0A%09%09%09%09type%3D%22com.awp.test.action.MultiFormTestAction%22%2F%3E%0A%09%09%09%09%0A%09%09%3Caction%20path%3D%22%2Fsecondpath%22%0A%09%09%09%09input%3D%22%2Fsecond.jsp%22%0A%09%09%09%09name%3D%22secondform%22%0A%09%09%09%09parameter%3D%22method%22%0A%09%09%09%09scope%3D%22request%22%0A%09%09%09%09type%3D%22com.awp.test.action.MultiFormTestAction%22%2F%3E%09%09%09%09%0A%09%3C%2Faction-mappings%3E%0A%09%3Cmessage-resources%20parameter%3D%22MessageResources%22%20%2F%3E%20%0A%3C%2Fstruts-config%3E%0A%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"   
  3. "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd " >   
  4. < struts-config >   
  5.     < form-beans >   
  6.         < form-bean   name = "firstform"   type = "com.awp.test.form.FirstForm" />   
  7.         < form-bean   name = "secondform"   type = "com.awp.test.form.SecondForm" />   
  8.     </ form-beans >   
  9.     < global-forwards >   
  10.         < forward   name = "display"   path = "/display.jsp" />   
  11.     </ global-forwards >   
  12.     < action-mappings >   
  13.         < action   path = "/firstpath"   
  14.                 input = "/first.jsp"   
  15.                 name = "firstform"   
  16.                 parameter = "method"   
  17.                 scope = "request"   
  18.                 type = "com.awp.test.action.MultiFormTestAction" />   
  19.                   
  20.         < action   path = "/secondpath"   
  21.                 input = "/second.jsp"   
  22.                 name = "secondform"   
  23.                 parameter = "method"   
  24.                 scope = "request"   
  25.                 type = "com.awp.test.action.MultiFormTestAction" />                 
  26.     </ action-mappings >   
  27.     < message-resources   parameter = "MessageResources"   />    
  28. </ struts-config >   
<?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

Html代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://awp.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%25%40%20page%20language%3D%22java%22%20pageEncoding%3D%22UTF-8%22%25%3E%0A%3C!doctype%20html%20public%20%22-%2F%2FW3C%2F%2FDTD%20XHTML%201.0%20Transitional%2F%2FEN%22%20%0A%22http%3A%2F%2Fwww.w3.org%2FTR%2Fxhtml1%2FDTD%2Fxhtml1-transitional.dtd%22%3E%0A%3Chtml%3E%0A%20%20%3Chead%3E%0A%20%20%20%20%3Ctitle%3E%E6%88%91%E6%98%AF%E6%8F%90%E4%BA%A4%E7%AC%AC%E4%B8%80%E4%B8%AAFORM%E7%9A%84%E9%A1%B5%E9%9D%A2%3C%2Ftitle%3E%0A%20%20%3C%2Fhead%3E%0A%20%20%3Cbody%3E%0A%20%20%20%3Cform%20action%3D%22%3C%25%3Drequest.getContextPath()%25%3E%2Ffirstpath.do%3Fmethod%3DfirstForm%22%20method%3D%22post%22%3E%0A%20%20%20%09%3Cinput%20name%3D%22name%22%20value%3D%22%22%20type%3D%22text%22%2F%3E%0A%20%20%20%09%3Cinput%20name%3D%22ok%22%20value%3D%22%E6%8F%90%E4%BA%A4%22%20type%3D%22submit%22%2F%3E%0A%20%20%20%3C%2Fform%3E%0A%20%20%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. < %@ page  language = "java"   pageEncoding = "UTF-8" % >   
  2. <!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN"   
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >   
  4. < html >   
  5.   < head >   
  6.     < title > 我是提交第一个FORM的页面 </ title >   
  7.   </ head >   
  8.   < body >   
  9.    < form   action = "<%=request.getContextPath()%>/firstpath.do?method=firstForm"   method = "post" >   
  10.     < input   name = "name"   value = ""   type = "text" />   
  11.     < input   name = "ok"   value = "提交"   type = "submit" />   
  12.    </ form >   
  13.   </ body >   
  14. </ html >   
<%@ 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

Html代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://awp.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%25%40%20page%20language%3D%22java%22%20pageEncoding%3D%22UTF-8%22%25%3E%0A%3C!doctype%20html%20public%20%22-%2F%2FW3C%2F%2FDTD%20XHTML%201.0%20Transitional%2F%2FEN%22%20%0A%22http%3A%2F%2Fwww.w3.org%2FTR%2Fxhtml1%2FDTD%2Fxhtml1-transitional.dtd%22%3E%0A%3Chtml%3E%0A%20%20%3Chead%3E%0A%20%20%20%20%3Ctitle%3E%E6%88%91%E6%98%AF%E6%8F%90%E4%BA%A4%E7%AC%AC%E4%BA%8C%E4%B8%AAFORM%E7%9A%84%E9%A1%B5%E9%9D%A2%3C%2Ftitle%3E%0A%20%20%3C%2Fhead%3E%0A%20%20%3Cbody%3E%0A%20%20%20%3Cform%20action%3D%22%3C%25%3Drequest.getContextPath()%25%3E%2Fsecondpath.do%3Fmethod%3DsecondForm%22%20method%3D%22post%22%3E%0A%20%20%20%09%3Cinput%20name%3D%22name%22%20value%3D%22%22%20type%3D%22text%22%2F%3E%0A%20%20%20%09%3Cinput%20name%3D%22ok%22%20value%3D%22%E6%8F%90%E4%BA%A4%22%20type%3D%22submit%22%2F%3E%0A%20%20%20%3C%2Fform%3E%0A%20%20%3C%2Fbody%3E%0A%3C%2Fhtml%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. < %@ page  language = "java"   pageEncoding = "UTF-8" % >   
  2. <!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN"   
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >   
  4. < html >   
  5.   < head >   
  6.     < title > 我是提交第二个FORM的页面 </ title >   
  7.   </ head >   
  8.   < body >   
  9.    < form   action = "<%=request.getContextPath()%>/secondpath.do?method=secondForm"   method = "post" >   
  10.     < input   name = "name"   value = ""   type = "text" />   
  11.     < input   name = "ok"   value = "提交"   type = "submit" />   
  12.    </ form >   
  13.   </ body >   
  14. </ html >   
<%@ 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

Html代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://awp.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%25%40%20page%20language%3D%22java%22%20pageEncoding%3D%22UTF-8%22%25%3E%0A%3C!doctype%20html%20public%20%22-%2F%2FW3C%2F%2FDTD%20XHTML%201.0%20Transitional%2F%2FEN%22%20%0A%22http%3A%2F%2Fwww.w3.org%2FTR%2Fxhtml1%2FDTD%2Fxhtml1-transitional.dtd%22%3E%0A%3Chtml%3E%0A%20%20%3Chead%3E%0A%20%20%20%20%3Ctitle%3E%E6%98%BE%E7%A4%BA%E9%A1%B5%E9%9D%A2%3C%2Ftitle%3E%0A%20%20%3C%2Fhead%3E%0A%20%20%3Cbody%3E%0A%20%20%3Cbr%20%2F%3E%0A%20%20%20%E8%A1%A8%E5%8D%95%E6%8F%90%E4%BA%A4%E7%9A%84%E5%86%85%E5%AE%B9%E4%B8%BA%EF%BC%9A%24%7Bname%7D%20%3Cbr%20%2F%3E%3Cbr%20%2F%3E%0A%20%20%20%3Ca%20href%3D%22first.jsp%22%3E%E6%89%93%E5%BC%80%E8%A1%A8%E5%8D%951%3C%2Fa%3E%3Cbr%20%2F%3E%3Cbr%20%2F%3E%0A%20%20%20%3Ca%20href%3D%22second.jsp%22%3E%E6%89%93%E5%BC%80%E8%A1%A8%E5%8D%952%3C%2Fa%3E%0A%20%20%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. < %@ page  language = "java"   pageEncoding = "UTF-8" % >   
  2. <!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN"   
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >   
  4. < html >   
  5.   < head >   
  6.     < title > 显示页面 </ title >   
  7.   </ head >   
  8.   < body >   
  9.   < br   />   
  10.    表单提交的内容为:${name} < br   /> < br   />   
  11.    < a   href = "first.jsp" > 打开表单1 </ a > < br   /> < br   />   
  12.    < a   href = "second.jsp" > 打开表单2 </ a >   
  13.   </ body >   
  14. </ html >   
<%@ 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。

你可能感兴趣的:(apache,jsp,bean,struts,Flash)