两类不同的action
一、继承的是org.apache.struts.action.Action类,其方法的参数是action,并且要覆写execute方法;
二、继承的是org.apache.struts.actions.DispatchAction类其方法的参数是method;
一、
1.action的定义
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class PersonAction extends Action{
//此处最好定义静态的关于action的方法名称
private static String ORWARD_QUERY = "query";
@override
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response,
){
PersonForm actionform = (PersonForm) form;
String action = actionform.getAction();
if (action == null || action.length() == 0) {
action = "query";
}
if (action.equalsIgnoreCase("ORWARD_QUERY")) {
return query( mapping, form,request,response);
}else if(){
}
return null;
}
public ActionForward query(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse,
)
PersonForm actionform = (PersonForm) form;
String para = actionform.getName();
//逻辑判断
request.setAttribute("para",para); //有时候还可以这样传递参数,不推荐,这样程序缺乏灵活性
return mapping.findForward(ORWARD_QUERY);
}
}
2.其XML文件的配置:
name="PersonForm"
scope="request"
path="/Person"
type="com.test.action.PersonAction">
二、继承DispachAction
1.类
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 OwnerChangeAction extends DispatchAction {
//此处最好定义静态的关于action的方法名称
private static String ORWARD_QUERY = "query";
public ActionForward query(
ActionMapping mapping,
} ActionForm form,
HttpServletRequest request,
HttpServletResponse,
)
PersonForm actionform = (PersonForm) form;
String para = actionform.getName();
//逻辑判断
request.setAttribute("para",para); //有时候还可以这样传递参数,不推荐,这样程序缺乏灵活性
return mapping.findForward(ORWARD_QUERY);
}
2. 其XML文件的配置:
parameter="method"
path="/test/person"
scope="request"
type="com.test.action.PersonAction"
name="personform"
attribute="personform">