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

com.j2mvc.framework.action.Action 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.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import com.j2mvc.util.json.JSONFactory;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.j2mvc.framework.Session;
import com.j2mvc.framework.mapping.ContentType;

import org.w3c.dom.Document;
 
/** 
 * Action
 * 
 * @author 杨朔
 * @version 1.0 2014-2-23
 * @version 1.1.6 2014-8-17
 */
public abstract class Action {
	
	
	protected HttpServletResponse response;
	protected HttpServletRequest request;
	protected ActionBean bean;
	protected PrintWriter out;
	protected String path;
	protected HttpSession session;
	protected JSONObject jsonData;
	protected Document xmlData;
	protected String requestBody;
	protected UploadBean uploadBean;
	
	public UploadBean getUploadBean() {
		return uploadBean;
	}

	public void setUploadBean(UploadBean uploadBean) {
		this.uploadBean = uploadBean;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
		try {
			this.out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
		this.path = request.getContextPath();
		this.session = request.getSession();
		put("path", path);
		put("PATH", path);
		put("ROOT_PATH", path);
	}

	public JSONObject getJsonData() {
		return jsonData;
	}

	public void setJsonData(JSONObject jsonData) {
		this.jsonData = jsonData;
	}

	public Document getXmlData() {
		return xmlData;
	}

	public void setXmlData(Document xmlData) {
		this.xmlData = xmlData;
	}

	public String getRequestBody() {
		return requestBody;
	}

	public void setRequestBody(String requestBody) {
		this.requestBody = requestBody;
	}

	public abstract String onStart();

	public ActionBean getBean() {
		return bean;
	}

	public void setBean(ActionBean bean) {
		this.bean = bean;
		put("title", bean.getTitle());
		put("TITLE", bean.getTitle());
		put("keywords", bean.getKeywords());
		put("KEYWORDS", bean.getKeywords());
		put("DESCRIPTION", bean.getDescription());
		put("description", bean.getDescription());
	}

	public void setAttribute(String name, Object value) {
		request.setAttribute(name, value);
	}

	/**
	 * 获取数组参数
	 * 
	 * @param name
	 * 
	 */
	protected Object[] getParams(String name) {
		return request.getParameterValues(name);
	}

	/**
	 * 获取参数值
	 * 
	 * @param name
	 * 
	 */
	protected String getParam(String name) {
		String contentType = bean.getContentType();
		String value = null;
		if(contentType!=null && ContentType.JSON.equalsIgnoreCase(contentType)) {
			try {
				value = jsonData.getString(name);
			} catch (JSONException e) {
				Logger.getLogger(Action.class).warn("读取JSON值错误:"+e.getMessage());
			}
		}else {
			value = request.getParameter(name) != null ? request.getParameter(name).trim() : "";
		}
		if(value != null) {
			// 编码转换
			value = getCharset(value);
		}
		return value;
	}

	protected JSONObject getJsonBody(){
		return jsonData;
	}
	/**
	 * 获取UTF8格式
	 * 
	 * @param value
	 * 
	 */
	protected String getCharset(String value) {
		if (value == null)
			return "";
		try {
			if (java.nio.charset.Charset.forName("ISO-8859-1").newEncoder().canEncode(value)) {
				value = new String(value.getBytes("ISO-8859-1"), Session.encoding);
			} else if (java.nio.charset.Charset.forName("UTF-8").newEncoder().canEncode(value)) {
				value = new String(value.getBytes("UTF-8"), Session.encoding);
			} else if (java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(value)) {
				value = new String(value.getBytes("GBK"), Session.encoding);
			} else if (java.nio.charset.Charset.forName("GB2312").newEncoder().canEncode(value)) {
				value = new String(value.getBytes("GB2312"), Session.encoding);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}

	/**
	 * 输出JSON字符串
	 * 
	 * @param object
	 */
	public void printJson(Object object) {
		Object jsonObject =  new JSONFactory().toJsonObject(object, true);
		try {
			// 为保证输出正确的JSON格式,需要先清除之前输出的所有内容
			HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper(response);
			wrapper.resetBuffer();
			wrapper.getResponse().getWriter().print(jsonObject != null ? jsonObject.toString() : "");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 输出JSON字符串
	 * 
	 * @param s
	 */
	public void print(String s) {
		try {
			response.getWriter().print(s);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 设置页面参数
	 * 
	 * @param name
	 * @param value
	 */
	public void put(String name, Object value) {
		request.setAttribute(name, value);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy