66import org .springframework .data .domain .Example ;
77import org .springframework .data .domain .Pageable ;
88import org .springframework .data .domain .Sort ;
9+ import org .springframework .util .StringUtils ;
910import org .springframework .web .context .request .RequestContextHolder ;
1011import org .springframework .web .context .request .ServletRequestAttributes ;
1112
1213import java .beans .PropertyDescriptor ;
14+ import java .util .Enumeration ;
1315import java .util .HashMap ;
1416import java .util .Map ;
1517import java .util .Optional ;
@@ -20,7 +22,7 @@ public class PageRequest extends org.springframework.data.domain.PageRequest {
2022 private int current ;
2123 private int pageSize ;
2224
23- private final Map <String ,Object > filters = new HashMap <>();
25+ private final Map <String , Object > filters = new HashMap <>();
2426
2527 @ Getter
2628 private final HttpServletRequest servletRequest ;
@@ -35,6 +37,19 @@ public PageRequest(int current, int pageSize, Sort sort) {
3537
3638 ServletRequestAttributes attributes = (ServletRequestAttributes ) RequestContextHolder .currentRequestAttributes ();
3739 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+ }
3853 }
3954
4055 public PageRequest () {
@@ -45,15 +60,25 @@ public void setCurrent(int current) {
4560 this .current = current > 0 ? current - 1 : 0 ;
4661 }
4762
48- public String getParameter (String key ){
63+ public String getParameter (String key ) {
4964 return servletRequest .getParameter (key );
5065 }
5166
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 );
5469 return result == null ? defaultValue : result ;
5570 }
5671
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+
5782 @ Override
5883 public int getPageSize () {
5984 return pageSize ;
@@ -127,38 +152,43 @@ public void addSort(Sort sort) {
127152 Sort nowSort = pageRequest .getSort ();
128153 if (nowSort == Sort .unsorted ()) {
129154 this .pageRequest = new PageRequest (getCurrent (), getPageSize (), sort );
130- }else {
155+ } else {
131156 pageRequest .getSort ().and (sort );
132157 }
133158 }
134159
135- public PageRequest addFilter (String key ,Object value ){
160+ public PageRequest addFilter (String key , Object value ) {
136161 this .filters .put (key , value );
137162 return this ;
138163 }
139164
140- public boolean hasFilter (){
165+ public boolean hasFilter () {
141166 return !this .filters .isEmpty ();
142167 }
143168
144- public <T > Example <T > getExample (Class <T > clazz ){
145- if (!hasFilter ()){
169+ public <T > Example <T > getExample (Class <T > clazz ) {
170+ if (!hasFilter ()) {
146171 return null ;
147172 }
173+ Object entity = null ;
148174 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 ) {
156187 }
157188 }
158- return (Example <T >) Example .of (entity );
159- }catch (Exception e ){
160- return null ;
161189 }
190+ return (Example <T >) Example .of (entity );
191+
162192 }
163193}
164194
0 commit comments