标签:
1 import java.lang.annotation.ElementType; 2 import java.lang.annotation.Retention; 3 import java.lang.annotation.RetentionPolicy; 4 import java.lang.annotation.Target; 5 6 @Retention(RetentionPolicy.RUNTIME) 7 @Target(ElementType.METHOD) 8 public @interface ParamsValidate { 9 ParamValidate[] value(); 10 }
1 import java.lang.annotation.ElementType; 2 import java.lang.annotation.Retention; 3 import java.lang.annotation.RetentionPolicy; 4 import java.lang.annotation.Target; 5 6 @Retention(RetentionPolicy.RUNTIME) 7 @Target(ElementType.METHOD) 8 public @interface ParamValidate { 17 String name(); 27 String regex() default ""; 36 boolean notNull() default false; 45 String message() default ""; 54 int minLen() default -1; 63 int maxLen() default -1; 72 int maxVal() default -1 ; 81 int minVal() default -1 ; 90 String dateFormat() default ""; 91 }
import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import cn.edsport.base.domain.model.pub.ServiceException; import cn.edsport.base.utils.StringUtil; public class ParamValidateMethods { /** * @author JinXue 2015年7月3日 下午9:44:52 * @Method: getAnnotationByMethod * @Description: TODO 根据目标方法和注解类型 得到该目标方法的指定注解 * @param method * @param annoClass * @return * @throws */ public static Annotation getAnnotationByMethod(Method method, Class annoClass) { Annotation all[] = method.getAnnotations(); for (Annotation annotation : all) { if (annotation.annotationType() == annoClass) { return annotation; } } return null; } /** * @author JinXue 2015年7月3日 下午9:45:04 * @Method: getMethodByClassAndName * @Description: TODO 根据类和方法名得到方法 * @param c * @param methodName * @return * @throws */ public static Method getMethodByClassAndName(Class c, String methodName) { Method[] methods = c.getDeclaredMethods(); for (Method method : methods) { if (method.getName().equals(methodName)) { return method; } } return null; } /** * @author JinXue 2015年7月3日 下午9:45:50 * @Method: getGetterNameByFiledName * @Description: TODO 得到该属性的getter方法名 * @param fieldName * @return * @throws */ public static String getGetterNameByFiledName(String fieldName) { return "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); } /** * @author JinXue 2015年7月3日 下午9:46:32 * @Method: getFieldByObjectAndFileName * @Description: TODO 根据对象和属性名得到 属性 * @param targetObj * @param fileName * @return * @throws SecurityException * @throws NoSuchMethodException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InvocationTargetException * @throws */ public static Object getFieldByObjectAndFileName(Object targetObj, String fileName) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { String tmp[] = fileName.split("\\."); Object arg = targetObj; for (int i = 0; i < tmp.length; i++) { Method methdo = arg.getClass().getMethod(getGetterNameByFiledName(tmp[i])); arg = methdo.invoke(arg); } return arg; } /** * @author JinXue 2015年7月3日 下午10:25:05 * @Method: validateFiled * @Description: TODO 验证字段的正确性 * @param valiedatefiles * @param request * @return * @throws SecurityException * @throws IllegalArgumentException * @throws NoSuchMethodException * @throws IllegalAccessException * @throws InvocationTargetException * @throws ServiceException * @throws */ public static boolean validateFiled(ParamValidate[] valiedatefiles, HttpServletRequest request) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, ServiceException { Map<String, Object> errorMaps = new HashMap<String, Object>(); boolean flag = true; for (ParamValidate validateFiled : valiedatefiles) { Object arg = null; if (!"".equals(validateFiled.name())) { arg = request.getParameter(validateFiled.name()); } else { flag = false; errorMaps.put(validateFiled.name(), "需验证的字段不能为空"); } if (validateFiled.notNull()) { // 判断参数是否为空 if (arg == null || "".equals(arg.toString())) { flag = false; errorMaps.put(validateFiled.name(), validateFiled.message()); } } else { // 如果该参数能够为空,并且当参数为空时,就不用判断后面的了 ,直接返回true if (arg == null) return true; } if (arg instanceof String) { if (!StringUtil.isEmptyOrNull(arg.toString()) && validateFiled.maxLen() > 0) { // 判断字符串最大长度 if (((String)arg).length() > validateFiled.maxLen()) { flag = false; errorMaps.put(validateFiled.name(), validateFiled.message()); } } if (!StringUtil.isEmptyOrNull(arg.toString()) && validateFiled.minLen() > 0) { // 判断字符串最小长度 if (((String)arg).length() < validateFiled.minLen()) { flag = false; errorMaps.put(validateFiled.name(), validateFiled.message()); } } if (!StringUtil.isEmptyOrNull(arg.toString()) && !"".equals(validateFiled.regex())) { // 判断正则 if (arg instanceof String) { if (!((String)arg).matches(validateFiled.regex())) { flag = false; errorMaps.put(validateFiled.name(), validateFiled.message()); } } else { flag = false; errorMaps.put(validateFiled.name(), validateFiled.message()); } } if (!StringUtil.isEmptyOrNull(arg.toString()) && !"".equals(validateFiled.dateFormat())) { SimpleDateFormat sdf = new SimpleDateFormat(); try { sdf.parse(validateFiled.dateFormat()); } catch (ParseException e) { flag = false; errorMaps.put(validateFiled.name(), validateFiled.message()); } } } if (arg != null && validateFiled.maxVal() != -1) { // 判断数值最大值 if (Integer.parseInt(arg.toString()) > validateFiled.maxVal()) { flag = false; errorMaps.put(validateFiled.name(), validateFiled.message()); } } if (arg != null && validateFiled.minVal() != -1) { // 判断数值最小值 if (Integer.parseInt(arg.toString()) < validateFiled.minVal()) { flag = false; errorMaps.put(validateFiled.name(), validateFiled.message()); } } } if (!flag) { throw new ServiceException(1, errorMaps); } return flag; } }
@Controller @RequestMapping(value=ControllerMappings.GET_MY_CLUB_LIST) public class ClubInfoGetMyClubInfoController { private Logger logger=Logger.getLogger(cn.edsport.app.web.controller.club.ClubInfoClubSetController.class); @ParamsValidate(value={ @ParamValidate(name="qryUserId",notNull=true,message="不能为空") }) @ResponseBody @RequestMapping(params="protocolVer=1.0") public Message getVersion_1_0(HttpServletRequest request,HttpServletResponse response,Long qryUserId)throws Exception{
return null; } }
标签:
原文地址:http://www.cnblogs.com/jinxue0302/p/4653912.html