6
6
import org .springframework .data .domain .Example ;
7
7
import org .springframework .data .domain .Pageable ;
8
8
import org .springframework .data .domain .Sort ;
9
+ import org .springframework .util .StringUtils ;
9
10
import org .springframework .web .context .request .RequestContextHolder ;
10
11
import org .springframework .web .context .request .ServletRequestAttributes ;
11
12
12
13
import java .beans .PropertyDescriptor ;
14
+ import java .util .Enumeration ;
13
15
import java .util .HashMap ;
14
16
import java .util .Map ;
15
17
import java .util .Optional ;
@@ -20,7 +22,7 @@ public class PageRequest extends org.springframework.data.domain.PageRequest {
20
22
private int current ;
21
23
private int pageSize ;
22
24
23
- private final Map <String ,Object > filters = new HashMap <>();
25
+ private final Map <String , Object > filters = new HashMap <>();
24
26
25
27
@ Getter
26
28
private final HttpServletRequest servletRequest ;
@@ -35,6 +37,19 @@ public PageRequest(int current, int pageSize, Sort sort) {
35
37
36
38
ServletRequestAttributes attributes = (ServletRequestAttributes ) RequestContextHolder .currentRequestAttributes ();
37
39
this .servletRequest = attributes .getRequest ();
40
+ this .syncParameter ();
41
+ }
42
+
43
+
44
+ private void syncParameter () {
45
+ Enumeration <String > enumeration = servletRequest .getParameterNames ();
46
+ while (enumeration .hasMoreElements ()) {
47
+ String key = enumeration .nextElement ();
48
+ String value = servletRequest .getParameter (key );
49
+ if (StringUtils .hasText (value )) {
50
+ this .filters .put (key , value );
51
+ }
52
+ }
38
53
}
39
54
40
55
public PageRequest () {
@@ -45,15 +60,25 @@ public void setCurrent(int current) {
45
60
this .current = current > 0 ? current - 1 : 0 ;
46
61
}
47
62
48
- public String getParameter (String key ){
63
+ public String getParameter (String key ) {
49
64
return servletRequest .getParameter (key );
50
65
}
51
66
52
- public String getParameter (String key ,String defaultValue ){
53
- String result = servletRequest .getParameter (key );
67
+ public String getParameter (String key , String defaultValue ) {
68
+ String result = servletRequest .getParameter (key );
54
69
return result == null ? defaultValue : result ;
55
70
}
56
71
72
+ public int getIntParameter (String key ) {
73
+ return Integer .parseInt (servletRequest .getParameter (key ));
74
+ }
75
+
76
+ public int getIntParameter (String key , int defaultValue ) {
77
+ String result = servletRequest .getParameter (key );
78
+ return result == null ? defaultValue : Integer .parseInt (result );
79
+ }
80
+
81
+
57
82
@ Override
58
83
public int getPageSize () {
59
84
return pageSize ;
@@ -127,38 +152,43 @@ public void addSort(Sort sort) {
127
152
Sort nowSort = pageRequest .getSort ();
128
153
if (nowSort == Sort .unsorted ()) {
129
154
this .pageRequest = new PageRequest (getCurrent (), getPageSize (), sort );
130
- }else {
155
+ } else {
131
156
pageRequest .getSort ().and (sort );
132
157
}
133
158
}
134
159
135
- public PageRequest addFilter (String key ,Object value ){
160
+ public PageRequest addFilter (String key , Object value ) {
136
161
this .filters .put (key , value );
137
162
return this ;
138
163
}
139
164
140
- public boolean hasFilter (){
165
+ public boolean hasFilter () {
141
166
return !this .filters .isEmpty ();
142
167
}
143
168
144
- public <T > Example <T > getExample (Class <T > clazz ){
145
- if (!hasFilter ()){
169
+ public <T > Example <T > getExample (Class <T > clazz ) {
170
+ if (!hasFilter ()) {
146
171
return null ;
147
172
}
173
+ Object entity = null ;
148
174
try {
149
- Object entity = clazz .getDeclaredConstructor ().newInstance ();
150
- PropertyDescriptor [] descriptors = BeanUtils .getPropertyDescriptors (clazz );
151
- for (PropertyDescriptor descriptor : descriptors ) {
152
- String name = descriptor .getName ();
153
- Object value = filters .get (name );
154
- if (value != null ) {
155
- descriptor .getWriteMethod ().invoke (entity ,value );
175
+ entity = clazz .getDeclaredConstructor ().newInstance ();
176
+ } catch (Exception e ) {
177
+ throw new RuntimeException (e );
178
+ }
179
+ PropertyDescriptor [] descriptors = BeanUtils .getPropertyDescriptors (clazz );
180
+ for (PropertyDescriptor descriptor : descriptors ) {
181
+ String name = descriptor .getName ();
182
+ Object value = filters .get (name );
183
+ if (value != null ) {
184
+ try {
185
+ descriptor .getWriteMethod ().invoke (entity , value );
186
+ } catch (Exception e ) {
156
187
}
157
188
}
158
- return (Example <T >) Example .of (entity );
159
- }catch (Exception e ){
160
- return null ;
161
189
}
190
+ return (Example <T >) Example .of (entity );
191
+
162
192
}
163
193
}
164
194
0 commit comments