All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.j2mvc.framework.dispatcher.DispatcherForward Maven / Gradle / Ivy

Go to download

强烈建议使用J2mvc 2.1以后的版本。 version 2.1.01 1.优化路径跳转,Servlet和Filter方式的路径设置 2.优化内存销毁 3.更换JSON依赖包 4.优化接收方法RequestMethod,封装不同ContentType格式提交 封装JSON,XML数据提交模块 JSON请求示例 { "id":"JSON134851", "title":"JSON提交的标题", "price":65.1, "stock":100 } XML请求示例 <!DOCTYPE root [ <!ELEMENT root ANY> <!ATTLIST Product SSN ID #REQUIRED>]> <root> <Product SSN='id'>XMLID12354</Product> <Product SSN='title'>XML提交的标题 </Product> <Product SSN='price'>55 </Product> <Product SSN='stock'>32 </Product> </root> version 2.1.02 1.解决URL无后缀情况无法加载静态资源,解决无法渲染CSS文件。 version 2.1.03 1.移除com.j2mvc.StringUtils.getUtf8()方法调用 更改为getCharset() version 2.1.04 1.去除Servlet和Filter的全局变量销毁,只交给Listener处理。 version 2.1.05,2.1.06,2.1.07 1.完善POST提交的JSON数据 支持接收基础数据类型、任意对象类型、任意数组类型。 不支持接收参数为集合类型或Map类型,但可以定义为接收对象类型的元素。 version 2.1.05,2.1.06,2.1.07 1.修改连接池变量 version 2.1.09 增加上传功能,修改RequestMethod,ContentType设置方式 version 2.1.10,2.1.11 更改上传文件名格式为UUID格式,移除JSON映射类,更改接收多文件上传。 version 2.1.12 删除文件列有的空对象 version 2.1.13 增加配置文件目录/conf,加载上传功能配置/conf/upload.properties version 2.1.18 拦截器也能获取ActionBean version 2.1.20 添加上传文件只读权限 version 2.1.21 支持同时接收文件和文本数据 version 2.1.22 增加文件接收类型media version 2.1.23 删除upload类printJson方法

The newest version!
package com.j2mvc.framework.dispatcher;

import java.io.IOException;
import java.lang.reflect.Method;

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

import org.apache.log4j.Logger;

import com.j2mvc.framework.action.ActionBean;
import com.j2mvc.framework.action.UploadBean;
import com.j2mvc.framework.dispatcher.reader.DefaultReader;
import com.j2mvc.framework.dispatcher.reader.FileReader;
import com.j2mvc.framework.dispatcher.reader.FormDataReader;
import com.j2mvc.framework.dispatcher.reader.JSONReader;
import com.j2mvc.framework.dispatcher.reader.XMLReader;
import com.j2mvc.framework.mapping.ContentType;
import com.j2mvc.framework.mapping.RequestMethod;
import com.j2mvc.framework.util.InvokeUtils;

/**  
 * 页面调配
 * @author 杨朔
 * @version 1.0 2014-2-23
 * @version 1.1.6 2014-8-17
 */
public class DispatcherForward {
	static final Logger  log = Logger.getLogger(DispatcherForward.class);

	private HttpServletRequest request;
	private HttpServletResponse response;
	private ActionBean bean;
	/**
	 * 构造器
	 * @param request
	 * @param response
	 * @param bean
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public DispatcherForward(HttpServletRequest request,
			HttpServletResponse response, 
			ActionBean bean) throws ServletException, IOException {
		super();
		this.request = request;
		this.response = response;
		this.bean = bean;
		init();
	}
	/**
	 * 初始化Action
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void init() throws ServletException, IOException{
		execute();
	}
	/**
	 * 执行方法
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void execute() throws ServletException, IOException{
		
		String className = bean.getClassName().indexOf(".")!=-1?bean.getClassName():
								bean.getPackageName() + "."+bean.getClassName();
		Class clazz = null;
		try {
			clazz = Class.forName(className);
		} catch (ClassNotFoundException e) {
			log.error("执行方法>>"+e.getMessage());
		}
		if(clazz!=null){
			try {
				Object obj = clazz.newInstance();
				InvokeUtils.invoke(clazz, "setRequest", obj, new Object[]{request},HttpServletRequest.class);
				InvokeUtils.invoke(clazz, "setResponse", obj,  new Object[]{response},HttpServletResponse.class);
				InvokeUtils.invoke(clazz, "setBean", obj,  new Object[]{bean},ActionBean.class);
				InvokeUtils.invoke(clazz, "setAttribute", obj,  new Object[]{"path",request.getContextPath()},String.class,Object.class);
				InvokeUtils.invoke(clazz, "setAttribute", obj,  new Object[]{"WEB_ROOT",request.getContextPath()},String.class,Object.class);
				InvokeUtils.invoke(clazz, "setAttribute", obj,  new Object[]{"PATH",request.getContextPath()},String.class,Object.class);
				// 执行当前Action方法
				Object result = InvokeUtils.invoke(clazz, "onStart", obj, null);
				if(result == null) {
					// 读取请求参数的数据
					// 按请求方式和数据类型分配读取方式
					Method method = bean.getMethod();
					String contentType = bean.getContentType();
					String requestMethod = request.getMethod();
					log.info("地址:"+bean.getPath()+bean.getUri()+";请求数据格式:"+contentType+";请求方法:"+requestMethod+".");
					
					if(requestMethod!=null && requestMethod.equalsIgnoreCase(RequestMethod.POST)) {
						contentType = contentType!=null && !contentType.trim().equals("")?contentType:ContentType.XWwwFormUrlencoded;
						if(contentType!=null && ContentType.FormData.equalsIgnoreCase(contentType)) {
							// multipart/form-data
							log.info(" read FormData.");
							result = new FormDataReader(request,method, obj).result();
						}else if(contentType!=null && ContentType.FILE.equalsIgnoreCase(contentType)) {
							// multipart/file
							log.info(" read File data.");
							FileReader reader  = new FileReader(request,method,obj,response,bean.getActionUpload());
							log.info("接收上传完毕.");
							InvokeUtils.invoke(clazz, "setUploadBean", obj,  new Object[]{reader.getUploadBean()},UploadBean.class);
							result = reader.result();
						}else if(contentType!=null && ContentType.XWwwFormUrlencoded.equalsIgnoreCase(contentType)) {
							// application/x-www-form-urlencoded
							log.info(" read XWwwFormUrlencoded, use DefaultReader.");
							result =  new DefaultReader(request,method, obj).result();
						}else if(contentType!=null && ContentType.JSON.equalsIgnoreCase(contentType)) {
							// application/json
							log.info(" read JSON.");
							JSONReader reader =  new JSONReader(request,method, obj);
							result = reader.result();
						}else if(contentType!=null && (
								ContentType.XML.equalsIgnoreCase(contentType)
								|| ContentType.XML_TEXT.equalsIgnoreCase(contentType))) {
							// text/xml
							// application/xml
							log.info(" read XML.");
							XMLReader reader = new XMLReader(request,method, obj);
							result = reader.result();
						}else {
							log.error("请求数据格式“"+contentType+"”不正确");
						}
					}else {
						// GET方法读取数据
						log.info(" read default.");
						result = new DefaultReader(request,method, obj).result();
					}
				}
				String file = result instanceof String?(String) result:null;
				forward(file);
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 跳转页面
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void forward(String file) throws ServletException, IOException{
		if(file==null)
			return;
		file = file.startsWith("/")?file:bean.getDir()+file;
		if(file!=null && !response.isCommitted()){
			if(bean.isIncude()){
				log.info(file+" is includePage");
				request.getRequestDispatcher(file).include(request, response);
			}else{
				request.getRequestDispatcher(file).forward(request, response);
			}
			response.getWriter().flush();
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy