www.pudn.com > dangdang.zip > ActionServlet.java, change:2006-06-22,size:5556b


package org.smartstruts.action; 
 
import java.io.IOException; 
import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import org.apache.commons.beanutils.BeanUtils; 
import org.apache.commons.digester.Digester; 
import org.apache.commons.digester.xmlrules.DigesterLoader; 
import org.smartstruts.config.ActionConfig; 
import org.smartstruts.config.FormBeanConfig; 
import org.smartstruts.config.ForwardConfig; 
import org.smartstruts.config.ModuleConfig; 
import org.smartstruts.test.TestParseXML; 
 
public class ActionServlet extends javax.servlet.http.HttpServlet { 
 
	public ModuleConfig moduleConfig = new ModuleConfig(); 
	// 规则文件路径 
	private static final String RULE = "org/smartstruts/config/rule.xml"; 
	// smart-struts配置文件所在位置 
	private static final String CONFIG = "struts-config.xml"; 
 
	public void init() { 
		try { 
			Digester digester = DigesterLoader 
					.createDigester(ActionServlet.class.getClassLoader() 
							.getResource(RULE)); 
			digester.push(moduleConfig); 
			digester.parse(ActionServlet.class.getClassLoader().getResource( 
					CONFIG)); 
		} catch (Exception e) { 
			e.printStackTrace(); 
		} 
	} 
 
	public void service(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException { 
		try { 
			// 根据请求解析出请求的path 
			String path = processPath(request, response); 
			// 根据path获取Action配置 
			ActionConfig actionConfig = moduleConfig.findActionConfig(path); 
			if (actionConfig != null) { 
				// 寻找对应的FormBean封装Request中的请求信息 
				ActionForm form = processActionForm(actionConfig, request, 
						response); 
				if (form != null) { 
					// 执行Action对象的execute方法处理,返回ForwardConfig 
					String forwardName = processAction(actionConfig, form, 
							request, response); 
					// 根据forward对象执行页面跳转 
					processForward(forwardName, actionConfig, request, response); 
				}else{ 
					// 执行Action对象的execute方法处理,返回ForwardConfig 
					String forwardName = processAction(actionConfig, form, 
							request, response); 
					// 根据forward对象执行页面跳转 
					processForward(forwardName, actionConfig, request, response); 
			 	} 
			} 
		} catch (Exception e) { 
			throw new ServletException(e); 
		} 
	} 
 
	private String processPath(HttpServletRequest request, 
			HttpServletResponse response) { 
		String uri = request.getServletPath(); 
		String path = uri.substring(0, uri.lastIndexOf(".")); 
		return path; 
	} 
 
	private ActionForm processActionForm(ActionConfig actionConfig, 
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception { 
		String formName = actionConfig.getName(); 
		ActionForm form = null; 
 
		if (formName != null) { 
 
			// request|session中查找 
			String scope = actionConfig.getScope(); 
			if ("request".equals(scope)) { 
				form = (ActionForm) request.getAttribute(actionConfig 
						.getAttribute()); 
			} else { 
				form = (ActionForm) request.getSession().getAttribute( 
						actionConfig.getAttribute()); 
			} 
 
			if (form == null) { 
				// 如果session|request中没有form就创建新的form对象 
				FormBeanConfig formBeanCfg = moduleConfig 
						.findFormBeanConfig(formName); 
				if (formBeanCfg != null) { 
					String formBeanType = formBeanCfg.getType(); 
					Class formBeanClass = Class.forName(formBeanType); 
					form = (ActionForm) formBeanClass.newInstance(); 
 
					if ("request".equals(scope)) { 
						request.setAttribute(actionConfig.getAttribute(), form); 
					} else { 
						request.getSession().setAttribute( 
								actionConfig.getAttribute(), form); 
					} 
				} 
			} 
			// 利用BeanUtils组件将请求信息放到form对象中 
			BeanUtils.populate(form, request.getParameterMap()); 
		} 
		return form; 
	} 
 
	private String processAction(ActionConfig actionConfig, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception { 
		// 获取type属性值,利用反射创建action实例 
		String actionType = actionConfig.getType(); 
		Class actionClass = Class.forName(actionType); 
		Action action = (Action) actionClass.newInstance(); 
 
		String methodName = actionConfig.getMethod(); 
 
		// 获得Method对象 
		Method method = action.getClass().getMethod( 
				methodName, 
				new Class[] { ActionForm.class, HttpServletRequest.class, 
						HttpServletResponse.class }); 
 
		// 调用方法,返回响应视图标识String 
		String forwardName = (String) method.invoke(action, new Object[] { 
				form, request, response }); 
 
		return forwardName; 
	} 
 
	private void processForward(String forwardName, ActionConfig actionConfig, 
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception, IOException { 
		ForwardConfig forwardCfg = actionConfig.findForward(forwardName); 
		// 如果<action>元素下没找到forward信息,去<global-forwards>中找 
		if (forwardCfg == null) { 
			forwardCfg = moduleConfig.findForwardConfig(forwardName); 
		} 
		// 如果找到了forward配置,执行跳转处理 
		if (forwardCfg != null) { 
			String forwardPath = forwardCfg.getPath(); 
			if (forwardPath != null) { 
				// 如果redirect为false 
				if (!forwardCfg.isRedirect()) { 
					request.getRequestDispatcher(forwardPath).forward(request, 
							response); 
				} else {// 如果redirect为true 
					String ctxPath = request.getContextPath(); 
					response.sendRedirect(ctxPath + forwardPath); 
				} 
			} 
		} 
	} 
 
}