重温经典struts1之DispatchAction完成一个Action中编写多个方法接收页面的增删改查请求

重温经典struts1之DispatchAction完成一个Action中编写多个方法接收页面的增删改查请求

前言

之前我们学习的Action类中,编写一个方法,重写父类的execute方法,接收页面发来的请求,而且,一个action中只能写一个方法。
今天,我们学习,Action类中写多个方法,接收页面的发来的增删改查请求,具体做法就是我们今天的主要内容DispatchAction类。


DispatchAction类

同样继承Action类,通过页面请求的url参数key=value方式(属性名=方法名),调用Action中所对应的方法。
我们编写的Action只需继承DispatchAction类即可。
例如

页面请求的url是http://localhost:8080/user_maint.do?command=list
list:是Action类中的方法名
command:是必须在struts-config.xml中,action标签中配置的parameter属性值
command=list:是调用Action中list方法

需求

如下图所示,
写一套用户增删改查页面,用户列表页面中有三个按钮,
点击添加用户按钮,跳转到用户添加页面
点击修改用户按钮,跳转到用户修改页面
点击删除用户按钮,跳转到用户删除页面
通过http://localhost:8080/user_maint.do?command=list,跳转到用户列表页面
※这里我们主要演示同一个Action中完成所有页面跳转,页面的组件和service层的具体的实现,我们就不详细展开了。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

具体实现步骤

1. struts-config.xml文件中定义Action

基本的配置我们就不展开了,
这里的重点是parameter属性值:command,
和页面请求url的key值对应,就是下边url中的command
http://localhost:8080/user_maint.do?command=list
这样就可以调用Action类中定义的list方法

struts-config.xml代码如下:

     <action
         path="/user_maint"
         type="jp.iosoft.action.UserAction"
         name="userForm"
         scope="request"
         parameter="command"
         validate="false">
         <forward name="list" path="/userList.jsp"></forward>
         <forward name="addUser" path="/addUser.jsp"></forward>
         <forward name="success" path="/userList.jsp" redirect="true"></forward>
     </action>

2. 编写UserAction类

继承DispatchAction类,定义增删改查方法,来完成页面的跳转
・list:跳转到用户列表页面
・showAddUser:跳转到用户添加页面
・showModifyUser:跳转到用户修改页面
・showDeleteUser:跳转到用户删除页面

UserAction代码如下:

package jp.iosoft.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;

public class UserAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		return mapping.findForward("list");

	}

	public ActionForward showAddUser(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		return mapping.findForward("addUser");

	}

	public ActionForward showModifyUser(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		return mapping.findForward("modifyUser");

	}

	public ActionForward showDeleteUser(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		return mapping.findForward("deleteUser");

	}

}

3. 编写增删改查页面

按照下图路径配置
在这里插入图片描述

用户列表页面代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%-- <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> --%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

	<h1>用户列表</h1>
	<hr>
	<div style="float:left">
		<form action="/user_maint.do?command=showAddUser" method="post">
			<input type="submit" value="添加用户" >
		</form>
	</div>
	<div style="float:left">
		<form action="/user_maint.do?command=showModifyUser" method="post">
			<input type="submit" value="修改用户" >
		</form>
	</div>
	<div style="float:left">
		<form action="/user_maint.do?command=showDeleteUser" method="post">
			<input type="submit" value="删除用户" >
		</form>
	</div>

</body>
</html>

用户添加页面代码如下:


<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%-- <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> --%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
<script type="text/javascript">
function goBack() {
	window.self.location="user_maint.do?command=list";
}
</script>
</head>
<body>
	<h1>用户添加页面</h1>
	<hr>
	<a href="#" οnclick="goBack();">返回</a><br>
</body>
</html>

用户修改页面代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%-- <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> --%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
<script type="text/javascript">
function goBack() {
	window.self.location="user_maint.do?command=list";
}
</script>
</head>
<body>
	<h1>用户修改页面</h1>
	<hr>
	<a href="#" οnclick="goBack();">返回</a><br>
</body>
</html>

用户删除页面代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%-- <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> --%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
<script type="text/javascript">
function goBack() {
	window.self.location="user_maint.do?command=list";
}
</script>
</head>
<body>
	<h1>用户删除页面</h1>
	<hr>
	<a href="#" οnclick="goBack();">返回</a><br>
</body>
</html>

拓展

DispatchAction类中有一个unspecified方法,当页面请求url中没有指定调用方法名时,该方法被调用,通常,增删改查的查询方法写在此方法中。

上边的Action代码,可做以下修改
把list方法名改成unspecified,

UserAction修改后的代码如下:


public class UserAction extends DispatchAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		return mapping.findForward("list");

	}

}

方法调用时,使用下边的url,无需带command=list

http://localhost:8080/user_maint.do?

总结

到这里我们就完成了Action类中继承DispatchAction类,完成一个Action中编写多个方法接收页面的增删改查请求,运行效果在文章开头需求中的图可以看到,欢迎留言交流,下篇见。

  • 20
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值