您的位置:首页 > 编程语言 > Java开发

struts1_DispatchAction 可以同时处理多个请求的Action

2015-08-12 00:00 218 查看
摘要: struts1_DispatchAction 可以同时处理多个请求的Action

下面是一段:html请求

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP '1.jsp' starting page</title>
</head>

<body>

<html:link action="/BookAction?method=add">添加图书</html:link>
<html:link action="/BookAction?method=delete">删除图书</html:link>
<html:link action="/BookAction?method=update">修改图书</html:link>
<html:link action="/BookAction?method=find">查找图书</html:link>

<br/> <br/>---------------------------------------------<br/><br/>

<html:link action="/addbook">添加图书</html:link>
<html:link action="/deletebook">删除图书</html:link>
<html:link action="/updatebook">修改图书</html:link>
<html:link action="/findbook">查找图书</html:link>

</body>
</html>

下面是配置文件:struts-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>

<action-mappings>
<action path="/BookAction"
type="cn.itcast.web.action.BookAction"
parameter="method"> <!-- 告诉struts,要调用的方法名称是通过什么参数带过来的 -->
</action>

</action-mappings>

</struts-config>

下面是处理请求的java代码:

//DispatchAction---action  ---execute()  add

/*
String method = mapping.getParamter();//拿到配置的参数的名称:method
String methodName = request.getParameter(method);  //add update拿到的是请求参数method对应的值:add还是delete。。。
然后反射BookAction,调用对应的请求方法

*
*/
public class BookAction extends DispatchAction {

public ActionForward add(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {

System.out.println("add....");
return null;
}

public ActionForward update(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub

System.out.println("udpate....");
return null;
}

public ActionForward delete(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub

System.out.println("delete....");
return null;
}

public ActionForward find(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
// TODO Auto-generated method stub
System.out.println("find....");
return null;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: