Source Code Cross Referenced for ActionExtend.java in  » Portal » DLOG4J » web » struts » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » DLOG4J » web.struts 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  This program is free software; you can redistribute it and/or modify
003:         *  it under the terms of the GNU General Public License as published by
004:         *  the Free Software Foundation; either version 2 of the License, or
005:         *  (at your option) any later version.
006:         *
007:         *  This program is distributed in the hope that it will be useful,
008:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
009:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
010:         *  GNU Library General Public License for more details.
011:         *
012:         *  You should have received a copy of the GNU General Public License
013:         *  along with this program; if not, write to the Free Software
014:         *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015:         */
016:        package web.struts;
017:
018:        import java.lang.reflect.InvocationTargetException;
019:        import java.lang.reflect.Method;
020:        import java.sql.*;
021:        import java.util.Enumeration;
022:        import javax.servlet.ServletContext;
023:        import javax.servlet.http.HttpServletRequest;
024:        import javax.servlet.http.HttpServletResponse;
025:        import javax.sql.DataSource;
026:        import org.apache.commons.beanutils.BeanUtils;
027:        import org.apache.struts.Globals;
028:        import org.apache.struts.action.*;
029:
030:        /**
031:         * 实现了事件映射扩展
032:         * 
033:         * @author Winter Lau
034:         */
035:        public abstract class ActionExtend extends Action {
036:
037:            /**
038:             * Action类执行入口
039:             */
040:            public ActionForward execute(ActionMapping mapping, ActionForm form,
041:			HttpServletRequest req, HttpServletResponse res) throws Exception {
042:		String param = null;
043:		String value = null;
044:		for (Enumeration enum = req.getParameterNames(); enum.hasMoreElements();) {
045:			String t_param = (String) enum.nextElement();
046:			if (t_param.startsWith(SUBMIT_BUTTON_PREFIX)) {
047:				value = req.getParameter(t_param);
048:				param = METHOD_PREFIX + t_param.substring(SUBMIT_BUTTON_PREFIX.length());
049:				break;
050:			}
051:		}
052:
053:		if (param == null)
054:			param = "doDefault";
055:		try{
056:			return callActionMethod(mapping, form, req, res, param, value);
057:		}catch(InvocationTargetException e){
058:			throw (Exception)e.getCause();
059:		}
060:	}
061:
062:            /**
063:             * 调用事件处理方法
064:             * @param mapping
065:             * @param form
066:             * @param req
067:             * @param res
068:             * @param methodName
069:             * @param value
070:             * @return
071:             * @throws Exception
072:             */
073:            protected ActionForward callActionMethod(ActionMapping mapping,
074:                    ActionForm form, HttpServletRequest req,
075:                    HttpServletResponse res, String methodName, String value)
076:                    throws Exception {
077:                Method doMethod = null;
078:                Object params[] = (Object[]) null;
079:                for (int i = 0; i < methodParams.length;)
080:                    try {
081:                        doMethod = getClass().getDeclaredMethod(methodName,
082:                                methodParams[i]);
083:                        if (doMethod.getParameterTypes().length == 4) {
084:                            params = (new Object[] { mapping, form, req, res });
085:                            break;
086:                        }
087:                        if (doMethod.getParameterTypes().length != 5)
088:                            continue;
089:                        params = new Object[] { mapping, form, req, res, value };
090:                        break;
091:                    } catch (NoSuchMethodException excp) {
092:                        i++;
093:                    }
094:
095:                if (doMethod != null) {
096:                    if (paramMapping(doMethod.getName()))
097:                        BeanUtils.populate(this , req.getParameterMap());
098:                    Object ret = doMethod.invoke(this , params);
099:                    if (doMethod.getReturnType().equals(ActionForward.class))
100:                        return (ActionForward) ret;
101:                    if (doMethod.getReturnType().equals(String.class))
102:                        return new ActionForward((String) ret);
103:                }
104:                return null;
105:            }
106:
107:            /**
108:             * 获取到默认数据库的连接
109:             * 
110:             * @return
111:             * @throws SQLException
112:             */
113:            protected Connection getConnection() throws SQLException {
114:                ServletContext context = servlet.getServletContext();
115:                DataSource ds = (DataSource) context
116:                        .getAttribute(Globals.DATA_SOURCE_KEY);
117:                return ds.getConnection();
118:            }
119:
120:            /**
121:             * 获取到指定数据库的连接
122:             * 
123:             * @return
124:             * @throws SQLException
125:             */
126:            protected Connection getConnection(String key) throws SQLException {
127:                ServletContext context = servlet.getServletContext();
128:                DataSource ds = (DataSource) context.getAttribute(key);
129:                return ds.getConnection();
130:            }
131:
132:            /**
133:             * 释放数据库资源
134:             * 
135:             * @param rs
136:             * @param stmt
137:             * @param conn
138:             */
139:            protected void close(ResultSet rs, Statement stmt, Connection conn) {
140:                if (rs != null)
141:                    try {
142:                        rs.close();
143:                    } catch (Exception exception) {
144:                    }
145:                if (stmt != null)
146:                    try {
147:                        stmt.close();
148:                    } catch (Exception exception1) {
149:                    }
150:                if (conn != null)
151:                    try {
152:                        conn.close();
153:                    } catch (Exception exception2) {
154:                    }
155:            }
156:
157:            /**
158:             * 返回是否将表单的域映射到Action类的属性上
159:             * 子类可以覆盖该方法已启用自动映射功能
160:             * 建议还是使用Struts的Formbean更符合设计模式
161:             * @param method
162:             * @return
163:             */
164:            protected boolean paramMapping(String method) {
165:                return false;
166:            }
167:
168:            public static void main(String args[]) {
169:                for (int i = 0; i < methodParams.length; i++) {
170:                    for (int j = 0; j < methodParams[i].length; j++)
171:                        System.out.print(methodParams[i][j].getName() + "\t");
172:
173:                    System.out.println();
174:                }
175:
176:            }
177:
178:            public static final String SUBMIT_BUTTON_PREFIX = "eventSubmit_";
179:
180:            public static final String METHOD_PREFIX = "do";
181:
182:            private static final Class method1Params[];
183:
184:            private static final Class method2Params[];
185:
186:            private static final Class method3Params[];
187:
188:            private static final Class method4Params[];
189:
190:            private static final Class methodParams[][];
191:            static {
192:                method1Params = (new Class[] {
193:                        org.apache.struts.action.ActionMapping.class,
194:                        org.apache.struts.action.ActionForm.class,
195:                        javax.servlet.ServletRequest.class,
196:                        javax.servlet.ServletResponse.class });
197:                method2Params = (new Class[] {
198:                        org.apache.struts.action.ActionMapping.class,
199:                        org.apache.struts.action.ActionForm.class,
200:                        javax.servlet.ServletRequest.class,
201:                        javax.servlet.ServletResponse.class,
202:                        java.lang.String.class });
203:                method3Params = (new Class[] {
204:                        org.apache.struts.action.ActionMapping.class,
205:                        org.apache.struts.action.ActionForm.class,
206:                        javax.servlet.http.HttpServletRequest.class,
207:                        javax.servlet.http.HttpServletResponse.class });
208:                method4Params = (new Class[] {
209:                        org.apache.struts.action.ActionMapping.class,
210:                        org.apache.struts.action.ActionForm.class,
211:                        javax.servlet.http.HttpServletRequest.class,
212:                        javax.servlet.http.HttpServletResponse.class,
213:                        java.lang.String.class });
214:                methodParams = (new Class[][] { method1Params, method2Params,
215:                        method3Params, method4Params });
216:            }
217:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.