DispatchAction

同一个Action完成对数据的增、删、改、查,其实就是开发一个DispatchAction的子类,这里的方法与execute方法同参、同返回值,在配置中需要在action标签下添加parameter属性

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foudation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
	<form-beans>
		<form-bean type="StudentForm" name="demo.StudentForm"></form-bean>
	</form-beans>
	
	<action-mappings>
		<action path="/studentAction" type="demo.StudentAction" name="StudentForm" parameter="method"></action>
	</action-mappings>
</struts-config>

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>ActionServlet</servlet-name>
	<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>  
  <init-param>
		<param-name>config</param-name>
		<param-value>/WEB-INF/struts-config.xml</param-value>
 	 </init-param>
 	 <load-on-startup>0</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>ActionServlet</servlet-name>
	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

package demo;

import java.io.PrintWriter;

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;

public class StudentAction extends DispatchAction {

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		PrintWriter out=response.getWriter();
		out.print("add...");
		return null;
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		PrintWriter out=response.getWriter();
		out.print("delete...");
		return null;
	}
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		PrintWriter out=response.getWriter();
		out.print("update...");
		return null;
	}
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		PrintWriter out=response.getWriter();
		out.print("query...");
		return null;
	}

}


你可能感兴趣的:(DispatchAction)