下面是一段: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;
}
}