⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 esimplerequestprocessor.java

📁 这是一个轻便的j2ee的web应用框架,是一个在多个项目中运用的实际框架,采用struts,hebinate,xml等技术,有丰富的tag,role,navigation,session,dictio
💻 JAVA
字号:
package com.esimple.framework.web.init;


import java.io.IOException;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.*;
import org.apache.struts.Globals;

import org.apache.struts.util.RequestUtils;

import com.esimple.framework.web.action.*;
import com.esimple.framework.web.taglib.html.Constants;
import com.esimple.framework.dao.ibatis.*;
import com.esimple.framework.bean.*;

/**
 * 扩展struts的RequestProcessor
 * @author steven
 * @version 0.9
 */
public class ESimpleRequestProcessor extends RequestProcessor {

	/**
	 * <p>Process an <code>HttpServletRequest</code> and create the
	 * corresponding <code>HttpServletResponse</code>.</p>
	 *
	 * @param request The servlet request we are processing
	 * @param response The servlet response we are creating
	 *
	 * @exception IOException if an input/output error occurs
	 * @exception ServletException if a processing exception occurs
	 */
	public void process(HttpServletRequest request,
						HttpServletResponse response)
		throws IOException, ServletException {
			
			HttpSession session = request.getSession();
			
			super.process(request,response);

	}


	/**
	 * Return an <code>Action</code> instance that will be used to process
	 * the current request, creating a new one if necessary.
	 *
	 * @param request The servlet request we are processing
	 * @param response The servlet response we are creating
	 * @param mapping The mapping we are using
	 *
	 * @exception IOException if an input/output error occurs
	 */
	protected Action processActionCreate(HttpServletRequest request,
										 HttpServletResponse response,
										 ActionMapping mapping)
		throws IOException {

		// Acquire the Action instance we will be using (if there is one)
		String className = mapping.getType();
		
		ServletContext ctx = servlet.getServletContext();
		//获得spring beanFactory
		BeanContainer factory =BeanContainerFactory.getBeanContainer();
			
			
		if (factory == null) {
			if (log.isDebugEnabled()) {
				log.info("Spring BeanFactory not loaded by SpringPlugin,so use standard struts");
			}
			return super.processActionCreate(request,response,mapping);    
		}
		
		Action action;
		if( factory.containsBean(className) ) {
			//register bean in spring
			action = (Action) factory.getBean(className);
			action.setServlet(servlet);
			return action;
		}else{
			log.info("not find Bean:"+ className + " in BeanFactory");
			Action newAction = super.processActionCreate(request,response,mapping); 
			if( newAction instanceof IbatisSupportAction){
				String defaultDao= (String)ctx.getAttribute("defaultDao");
				if( defaultDao != null ){
					ISqlMapClientDao dao = (ISqlMapClientDao)factory.getBean(defaultDao);
					( (IbatisSupportAction)newAction ).setSqlMapDao(dao);
				}	
			}//instanceof end
			return newAction;
		}
	
	}
	
	protected void processPopulate(HttpServletRequest request,
								   HttpServletResponse response,
								   ActionForm form,
								   ActionMapping mapping)
		throws ServletException {

		if (form == null) {
			return;
		}

		// Populate the bean properties of this ActionForm instance
		if (log.isDebugEnabled()) {
			log.debug(" populate Form properties in ESimpleRequestProcessor");
		}
		form.setServlet(this.servlet);
		form.reset(mapping, request);
		if (mapping.getMultipartClass() != null) {
			request.setAttribute(Globals.MULTIPART_KEY,
								 mapping.getMultipartClass());
		}
		
		if( form instanceof BaseForm ){
			//if 使用BaseForm,则采用特殊的值设置方法。
			populate((BaseForm)form, mapping.getPrefix(), mapping.getSuffix(),
										  request);
		}else
			RequestUtils.populate(form, mapping.getPrefix(),
									mapping.getSuffix(),
							  		request);

		// Set the cancellation request attribute if appropriate
		if ((request.getParameter(Constants.CANCEL_PROPERTY) != null) ||
			(request.getParameter(Constants.CANCEL_PROPERTY_X) != null)) {
			request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE);
		}

	}
	
	/**
	 * populate BaseForm properties,the implemation is 
	 * convert name to value(name)
	 * @param bean
	 * @param prefix
	 * @param suffix
	 * @param request
	 * @throws ServletException
	 */
	private static void populate(
		BaseForm bean,
		String prefix,
		String suffix,
		HttpServletRequest request)
		throws ServletException  {

		// Build a list of relevant request parameters from this request
		HashMap properties = new HashMap();
		// Iterator of parameter names
		Enumeration names = null;
		// Map for multipart parameters
		Map multipartParameters = null;

		names = request.getParameterNames();

		while (names.hasMoreElements()) {
			String name = (String) names.nextElement();
			String[] values = request.getParameterValues(name);
			if( values.length > 1 )	{
				bean.setValue(name,values);
			}else{
				bean.setValue(name,values[0]);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -