目标:学会使用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使用技术和技巧。
相关推荐
总的来说,学习Struts1.x需要掌握MVC模式,理解其核心组件如Action、Form Bean和配置文件的使用,熟悉视图层的实现以及相关的开发工具和库。通过实践和项目经验,可以逐步深入理解这个框架,并提升Java Web开发技能...
struts2-core-2.0.1.jar, struts2-core-2.0.11.1.jar, struts2-core-2.0.11.2.jar, struts2-core-2.0.11.jar, struts2-core-2.0.12.jar, struts2-core-2.0.14.jar, struts2-core-2.0.5.jar, struts2-core-2.0.6.jar,...
- **Struts1.x** 配置文件多,包括struts-config.xml、web.xml等,配置繁琐。 - **Struts2.x** 配置文件较少,主要为struts.xml,支持注解配置,减少XML配置的繁琐。 8. **插件与扩展性**: - **Struts1.x** ...
8. **Exception Handling**:Struts1.x提供了异常处理机制,通过配置struts-config.xml中的全局异常映射,可以捕获并处理运行时异常,提高系统的健壮性。 9. **Plug-in架构**:Struts1.x具有强大的插件扩展能力,...
- Struts 1.x的主要组件包括ActionForm、Action、Dispatcher(现在称为Servlet Filter)、配置文件(struts-config.xml)等。它的MVC模式帮助分离业务逻辑和视图层,简化了Web应用的开发。 这三者组合在一起,可能...
这个插件允许开发者在Struts 2中轻松地创建能够返回JSON的Action,使得前端(通常使用JavaScript库如jQuery)可以获取到JSON数据并进行进一步处理。 现在我们来详细讨论这两个库在实际应用中的作用: 1. **json-...
- **配置文件**:`struts-config.xml`是Struts1的配置文件,定义了Action类和ActionForm对象,以及URL到Action的映射。 - **ActionForm**:用于接收并封装来自客户端的表单数据,然后传递给Action处理。 2. **...
- **手动搭建**:在 MyEclipse 中手动添加 Struts1.x 的相关依赖库,配置 web.xml 文件,并创建 struts-config.xml 文件来定义 Action 映射等配置。 - **IDE 工具支持**:利用 MyEclipse 或 Eclipse 等集成开发环境 ...
3. **配置文件**:主要包括struts-config.xml,这是Struts1.x的主配置文件,用于定义Action、ActionForm、ActionMapping等组件。在这里,你可以配置URL与Action类的映射,设置数据验证规则等。 4. **ActionForm**:...
spring.jar spring-aop.jar spring-aop.jar spring-beans.jar spring-hibernate3.jar spring-jdbc.jar spring-struts.jar spring-web.jar
包含struts2-core-2.5.10.1.jar,struts2-jfreechart-plugin-2.5.10.1.jar,struts2-json-plugin-2.5.10.1.jar,struts2-junit-plugin-2.5.10.1.jar,struts2-bean-validation-plugin-2.5.10.1.jar,struts2-cdi-...
- 首先,你需要下载Struts1.x的库文件,如struts-1.3.10-all.zip,并将其解压缩。 - 在解压后的lib目录中,你可以找到所有必需的jar文件,包括Struts框架的实现和其他依赖。 - 创建一个新的Web项目,例如myStruts...
Struts1.x_action 是一个与Apache Struts 1.x框架相关的资源包,它包含了用于构建基于Java的企业级Web应用程序的组件。Struts是MVC(Model-View-Controller)设计模式的一个实现,它使得开发者可以更有效地管理和...
struts框架 所用到的包 主要用的是MVC框架 不过之中的标签和 struts1.x版本不同,值得我们学习
3. **配置文件**:`struts-config.xml`是Struts1.x的核心配置文件,用于定义Action、ActionForm、ActionForward等元素,以及数据源和国际化资源等。 二、ActionForm与数据绑定 1. **ActionForm**:ActionForm对象...
1. **ActionServlet**:这是Struts框架的核心,作为Servlet拦截请求,根据配置文件(struts-config.xml)将请求分发到相应的Action。 2. **Action**:开发者自定义的类,负责处理用户请求,通常会与业务逻辑层交互...
1、升级所需要的jar(见附件): freemarker-2.3.22.jar ognl-3.0.19.jar struts2-convention-plugin-2.3.32.jar struts2-core-2.3.32.jar struts2-spring-plugin-2.3.32.jar xwork-core-2.3.32.jar 2、删除...
struts2-config-browser-plugin-2.3.24.jar, struts2-core-2.3.24.jar, struts2-jasperreports-plugin-2.3.24.jar, struts2-jfreechart-...struts2-struts1-plugin-2.3.24.jar, struts2-tiles-plugin-2.3.24.jar,
以下是关于Struts1.x国际化的一些核心知识点: 1. **资源包(Resource Bundle)**: - 在Java中,资源包是包含本地化字符串和其他可配置资源的文件。对于Struts1,这些通常以`.properties`格式存储,例如`messages...
- **struts-config.xml**:这是Struts1.x的核心配置文件,定义了ActionMapping、ActionForm和Forward等元素。在这个例子中,你需要配置一个ActionMapping来映射登录请求,并指定对应的Action类。 - **web.xml**:...