首页>代码>Springboot整合上传文件到阿里云OSS示例demo>/aliyun-oss/src/main/java/com/fei/action/BaseAction.java
package com.fei.action;

import com.fei.util.JsonUtil;
import com.fei.vo.OperateResultVO;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName: BaseAction.java
 * @Description: 
 * @Date: 2017年7月19日  上午10:02:08
 *
 */
@Scope("prototype")
public class BaseAction {
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;

	@ModelAttribute
	public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
		this.session = request.getSession();
	}
	
	protected void printOperateSuccessJSON(String message){
		OperateResultVO vo = new OperateResultVO();
		vo.setState(true);
		vo.setMessage(message);
		printJSON(vo);
	}

	protected void crossDomainJSON(String message){
		OperateResultVO vo = new OperateResultVO();
		vo.setState(true);
		vo.setMessage(message);
		response.setContentType("text/html");
		response.setContentType("text/plain");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Access-Control-Allow-Origin", "*");//添加跨域访问
		response.setDateHeader("Expires", 0);
		PrintWriter out=null;
		try {
			out = response.getWriter();
			String JSON = JsonUtil.parseObj2Json(vo);
			out.print(JSON);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			out.close();
		}
	}


	protected void printOperateSuccessJSON(String message, Object rs){
		OperateResultVO vo = new OperateResultVO();
		vo.setState(true);
		vo.setMessage(message);
		vo.setRs(rs);
		printJSON(vo);
	}
	
	
	protected void printOperateResultJSON(boolean result){
		OperateResultVO vo = new OperateResultVO();
		vo.setState(result);
		String message="";
		if(result){
			message="操作成功!";
		}else{
			message="操作失败!";
		}
		
		vo.setMessage(message);
		printJSON(vo);
	}
	
	protected void printOperateFailureJSON(String message){
		OperateResultVO vo = new OperateResultVO();
		vo.setState(false);
		vo.setMessage(message);
		printJSON(vo);
	}
	
	protected void  printJSON(Object obj) {
        response.setContentType("text/html");  
        PrintWriter out=null;
        try {
			out = response.getWriter();
			String JSON = JsonUtil.parseObj2Json(obj);
			out.print(JSON);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
	        out.close();  
		}
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    dateFormat.setLenient(false);  
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}
}
最近下载更多
912299793  LV21 3月30日
gchln1  LV5 2021年7月21日
newhaijun  LV15 2021年6月24日
1551862273  LV6 2020年11月26日
sky19961212  LV18 2020年11月12日
Stton丶C  LV3 2020年9月8日
sanstyle  LV2 2020年8月13日
tandechun  LV3 2020年6月10日
哈哈zr  LV11 2020年6月9日
renxiao123  LV17 2020年5月31日
最近浏览更多
912299793  LV21 3月30日
青柠编程  LV13 2023年5月22日
胡明杨 2023年5月15日
暂无贡献等级
哇塞塞哈哈哈  LV8 2023年5月4日
2022-09-19 2022年9月19日
暂无贡献等级
2468867327  LV10 2022年6月27日
qianxiaogeng 2022年5月20日
暂无贡献等级
徐超平  LV2 2022年3月21日
bbtoybbtoy 2022年3月21日
暂无贡献等级
Luis虎子  LV16 2021年12月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友