本文主要是介绍struts中的DispatcherAction简介+Demo,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
引言
小编觉得代码就是要不断的简洁,不断的加强,便于维护,这就是我们的目标!!
DispatcherAction
当我们有多个功能相似的业务,使用Action的时候,就需要建立很多个Action类,而且需要在struts-config.xml文件中配置多个<action>标签。其中好的的重复工作不说,看着就让人觉得不舒服……
所以,我们可以使用DispatcherAction帮助我们这些相似的功能方法一个Action中,各个业务逻辑通过传入不同的参数来决定执行哪个方法。
DispatcherAction的使用
1、DispatcherAction中的execute方法,如果覆盖必须显示调用super.execute()
2、Parameter参数值不能是execute和perform
3、<action>标签中的parameter属性
Demo
1、建立一个UserAction 类,继承DispatcherAction类
package com.bjpowernode.struts;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 add(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {// return mapping.findForward("add_success");return mapping.findForward("success");}public ActionForward del(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {
// return mapping.findForward("del_success");return mapping.findForward("success");}public ActionForward update(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {
// return mapping.findForward("update_success");return mapping.findForward("success");}
}
2、配置struts-config.xml文件
<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE struts-config PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN""http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"><struts-config><action-mappings><action path="/user/*"type="com.bjpowernode.struts.UserAction"parameter="command"><forward name="success" path="/user/{1}_success.jsp"/></action></action-mappings>
</struts-config>
3、建立jsp页面
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><a href="user/add.do?command=add">添加</a><a href="user/del.do?command=del">删除</a><a href="user/update.do?command=update">修改</a></body>
</html>
注意:页面上的url传的参数command与配置文件中<action>标签中的parameter属性要一致!!
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>add_success.jsp</body>
</html>
del_success.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>del_success.jsp
</body>
</html>
update_success.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>update_success.jsp
</body>
</html>
4、效果
总结
向着我们的目标前进!!
这篇关于struts中的DispatcherAction简介+Demo的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!