BaseController继承自JFinal Controller,提供了一些公共的常用调用方法
package com.zshsoft.controller.base;
import java.math.BigDecimal;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import com.jfinal.core.Controller;
import com.jfinal.core.NotAction;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.render.JsonRender;
import com.jfinal.upload.UploadFile;
import com.zshsoft.web.kit.RequestUtils;
import com.zshsoft.web.vo.HMap;
import com.zshsoft.web.vo.PageData;
import com.zshsoft.web.vo.Tips;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
/***********************************************
* 控制类基类,封装一些公用的方法
*
* @author 周光
*
*/
public class BaseController extends Controller {
private static Logger log = Logger.getLogger(BaseController.class);
public final static String IDS = "ids";// ID集合储存变量
public final static String ID = "id";
/**
* 重写getModel方法,使得表单name的前缀不区分大小写
*/
@Override
@NotAction
public <T> T getModel(Class<T> modelClass) {
return super.getModel(modelClass, "", true);
}
/********************************************
* 获取Request中的参数
*
* @return
*/
protected Map<String, Object> getParaToMap() {
Enumeration<String> enume = getParaNames();
Map<String, Object> map = new HashMap<String, Object>();
while (enume.hasMoreElements()) {
String name = (String) enume.nextElement();
Object value = getPara(name);
map.put(name, value);
}
return map;
}
/******************************************************
* 判断IE浏览器返回JSON格式,参数为一个对象类型
*
* @param obj
* @throws Exception
*/
protected void renderObj(Object obj) {
if (obj == null) {
renderNull();
return;
}
String header = getRequest().getHeader("User-Agent").toLowerCase();
// IE10以下浏览器判断标准
int msie = header.indexOf("msie");
// IE11以上,没有MSIE字样了,改为trident
int trident = header.indexOf("trident");
boolean isIE = msie > 0 || trident > 0;// 判断是否在IE浏览器
if (isIE) {
// IE 不支持 application/json, 在 ajax 上传文件完成后返回 json时 IE 提示下载文件
render(new JsonRender(obj).forIE());
} else {
renderJson(obj);
}
}
@NotAction
public float getParaToFloat() {
String p = getPara();
return new Float(p).floatValue();
}
@NotAction
public float getParaToFloat(int i) {
String p = getPara(i);
return new Float(p).floatValue();
}
@NotAction
public float getParaToFloat(String name) {
String p = getPara(name);
return new Float(p).floatValue();
}
@NotAction
public byte[] getParaToByte(String name) {
String p = getPara(name);
return p.getBytes();
}
/****************************************************
* 保存scale位小数
*
* @param name
* @param scale
* @return
*/
@NotAction
public float getParaToFloat(String name, int scale) {
String p = getPara(name);
return NumberUtil.round(new Float(p).floatValue(), scale).floatValue();
}
/***********************************************************
* 默认值
*
* @param name
* @param defaultValue
* @return
*/
@NotAction
public float getParaToFloat(String name, float defaultValue) {
String p = getPara(name);
if (StrKit.isBlank(p)) {
return defaultValue;
}
return new Float(p).floatValue();
}
/****************************************
* 将参数对象转为BigDecimal类型
*
* @return
*/
@NotAction
public BigDecimal getParaToBigDecimal() {
String p = getPara();
return new BigDecimal(p);
}
/***************************************************
* 将参数对象转为BigDecimal类型
*
* @param name 参数名称
* @return
*/
@NotAction
public BigDecimal getParaToBigDecimal(String name) {
String p = getPara(name);
return new BigDecimal(p);
}
/****************************************************************************
* 将参数对象转为BigDecimal类型
*
* @param name 参数名称
* @param defaultValue 默认值
* @return
*/
@NotAction
public BigDecimal getParaToBigDecimal(String name, String defaultValue) {
String p = getPara(name);
if (StrKit.isBlank(p)) {
return new BigDecimal(defaultValue);
}
return new BigDecimal(p);
}
/********************************************
* 将参数对象转为BigDecimal类型
*
* @param n
* @return
*/
@NotAction
public BigDecimal getParaToBigDecimal(int n) {
String p = getPara(n);
return new BigDecimal(p);
}
/******************************************************************
* 将参数对象转为BigDecimal类型
*
* @param n
* @param defaultValue
* @return
*/
@NotAction
public BigDecimal getParaToBigDecimal(int n, String defaultValue) {
String p = getPara(n);
if (StrKit.isBlank(p)) {
return new BigDecimal(defaultValue);
}
return new BigDecimal(p);
}
/**********************************************************************
* 默认值,并且保留scale位小数
*
* @param name
* @param defaultValue
* @param scale
* @return
*/
@NotAction
public float getParaToFloat(String name, float defaultValue, int scale) {
String p = getPara(name);
if (StrKit.isBlank(p)) {
return defaultValue;
}
return NumberUtil.round(new Float(p).floatValue(), scale).floatValue();
}
/**********************************************
*
* @param name
* @return
*/
@NotAction
public String getParaValueToStr(String name) {
String[] values = getParaValues(name);
return StringUtils.join(values, ",");
}
/******************************************************************
*
* @param name
* @param defaultValue
* @return
*/
@NotAction
public String getParaValueToStr(String name, String defaultValue) {
String[] values = getParaValues(name);
if (null == values || values.length == 0) {
return defaultValue;
}
return StringUtils.join(values, ",");
}
/************************************************************************
* 返回EasyUI DataGrid格式数据
*
* @param data
* @throws Exception
*/
protected void renderDataGrid(HMap data) {
renderObj(data);
}
/*******************************************
* 默认操作成功
*
* @throws Exception
*/
protected void renderOK() {
renderObj(Ret.ok("msg", "操作成功!"));
}
/*******************************************
* 默认操作成功
*
* @throws Exception
*/
protected void renderOK(Ret ret) {
renderObj(Ret.ok("msg", "操作成功!").set(ret));
}
/*******************************************
* 操作成功,传入参数
*
* @throws Exception
*/
protected void renderOK(String msg) {
renderObj(Ret.ok("msg", msg));
}
/*******************************************
* 操作成功,是否显示提示窗口
*
* @throws Exception
*/
protected void renderOK(boolean showTip) {
renderObj(Ret.ok("msg", "操作成功!").set("showTip", showTip));
}
/***********************************************
* 操作成功
*
* @param msg
* @param info
*/
protected void renderOK(String msg, String info) {
renderObj(Ret.ok("msg", msg).set("info", info));
}
/*************************************
*
* @param url
*/
protected void renderUrl(String url) {
renderObj(Ret.ok("msg", "操作成功!").set("url", url));
}
/*************************************************
*
* @param msg
* @param url
*/
@NotAction
public void renderUrl(String msg, String url) {
renderObj(Ret.ok("msg", msg).set("url", url));
}
/**************************************************************
*
* @param flag
* @param msg
* @param url
*/
protected void renderUrl(boolean flag, String msg, String url) {
renderObj(Ret.ok("msg", msg).set("url", url).set("flag", flag));
}
/***********************************************
* 操作成功
*
* @param msg
* @param info
*/
protected void renderOK(String msg, Map<Object, Object> data) {
renderObj(Ret.ok("msg", msg).set(data));
}
/*******************************************
* 默认操作成功
*
* @throws Exception
*/
protected void renderErr() {
renderObj(Ret.fail("err", "操作失败!"));
}
/******************************************************
* 操作失败提示信息
*
* @param err
* @throws Exception
*/
@NotAction
public void renderErr(String err) {
renderObj(Ret.fail("err", err));
}
/*******************************************************
* 操作失败提示信息
*
* @param e
* @throws Exception
*/
protected void renderErr(Exception e) {
// String err = PubKit.getExceptionStackTrace(e);
e.printStackTrace();
renderObj(Ret.fail("err", e.getMessage()));
}
/*******************************************************
* 操作失败提示信息,并打印错误信息在控制台
*
* @param e
* @throws Exception
*/
protected void renderErr(Exception e, boolean print) {
if (print) {
e.printStackTrace();
}
String err = e.getMessage();
renderObj(Ret.fail("err", err));
}
@NotAction
public void renderStatus(String status, String info) {
renderObj(Ret.create("status", status).set("info", info));
}
private final static String URL_SUCCESS = "/show_info.html";
private final static String URL_ERROR = "/eapdomain/_inc/500.html";
/**************************
* 跳转到成功页面
*/
@NotAction
public void renderSuccess() {
setAttr("tip", new Tips());
render(URL_SUCCESS);
}
@NotAction
public void renderSuccess(String msg) {
Tips tip = new Tips();
tip.setText(msg);
setAttr("tip", tip);
render(URL_SUCCESS);
}
@NotAction
public void renderSuccess(String msg, String url) {
Tips tip = new Tips();
tip.setText(msg);
setAttr("tip", tip);
render(url);
}
/**************************
* 转到错误提示页面
*/
@NotAction
public void renderTipErr() {
Tips tip = new Tips();
tip.setText("操作失败!");
setAttr("tip", tip);
render(URL_ERROR);
}
/*************************************
* 转到错误提示页面
*
* @param err
*/
@NotAction
public void renderTipErr(String err) {
Tips tip = new Tips();
tip.setText(err);
setAttr("tip", tip);
render(URL_ERROR);
}
/*************************************
* 转到错误提示页面
*
* @param err
*/
@NotAction
public void renderTipErr(String err, String url) {
Tips tip = new Tips();
tip.setText(err);
setAttr("tip", tip);
render(url);
}
/**************************************
* 转到错误提示页面
*
* @param e
*/
@NotAction
public void renderTipErr(Exception e) {
e.printStackTrace();
Tips tip = new Tips();
tip.setText(e.getMessage());
setAttr("tip", tip);
render(URL_ERROR);
}
@NotAction
public Map<String, String> getParaToStringMap() {
Enumeration<String> enume = getParaNames();
Map<String, String> map = new HashMap<String, String>();
while (enume.hasMoreElements()) {
String name = (String) enume.nextElement();
map.put(name, getPara(name));
}
return map;
}
/**
* 是否是手机浏览器
*
* @return
*/
@NotAction
public boolean isMoblieBrowser() {
return RequestUtils.isMoblieBrowser(getRequest());
}
/**
* 是否是微信浏览器
*
* @return
*/
@NotAction
public boolean isWechatBrowser() {
return RequestUtils.isWechatBrowser(getRequest());
}
/**
* 是否是IE浏览器
*
* @return
*/
@NotAction
public boolean isIEBrowser() {
return RequestUtils.isIEBrowser(getRequest());
}
/**
* 是否是ajax请求
*
* @return
*/
@NotAction
public boolean isAjaxRequest() {
return RequestUtils.isAjaxRequest(getRequest());
}
/**
* 是否是multpart的请求(带有文件上传的请求)
*
* @return
*/
@NotAction
public boolean isMultipartRequest() {
return RequestUtils.isMultipartRequest(getRequest());
}
/**
* 获取ip地址
*
* @return
*/
@NotAction
public String getIPAddress() {
return RequestUtils.getIpAddress(getRequest());
}
/**
* 获取 referer
*
* @return
*/
@NotAction
public String getReferer() {
return RequestUtils.getReferer(getRequest());
}
/**
* 获取ua
*
* @return
*/
@NotAction
public String getUserAgent() {
return RequestUtils.getUserAgent(getRequest());
}
protected HashMap<String, Object> flash;
@NotAction
public Controller setFlashAttr(String name, Object value) {
if (flash == null) {
flash = new HashMap<String, Object>();
}
flash.put(name, value);
return this;
}
@NotAction
public Controller setFlashMap(Map<String, Object> map) {
if (map == null) {
throw new NullPointerException("map is null");
}
if (flash == null) {
flash = new HashMap<String, Object>();
}
flash.putAll(map);
return this;
}
@SuppressWarnings("unchecked")
@NotAction
public <T> T getFlashAttr(String name) {
return flash == null ? null : (T) flash.get(name);
}
@NotAction
public HashMap<String, Object> getFlashAttrs() {
return flash;
}
/**
* 获取当前网址
*
* @return
*/
@NotAction
public String getBaseUrl() {
HttpServletRequest req = getRequest();
int port = req.getServerPort();
return port == 80 ? String.format("%s://%s%s", req.getScheme(), req.getServerName(), req.getContextPath())
: String.format("%s://%s%s%s", req.getScheme(), req.getServerName(), ":" + port, req.getContextPath());
}
/**
* 获取所有上传的文件
*
* @return
*/
@NotAction
public HashMap<String, UploadFile> getUploadFilesMap() {
if (!isMultipartRequest()) {
return null;
}
List<UploadFile> fileList = getFiles();
HashMap<String, UploadFile> filesMap = null;
if (fileList != null && fileList.size() > 0) {
filesMap = new HashMap<String, UploadFile>();
for (UploadFile ufile : fileList) {
filesMap.put(ufile.getParameterName(), ufile);
}
}
return filesMap;
}
@NotAction
public PageData getPageData() {
return new PageData(getRequest());
}
@NotAction
public void renderErrPage(String err) {
Ret ret = Ret.fail("err", err);
set("ret", ret);
render("/eapdomain/_inc/500.html");
}
@NotAction
public void renderErrPage() {
renderErrPage("系统出错!");
}
@NotAction
public void renderErrPage(Exception e) {
e.printStackTrace();
renderErrPage(e.getMessage());
}
@NotAction
public void renderMsgPage(String path, String msg) {
Ret ret = Ret.create();
ret.set("msg", msg);
set("ret", ret);
render(path + "/msg.html");
}
@NotAction
public void renderMsgPage(String msg) {
renderMsgPage("/drug", msg);
}
@NotAction
public void renderMsgPage(Exception ex) {
ex.printStackTrace();
renderMsgPage(ex.getMessage());
}
@NotAction
public JSONObject getJSONObject() {
String inputData = getRawData();
return JSONUtil.parseObj(inputData);
}
@NotAction
public JSONArray getJSONArray() {
String inputData = getRawData();
return JSONUtil.parseArray(inputData);
}
}
文档更新时间: 2020-06-30 16:43 作者:周光