目标:学会使用struts1.x的四种常用的Action,派发技术的使用
推荐:推荐使用DispatchAction类
对象:适合自学者、初学者、兴趣爱好者。
理念: 先行动(Coding),后理解(Thinking) ; 在最短的时间内采取最大量的Coding 。 分享越多,收获越大
--------------------------------------------------------------------------------------------------------------------------------
一、继承Action类,java代码如下:
package com.raky.train.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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.action.DynaActionForm; /** * struts1.x UserAction * * @author raky * @version v1.0 * */ public class UserAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String method = request.getParameter("method"); if(null != method && method.length() > 0){ if(method.equals("add")){ return this.add(mapping, form, request, response); } if(method.equals("update")){ return this.update(mapping, form, request, response); } if(method.equals("delete")){ return this.delete(mapping, form, request, response); } } return null; } public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm dynaForm = (DynaActionForm) form; dynaForm.set("message", "执行action1添加方法"); return mapping.findForward("success"); } public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm dynaForm = (DynaActionForm) form; dynaForm.set("message", "执行action1更新方法"); return mapping.findForward("success"); } public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm dynaForm = (DynaActionForm) form; dynaForm.set("message", "执行action1删除方法"); return mapping.findForward("success"); } }
页面代码,如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://struts.apache.org/tags-html-el" prefix="html-el" %> <c:set var="ctx" value="${pageContext.request['contextPath']}" /> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Raky train struts1.x - action1 - Test1 page</title> <script type="text/javascript"> function submitForm(methodValue) { document.forms[0].action = "${ctx}/userAction.do?method=" + methodValue; document.forms[0].submit(); } </script> </head> <body> <h2>raky - struts1.x - action1</h2> <html-el:form action="/userAction" method="post"> <table> <tr> <td align="center" colspan="2"> <html-el:submit property="submitAdd" onclick="submitForm('add')" value="添加" /> <html-el:submit property="submitUpdate" onclick="submitForm('update')" value="修改" /> <html-el:submit property="submitDelete" onclick="submitForm('delete')" value="删除" /> <html-el:button property="btnValue" value="返回" onclick="location.href=${ctx}/" /> </td> </tr> </table> </html-el:form> </body> </html>
二、继承DispatchAction,java代码如下:
package com.raky.train.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.action.DynaActionForm; import org.apache.struts.actions.DispatchAction; /** * struts1.x UserDispatchAction * * @author raky * @version v1.0 * */ public class UserDispatchAction extends DispatchAction { public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm dynaForm = (DynaActionForm) form; dynaForm.set("message", "执行action2添加方法"); return mapping.findForward("success"); } public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm dynaForm = (DynaActionForm) form; dynaForm.set("message", "执行action2更新方法"); return mapping.findForward("success"); } public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm dynaForm = (DynaActionForm) form; dynaForm.set("message", "执行action2删除方法"); return mapping.findForward("success"); } }
页面代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://struts.apache.org/tags-html-el" prefix="html-el" %> <c:set var="ctx" value="${pageContext.request['contextPath']}" /> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Raky train struts1.x - action2 - Test2 page</title> <script type="text/javascript"> function submitForm(methodValue) { document.forms[0].action = "${ctx}/userDispatchAction.do?method=" + methodValue; document.forms[0].submit(); } </script> </head> <body> <h2>raky - struts1.x - action2</h2> <html-el:form action="/userDispatchAction" method="post"> <table> <tr> <td align="center" colspan="2"> <html-el:link href="${ctx}/userDispatchAction.do?method=add">添加</html-el:link> <html-el:link href="${ctx}/userDispatchAction.do?method=update">修改</html-el:link> <html-el:link href="${ctx}/userDispatchAction.do?method=delete">删除</html-el:link> <html-el:link href="${ctx}/">返回</html-el:link> </td> </tr> </table> </html-el:form> </body> </html>
三、继承EventDispatchAction,java代码如下:
package com.raky.train.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.action.DynaActionForm; import org.apache.struts.actions.EventDispatchAction; /** * struts1.x UserLookupDispatchAction * * @author raky * @version v1.0 * */ public class UserEventDispatchAction extends EventDispatchAction { public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm dynaForm = (DynaActionForm) form; dynaForm.set("message", "执行action3添加方法"); return mapping.findForward("success"); } public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm dynaForm = (DynaActionForm) form; dynaForm.set("message", "执行action3更新方法"); return mapping.findForward("success"); } public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm dynaForm = (DynaActionForm) form; dynaForm.set("message", "执行action3删除方法"); return mapping.findForward("success"); } }
页面代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://struts.apache.org/tags-html-el" prefix="html-el" %> <c:set var="ctx" value="${pageContext.request['contextPath']}" /> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Raky train struts1.x - action3 - Test3 page</title> </head> <body> <h2>raky - struts1.x - action3</h2> <form action="${ctx}/userEventDispatchAction.do" method="post"> <table> <tr> <td align="center" colspan="2"> <html-el:submit property="add" value="添加" /> <html-el:submit property="update" value="修改" /> <html-el:submit property="delete" value="删除" /> <html-el:button property="btnValue" value="返回" onclick="location.href=${ctx}/" /> </td> </tr> </table> </form> </body> </html>
四、继承LookupDispatchAction,java代码如下:
package com.raky.train.action; import java.util.HashMap; import java.util.Map; 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.action.DynaActionForm; import org.apache.struts.actions.LookupDispatchAction; /** * struts1.x UserLookupDispatchAction * * @author raky * @version v1.0 * */ public class UserLookupDispatchAction extends LookupDispatchAction { @SuppressWarnings({ "rawtypes", "unchecked" }) protected Map getKeyMethodMap() { Map map = new HashMap(); map.put("dynaForm.add", "add"); map.put("dynaForm.update", "update"); map.put("dynaForm.delete", "delete"); return map; } public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm dynaForm = (DynaActionForm) form; dynaForm.set("message", "执行action4添加方法"); return mapping.findForward("success"); } public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm dynaForm = (DynaActionForm) form; dynaForm.set("message", "执行action4更新方法"); return mapping.findForward("success"); } public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm dynaForm = (DynaActionForm) form; dynaForm.set("message", "执行action4删除方法"); return mapping.findForward("success"); } }
资源文件代码如下:
dynaForm.add = add dynaForm.update = update dynaForm.delete = delete
页面代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://struts.apache.org/tags-html-el" prefix="html-el" %> <c:set var="ctx" value="${pageContext.request['contextPath']}" /> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Raky train struts1.x - action4 - Test4 page</title> <script type="text/javascript"> function submitForm(methodValue) { document.forms[0].action = "${ctx}/userLookupAction.do?method=" + methodValue; document.forms[0].submit(); } </script> </head> <body> <h2>raky - struts1.x - action4</h2> <html-el:form action="/userLookupAction" method="post"> <table> <tr> <td align="center" colspan="2"> <input type="submit" name="submitAdd" onclick="submitForm('add')" value="添加" /> <input type="button" name="btnUpdate" onclick="submitForm('update')" value="修改" /> <input type="submit" name="submitDelete" onclick="submitForm('delete')" value="删除" /> <html-el:button property="btnValue" value="返回" onclick="location.href=${ctx}/" /> </td> </tr> </table> </html-el:form> </body> </html>
总体strutsConfig.xml配置文件代码如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <!-- =================== Form Bean Definitions ==================== --> <form-beans> <form-bean name="dynaForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="message" type="java.lang.String" /> </form-bean> </form-beans> <!-- ================= Global Forward Definitions ================= --> <global-forwards> <forward name="success" path="/success.jsp" /> </global-forwards> <!-- =================== Action Definitions ==================== --> <action-mappings> <action input="/action/test1.jsp" name="dynaForm" path="/userAction" scope="request" type="com.raky.train.action.UserAction" validate="false" /> <action input="/action/test2.jsp" parameter="method" name="dynaForm" path="/userDispatchAction" scope="request" type="com.raky.train.action.UserDispatchAction" validate="false" /> <action input="/action/test3.jsp" parameter="add,update,delete" name="dynaForm" path="/userEventDispatchAction" scope="request" type="com.raky.train.action.UserEventDispatchAction" validate="false" /> <action input="/action/test4.jsp" parameter="method" name="dynaForm" path="/userLookupAction" scope="request" type="com.raky.train.action.UserLookupDispatchAction" validate="false" /> </action-mappings> <!-- ================ Message Resources Definitions ================ --> <message-resources parameter="MessageResources" /> </struts-config>
总结:本文基本上总结了struts1.x常用的Action使用技术和技巧。
相关推荐
基于java的贝儿米幼儿教育管理系统答辩PPT.pptx
本压缩包资源说明,你现在往下拉可以看到压缩包内容目录 我是批量上传的基于SpringBoot+Vue的项目,所以描述都一样;有源码有数据库脚本,系统都是测试过可运行的,看文件名即可区分项目~ |Java|SpringBoot|Vue|前后端分离| 开发语言:Java 框架:SpringBoot,Vue JDK版本:JDK1.8 数据库:MySQL 5.7+(推荐5.7,8.0也可以) 数据库工具:Navicat 开发软件: idea/eclipse(推荐idea) Maven包:Maven3.3.9+ 系统环境:Windows/Mac
基于java的消防物资存储系统答辩PPT.pptx
项目经过测试均可完美运行! 环境说明: 开发语言:java jdk:jdk1.8 数据库:mysql 5.7+ 数据库工具:Navicat11+ 管理工具:maven 开发工具:idea/eclipse
项目经过测试均可完美运行! 环境说明: 开发语言:java jdk:jdk1.8 数据库:mysql 5.7+ 数据库工具:Navicat11+ 管理工具:maven 开发工具:idea/eclipse
TA_lib库(whl轮子),直接pip install安装即可,下载即用,非常方便,各个python版本对应的都有。 使用方法: 1、下载下来解压; 2、确保有python环境,命令行进入终端,cd到whl存放的目录,直接输入pip install TA_lib-xxxx.whl就可以安装,等待安装成功,即可使用! 优点:无需C++环境编译,下载即用,方便
使用软件自带的basic脚本编辑制作的脚本 低版本软件无法输出Excel报告,可以通过脚本方式实现这一功能
基于java的就业信息管理系统答辩PPT.pptx
25法理学背诵逻辑.apk.1g
基于java的大学生校园兼职系统答辩PPT.pptx
做到代码,和分析的源数据
本压缩包资源说明,你现在往下拉可以看到压缩包内容目录 我是批量上传的基于SpringBoot+Vue的项目,所以描述都一样;有源码有数据库脚本,系统都是测试过可运行的,看文件名即可区分项目~ |Java|SpringBoot|Vue|前后端分离| 开发语言:Java 框架:SpringBoot,Vue JDK版本:JDK1.8 数据库:MySQL 5.7+(推荐5.7,8.0也可以) 数据库工具:Navicat 开发软件: idea/eclipse(推荐idea) Maven包:Maven3.3.9+ 系统环境:Windows/Mac
项目经过测试均可完美运行! 环境说明: 开发语言:java jdk:jdk1.8 数据库:mysql 5.7+ 数据库工具:Navicat11+ 管理工具:maven 开发工具:idea/eclipse
适用于ensp已经入门人群的学习,有一定难度
基于java的数码论坛系统设计与实现答辩PPT.pptx
tornado-6.4.1-cp38-abi3-macosx_10_9_universal2.whl
基于java的医院信管系统答辩PPT.pptx
项目经过测试均可完美运行! 环境说明: 开发语言:java jdk:jdk1.8 数据库:mysql 5.7+ 数据库工具:Navicat11+ 管理工具:maven 开发工具:idea/eclipse
tornado-4.2.tar.gz
链表 合并两个链表,链表基础操作