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: }
|