非常讨厌Struts1 Action的设计,一堆花俏的概念,没必要的复杂度.
但是工作关系,还非要使用这个垃圾,没办法,只好把苍蝇包起来咽下去.
做一个Action基类,SimpleAction ,把它封装的更像webwork.
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtilsBean; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping;
/** * @author jindw * @xwork.package name = "" */ public final class SimpleAction extends AbstractAction { /** * Commons Logging instance. */ protected static Log log = LogFactory.getLog(SimpleAction.class);
/** * common result name define */ public static final String SUCCESS = "success";
public static final String ERROR = "error";
public static final String INPUT = "input";
/** * method cache */ protected Map executeMap = new HashMap();
protected Map executeArgumentTypesMap = new HashMap();
public SimpleAction() { super(); Method[] methods = this.getClass().getMethods(); for (int i = 0; i < methods.length; i++) {
Method method = methods[i]; if (String.class != method.getReturnType()) { continue; } Class[] params = method.getParameterTypes(); int argCount = 0; for (int j = 0; j < params.length; j++) { if (ActionForm.class.isAssignableFrom(params[j])) { argCount++; } else if (HttpServletRequest.class.isAssignableFrom(params[j])) { argCount++; } else if (HttpServletResponse.class .isAssignableFrom(params[j])) { argCount++; } else if (ActionMapping.class.isAssignableFrom(params[j])) { argCount++; } else { argCount = -1; break; } } if (argCount >= 0) { executeMap.put(method.getName(), method); executeArgumentTypesMap.put(method.getName(), params); } } initialize(); }
protected void initialize() { Method[] methods = this.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { Class[] paramTypes = methods[i].getParameterTypes(); if (paramTypes.length == 1) { String name = methods[i].getName(); if (name.startsWith("set")) { name = name.substring(3); if (name.length() > 0) { char fc = name.charAt(0); if (Character.isUpperCase(fc)) { name = Character.toLowerCase(fc) + name.substring(1); //implement it eg:get from Spring Context Object value = getBean(name); if (value != null) { try { methods[i].invoke(this, new Object[] { value }); } catch (Exception e) { log.info("set property error:", e); } } } } } } } }
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Get the method's name. This could be overridden in subclasses. String methodName = getMethodName(mapping, form); // Invoke the named method, and return the result String result = dispatchMethod(mapping, form, request, response, methodName); request.setAttribute("actionForm", form); if(result == null){ return null; }else{ return transformForward(mapping.findForward(result), request); } }
/** * @param forward * @param request * @return */ protected ActionForward transformForward(ActionForward forward, HttpServletRequest request) { if (forward == null) { return null; } else { StringBuffer buf = new StringBuffer(forward.getPath()); boolean containsVariable = false; for (int k = 0, i = buf.indexOf("${", k); i >= 0; i = buf.indexOf( "${", k), k = i) { int j = buf.indexOf("}", i); if (j > 0) { containsVariable = true; k = j; String key = buf.substring(i + 2, j); Object o = null; if(key.indexOf('.')<0){ o=request.getAttribute(key); }else{ String[] keys = key.split("[.]"); o=request.getAttribute(keys[0]); for(int l = 1;l<keys.length;l++){ try { o = BeanUtilsBean.getInstance().getPropertyUtils().getProperty(o, keys[l].trim()); } catch (Exception e) { log.debug("find property error:", e); o = null; break; } } } buf.replace(i, j + 1, String.valueOf(o)); } } if (containsVariable) { forward = new ActionForward(forward); forward.setPath(buf.toString()); return forward; } else { return forward; } } }
public static void main(String[] args) { StringBuffer buf = new StringBuffer("http://sdssfs${123}&{123}&${sd}"); for (int k = 0, i = buf.indexOf("${", k); i >= 0; i = buf.indexOf("${", k), k = i) { int j = buf.indexOf("}", i); if (j > 0) { k = j; String key = buf.substring(i + 2, j); buf.replace(i, j + 1, "%" + key + "%"); } } System.out.println(buf); }
protected String dispatchMethod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String methodName) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Method method = (Method) executeMap.get(methodName); Class[] argumentTypes = (Class[]) executeArgumentTypesMap .get(methodName); Object[] params = new Object[argumentTypes.length]; for (int i = 0; i < params.length; i++) { Class type = argumentTypes[i]; if (ActionForm.class.isAssignableFrom(type)) { if(type.isAssignableFrom(form.getClass())){ params[i] = form; }else{ throw new ClassCastException("action form type is:"+form.getClass()+";but required:"+type+";"); } } else if (HttpServletRequest.class.isAssignableFrom(type)) { params[i] = request; } else if (HttpServletResponse.class.isAssignableFrom(type)) { params[i] = response; } else if (ActionMapping.class.isAssignableFrom(type)) { params[i] = mapping; } } return (String) method.invoke(this, params); }
protected String getMethodName(ActionMapping mapping, ActionForm form) { String param = mapping.getParameter(); if (param != null) { int i = param.indexOf("method="); if (i > 0) { int j = param.indexOf(i, ';'); if (j >= 0) { return param.substring(i + ("method=".length()), j).trim(); } else { return param.substring(i + ("method=".length())).trim(); } } else { if (this.executeMap.containsKey(param)) { return param; } } } return "execute"; }
} |
分享到:
相关推荐
赠送jar包:struts-core-1.3.8.jar; 赠送原API文档:struts-core-1.3.8-javadoc.jar; 赠送源代码:struts-core-1.3.8-sources.jar; 赠送Maven依赖信息文件:struts-core-1.3.8.pom; 包含翻译后的API文档:struts...
绝对是官网最新版本2012年5月9日下载的,方便大家使用,很不错的资源,期待大家分享,只因我们都是ssh人~(所有相关jar包在:struts-2.3.3-all.zip\struts-2.3.3\lib)docs中都有相应的例子,会告诉你怎么用!...
这个插件允许开发者在Struts 2中轻松地创建能够返回JSON的Action,使得前端(通常使用JavaScript库如jQuery)可以获取到JSON数据并进行进一步处理。 现在我们来详细讨论这两个库在实际应用中的作用: 1. **json-...
在使用struts-2.3.31-all.zip压缩包时,通常会包含以下组件: - `struts2-core.jar`:框架核心库,包含了Action、Interceptor、Result等主要组件。 - `struts2-convention-plugin.jar`:约定优于配置插件,简化...
这个"struts-2.5.12-min 精简的核心li包"是Struts 2框架的一个轻量级版本,包含了运行Struts 2应用程序所必需的基础组件。 首先,我们来看一下Struts 2框架的主要组成部分: 1. **Action**:在Struts 2中,Action...
Struts-xwork-core是Struts2框架的核心组件,它提供了Action和结果的执行模型,以及类型转换、数据验证和国际化等功能。在这个压缩包中,包含了该核心库的源代码,对于学习和理解Struts2的工作原理及其内部机制极具...
1. `struts-2.3.16.jar`:核心库,包含框架的基本组件。 2. `struts2-plugins`目录:各种插件,如Freemarker、Tiles、JSON等。 3. `struts2-dojo-plugin`:提供与Dojo库的集成,用于创建富客户端界面。 4. `struts2-...
这个"struts-2.5.2-all.zip"压缩包包含了Struts 2.5.2版本的所有必需组件,方便开发者在项目中快速集成和使用。 首先,我们来看看Struts 2的核心概念: 1. **Action类**:它是处理用户请求的核心,负责业务逻辑的...
这个“struts-1.3.8-all.zip”官方完整包包含了运行和开发Struts 1.3.8应用所需的所有组件和资源。 首先,我们来了解一下Struts的核心概念和组件: 1. **ActionServlet**:这是Struts框架的核心控制器,负责处理...
struts1.38包,struts-core-1.3.8.jarstruts1.38包,struts-core-1.3.8.jar
Struts2.0.11-all.zip 是一个包含Apache Struts 2.0.11版本的完整包,它是一个广泛使用的开源MVC框架,专为Java开发人员设计,用于构建可维护、易于扩展的企业级Web应用程序。Struts 2是Struts 1的升级版,提供了更...
Struts 2是Java Web开发中的一个开源框架,主要用于构建MVC(模型-视图-控制器)架构..."struts-2.3.32-all"包是学习和使用Struts 2不可或缺的资源,涵盖了框架的所有组成部分,对于Java Web开发者来说具有很高的价值。
在使用 "struts-1.3.8-all.jar" 文件时,开发者通常需要将其加入项目的类路径(classpath),这样整个框架就可以运行起来。同时,还需要编写相应的 Action 类、ActionForm 类以及配置文件,以实现特定的应用逻辑和...
1. **核心库**:主要由`struts-core.jar`组成,提供了Action、Form、PlugIn等核心组件,以及Struts的配置管理、请求处理机制。 2. **标签库**:包括`struts-bean.jar`、`struts-html.jar`、`struts-logic.jar`和`...
1. **Action和Result**: Struts2的动作(Action)是业务逻辑的执行单元,而结果(Result)则定义了动作执行后的跳转页面或处理方式。Action类可以自定义,以实现特定的业务逻辑,而Result则通过配置文件来指定如何...
Struts-AJAX-JSON-Struts 包是基于经典的Struts框架,为了实现AJAX(异步JavaScript和XML)和JSON(JavaScript Object Notation)交互功能而设计的一组库和资源集合。这个包通常包含了Struts框架的核心组件,以及...
2. **配置文件**:Struts2使用XML配置文件(通常命名为struts.xml或struts-default.xml)来定义Action、结果、拦截器等。这些配置告诉Struts如何映射HTTP请求到Action类,以及Action执行后如何显示结果。 3. **拦截...
3. **结果类型与结果配置**:Action执行后,通常会有一个结果返回给用户,这可以通过定义结果类型(如dispatcher、stream等)和结果配置来实现。例如,dispatcher结果类型会将响应转发到一个JSP页面。 4. **拦截器...
标题“struts-2.5所有jar包”指的是Struts2框架的2.5版本所需的全部依赖库。这些jar包是构建和运行Struts2应用程序的基础,包含了框架的核心组件、插件、以及其他必要的库。其中,`struts-2.5.16`可能是Struts2框架...
"struts-2.1.8-all-src.zip"是一个包含了Struts2 2.1.8版本所有源代码的压缩文件,特别的是,它并不包含已经编译好的jar包,这意味着开发者可以深入研究其内部实现,理解框架的工作原理,以及根据需求进行自定义修改...