/**
* 当前页码
*/
private int pageNum = 1;
/**
* 每页条数
*/
private int pageSize = 10;
/**
* @BelongsProject: spring-boot-self-exercise
* @BelongsPackage: com.geekmice.springbootselfexercise.annotation
* @Author: pingmingbo
* @CreateTime: 2023-08-25 17:04
* @Description: 分页参数默认值
* @Version: 1.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface IntDefaultValue {
}
package com.geekmice.springbootselfexercise.aspect;
import com.geekmice.springbootselfexercise.param.TzAreaParam;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
/**
* @BelongsProject: spring-boot-self-exercise
* @BelongsPackage: com.geekmice.springbootselfexercise.aspect
* @Author: pingmingbo
* @CreateTime: 2023-08-25 17:05
* @Description: 默认值切面
* @Version: 1.0
*/
@Component
@Aspect
@Slf4j
public class IntDefaultValueAspect {
/**
* 分页参数默认值
* @param point
* @return
* @throws Throwable
*/
@Around("@annotation(com.geekmice.springbootselfexercise.annotation.IntDefaultValue)")
public Object setIntValue(ProceedingJoinPoint point) throws Throwable {
Object[] parameters = point.getArgs();
for (Object arg : parameters) {
if (arg instanceof TzAreaParam) {
TzAreaParam temp = (TzAreaParam) arg;
if (0 == temp.getPageNum() || 0 == temp.getPageSize()) {
temp.setPageNum(1);
temp.setPageSize(10);
}
}
}
Object proceed = point.proceed();
return proceed;
}
}
/**
* 分页查询
*
*/
@GetMapping("listPage")
@MethodExporter
@ApiOperation(value = "分页查询")
@IntDefaultValue
public AjaxResult listPage(@Valid TzAreaParam param) {
log.info("分页查询 param : [{}]" , param);
PageInfo<TzArea> result = tzAreaService.listPage(param);
return AjaxResult.success(result);
}
使用前
2023-09-05 09:19:03.654 INFO 39792 — [nio-8081-exec-4] c.g.s.controller.TzAreaController : 分页查询 param : [TzAreaParam(areaId=null, areaName=北京市, parentId=null, level=null, pageNum=1, pageSize=1, count=0)]
使用后
2023-09-05 09:19:26.558 INFO 39792 — [nio-8081-exec-1] c.g.s.controller.TzAreaController : 分页查询 param : [TzAreaParam(areaId=null, areaName=北京市, parentId=null, level=null, pageNum=1, pageSize=10, count=0)]
注意:启动类加上注解@ServletComponentScan,使用过滤器,监听器,拦截器都可以交给spring容器管理。
package com.geekmice.springbootselfexercise.param;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
/**
* @BelongsProject: spring-boot-self-exercise
* @BelongsPackage: com.geekmice.springbootselfexercise.param
* @Author: pingmingbo
* @CreateTime: 2023-09-05 09:41
* @Description: TODO
* @Version: 1.0
*/
public class MyRequestWrapper extends HttpServletRequestWrapper {
private Map params = new HashMap<>();
public MyRequestWrapper(HttpServletRequest request, Map newParams) {
super(request);
if (request.getParameterMap() != null) {
this.params.putAll(request.getParameterMap());
}
if (newParams != null) {
this.params.putAll(newParams);
}
}
//主要覆盖这个方法来获取新的参数对象
@Override
public Map getParameterMap() {
return params;
}
@Override
public String[] getParameterValues(String name) {
Object v = params.get(name);
if (v == null) {
return null;
} else if (v instanceof String[]) {
return (String[]) v;
} else if (v instanceof String) {
return new String[]{(String) v};
} else {
return new String[]{v.toString()};
}
}
/**
* 根据参数的key获取参数
*
* @param name
* @return
*/
@Override
public String getParameter(String name) {
Object v = params.get(name);
if (v == null) {
return null;
} else if (v instanceof String[]) {
String[] strArr = (String[]) v;
if (strArr.length > 0) {
return strArr[0];
} else {
return null;
}
} else if (v instanceof String) {
return (String) v;
} else {
return v.toString();
}
}
public Enumeration getParameterNames() {
Vector l = new Vector(params.keySet());
return l.elements();
}
}
package com.geekmice.springbootselfexercise.filter;
import com.geekmice.springbootselfexercise.param.MyRequestWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @BelongsProject: spring-boot-self-exercise
* @BelongsPackage: com.geekmice.springbootselfexercise.filter
* @Author: pingmingbo
* @CreateTime: 2023-09-05 09:27
* @Description: 修改分页参数默认值
* @Version: 1.0
*/
@Configuration
@Slf4j
@WebFilter(filterName = "authFilter", urlPatterns = {"/tzArea"})
public class InitValueFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("过滤器开始初始化 : [{}]" , filterConfig);
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
String pageSize = httpServletRequest.getParameter("pageSize");
String pageNum = httpServletRequest.getParameter("pageNum");
if(StringUtils.isAnyBlank(pageSize,pageNum)){
pageSize="11";
pageNum="1";
Map<String,Object> map = new HashMap(16);
map.put("pageSize", pageSize);
map.put("pageNum", pageNum);
MyRequestWrapper myRequestWrapper = new MyRequestWrapper(httpServletRequest, map);
chain.doFilter(myRequestWrapper, response);
}
}
@Override
public void destroy() {
log.info("过滤器销毁");
}
}
效果
2023-09-05 10:08:51.174 INFO 14824 — [nio-8081-exec-9] c.g.s.controller.TzAreaController : 分页查询 param : [TzAreaParam(areaId=null, areaName=1, parentId=null, level=null, pageNum=1, pageSize=11, count=0)]