提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.common; |
E |
2 |
|
|
3 |
import com.hx.exception.ParamException; |
|
4 |
import com.hx.exception.ServiceException; |
|
5 |
import com.hx.mybatisTool.SqlParam; |
|
6 |
import com.hx.mybatisTool.SqlSentence; |
|
7 |
import org.springframework.beans.propertyeditors.CustomDateEditor; |
|
8 |
import org.springframework.web.bind.WebDataBinder; |
|
9 |
import org.springframework.web.bind.annotation.InitBinder; |
|
10 |
import org.springframework.web.context.request.RequestContextHolder; |
|
11 |
import org.springframework.web.context.request.ServletRequestAttributes; |
|
12 |
import org.springframework.web.context.request.WebRequest; |
|
13 |
|
|
14 |
import javax.servlet.http.HttpServletRequest; |
|
15 |
import javax.servlet.http.HttpSession; |
|
16 |
import java.text.DateFormat; |
|
17 |
import java.text.SimpleDateFormat; |
|
18 |
import java.util.Date; |
|
19 |
import java.util.HashMap; |
|
20 |
import java.util.Map; |
|
21 |
|
|
22 |
|
|
23 |
/**公共初始化 |
|
24 |
* @author ChenJiaHe |
|
25 |
* @Date 2020-06-11 |
|
26 |
*/ |
|
27 |
|
|
28 |
public class CommonInit { |
|
29 |
|
|
30 |
/*请不要声明变量,会导致不安全,因为这个是单列*/ |
|
31 |
|
|
32 |
protected HttpServletRequest request = null; |
|
33 |
protected HttpSession session = null; |
|
34 |
|
|
35 |
|
|
36 |
//只需要加上下面这段即可,注意不能忘记注解 |
|
37 |
@InitBinder |
|
38 |
public void initBinder(WebDataBinder binder, WebRequest request) { |
|
39 |
//转换日期 注意这里的转化要和传进来的字符串的格式一直 如2015-9-9 就应该为yyyy-MM-dd |
|
40 |
DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
41 |
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器 |
|
42 |
} |
|
43 |
|
|
44 |
public HttpServletRequest getRequest() { |
|
45 |
//获取参数对象 |
|
46 |
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
|
47 |
request = attributes.getRequest(); |
|
48 |
return request; |
|
49 |
} |
|
50 |
|
|
51 |
public HttpSession getSession() { |
|
52 |
//获取参数对象 |
|
53 |
session = getRequest() .getSession(); |
|
54 |
return session; |
|
55 |
} |
|
56 |
|
|
57 |
/** |
|
58 |
* 获取request里某个属性 |
|
59 |
* @param attrName 属性名称 |
|
60 |
* @return 对象 |
|
61 |
*/ |
|
62 |
public Object getRequestAttribute(String attrName) |
|
63 |
{ |
|
64 |
return getRequest().getAttribute(attrName); |
|
65 |
} |
|
66 |
|
|
67 |
/** |
|
68 |
* 设置一个request属性 |
|
69 |
* @param attrName 属性名称 |
|
70 |
* @param attrObject 属性值 |
|
71 |
*/ |
|
72 |
public void setRequestAttribute(String attrName, Object attrObject) { |
|
73 |
getRequest().setAttribute(attrName, attrObject); |
|
74 |
} |
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
/** |
|
79 |
* 抛出服务异常 |
|
80 |
* @param msg 错误信息 |
|
81 |
*/ |
|
82 |
public void throwServiceException(String msg) |
|
83 |
{ |
|
84 |
throw new ServiceException(msg); |
|
85 |
} |
|
86 |
|
|
87 |
/** |
|
88 |
* 抛出参数异常 |
|
89 |
* @param msg 错误信息 |
|
90 |
*/ |
|
91 |
public void throwParamException(String msg) |
|
92 |
{ |
|
93 |
throw new ParamException(msg); |
|
94 |
} |
|
95 |
|
|
96 |
|
|
97 |
} |