1
0
Quellcode durchsuchen

枚举参数绑定

xiatian vor 17 Stunden
Ursprung
Commit
20c80e38b4

+ 292 - 273
stmms-web/src/main/java/cn/com/qmth/stmms/common/controller/BaseController.java

@@ -1,5 +1,15 @@
 package cn.com.qmth.stmms.common.controller;
 
+import java.beans.PropertyEditorSupport;
+import java.util.Date;
+import java.util.List;
+
+import org.apache.commons.lang3.StringEscapeUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.propertyeditors.CustomBooleanEditor;
+import org.springframework.web.bind.WebDataBinder;
+import org.springframework.web.bind.annotation.InitBinder;
+
 import cn.com.qmth.stmms.biz.exam.dao.SelectiveStudentDao;
 import cn.com.qmth.stmms.biz.exam.model.ExamStudent;
 import cn.com.qmth.stmms.biz.exam.model.MarkGroup;
@@ -7,23 +17,13 @@ import cn.com.qmth.stmms.biz.exam.service.ExamQuestionService;
 import cn.com.qmth.stmms.biz.exam.service.ExamStudentService;
 import cn.com.qmth.stmms.biz.exam.service.ExamSubjectService;
 import cn.com.qmth.stmms.biz.exam.service.MarkGroupService;
-import cn.com.qmth.stmms.biz.file.enums.FileType;
-import cn.com.qmth.stmms.biz.file.enums.FormatType;
 import cn.com.qmth.stmms.biz.user.model.User;
 import cn.com.qmth.stmms.biz.user.service.UserService;
 import cn.com.qmth.stmms.biz.utils.ScoreCalculateUtil;
 import cn.com.qmth.stmms.biz.utils.ScoreInfo;
-import cn.com.qmth.stmms.common.enums.*;
+import cn.com.qmth.stmms.common.enums.MarkStatus;
+import cn.com.qmth.stmms.common.enums.SubjectiveStatus;
 import cn.com.qmth.stmms.common.utils.DateUtils;
-import org.apache.commons.lang3.StringEscapeUtils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.propertyeditors.CustomBooleanEditor;
-import org.springframework.web.bind.WebDataBinder;
-import org.springframework.web.bind.annotation.InitBinder;
-
-import java.beans.PropertyEditorSupport;
-import java.util.Date;
-import java.util.List;
 
 public class BaseController {
 
@@ -81,267 +81,286 @@ public class BaseController {
                 setValue(DateUtils.parseDate(text));
             }
         });
-        // ExamSubjectStatus 类型转换
-        binder.registerCustomEditor(ExamSubjectStatus.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(ExamSubjectStatus.findByValue(Integer.valueOf(text)));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // LibraryStatus 类型转换
-        binder.registerCustomEditor(LibraryStatus.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(LibraryStatus.valueOf(text));
-//                    setValue(LibraryStatus.findByValue(Integer.valueOf(text)));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // Role 类型转换
-        binder.registerCustomEditor(Role.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(Role.valueOf(text));
-//                    setValue(Role.findByValue(Integer.valueOf(text)));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // UserSource 类型转换
-        binder.registerCustomEditor(UserSource.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(UserSource.valueOf(text));
-//                    setValue(UserSource.findByValue(Integer.valueOf(text)));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // MarkStatus 类型转换
-        binder.registerCustomEditor(MarkStatus.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(MarkStatus.valueOf(text));
-//                    setValue(MarkStatus.findByName(text));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // MarkMode 类型转换
-        binder.registerCustomEditor(MarkMode.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(MarkMode.valueOf(text));
-//                    setValue(MarkMode.findByName(text));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-
-        // HistoryStatus 类型转换
-        binder.registerCustomEditor(HistoryStatus.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(HistoryStatus.valueOf(text));
-//                    setValue(HistoryStatus.findByValue(Integer.valueOf(text)));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-
-        // CheckType 类型转换
-        binder.registerCustomEditor(CheckType.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(CheckType.valueOf(text));
-//                    setValue(CheckType.findByValue(Integer.valueOf(text)));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // ObjectivePolicy 类型转换
-        binder.registerCustomEditor(ObjectivePolicy.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(ObjectivePolicy.valueOf(text));
-//                    setValue(ObjectivePolicy.findByValue(Integer.valueOf(text)));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // ExamStatus 类型转换
-        binder.registerCustomEditor(ExamStatus.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(ExamStatus.valueOf(text));
-//                    setValue(ExamStatus.findByValue(Integer.valueOf(text)));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // ExamType 类型转换
-        binder.registerCustomEditor(ExamType.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(ExamType.valueOf(text));
-//                    setValue(ExamType.findByValue(Integer.valueOf(text)));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // LogType 类型转换
-        binder.registerCustomEditor(LogType.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(LogType.valueOf(text));
-//                    setValue(LogType.findByValue(Integer.valueOf(text)));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // FileType 类型转换
-        binder.registerCustomEditor(FileType.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(FileType.valueOf(text));
-//                    setValue(FileType.findByText(text));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // FormatType 类型转换
-        binder.registerCustomEditor(FormatType.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(FormatType.valueOf(text));
-//                    setValue(FormatType.findByText(text));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // ExamSource 类型转换
-        binder.registerCustomEditor(ExamSource.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(ExamSource.valueOf(text));
-//                    setValue(ExamSource.findByValue(Integer.valueOf(text)));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // SubjectiveStatus 类型转换
-        binder.registerCustomEditor(SubjectiveStatus.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(SubjectiveStatus.valueOf(text));
-//                    setValue(SubjectiveStatus.findByText(text));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // SelectiveStatus 类型转换
-        binder.registerCustomEditor(SelectiveStatus.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(SelectiveStatus.valueOf(text));
-//                    setValue(SelectiveStatus.findByValue(Integer.valueOf(text)));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // SystemAuthType 类型转换
-        binder.registerCustomEditor(SystemAuthType.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(SystemAuthType.valueOf(text));
-//                    setValue(SystemAuthType.findByValue(Integer.valueOf(text)));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // QuestionType 类型转换
-        binder.registerCustomEditor(QuestionType.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(QuestionType.valueOf(text));
-//                    setValue(QuestionType.findByValue(Integer.valueOf(text)));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
-        // TrackCountPolicy 类型转换
-        binder.registerCustomEditor(TrackCountPolicy.class, new PropertyEditorSupport() {
-
-            @Override
-            public void setAsText(String text) {
-                try {
-                    setValue(TrackCountPolicy.valueOf(text));
-//                    setValue(TrackCountPolicy.findByValue(Integer.valueOf(text)));
-                } catch (Exception e) {
-                    setValue(null);
-                }
-            }
-        });
+        // // ExamSubjectStatus 类型转换
+        // binder.registerCustomEditor(ExamSubjectStatus.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(ExamSubjectStatus.findByValue(Integer.valueOf(text)));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // LibraryStatus 类型转换
+        // binder.registerCustomEditor(LibraryStatus.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(LibraryStatus.valueOf(text));
+        //// setValue(LibraryStatus.findByValue(Integer.valueOf(text)));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // Role 类型转换
+        // binder.registerCustomEditor(Role.class, new PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(Role.valueOf(text));
+        //// setValue(Role.findByValue(Integer.valueOf(text)));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // UserSource 类型转换
+        // binder.registerCustomEditor(UserSource.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(UserSource.valueOf(text));
+        //// setValue(UserSource.findByValue(Integer.valueOf(text)));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // MarkStatus 类型转换
+        // binder.registerCustomEditor(MarkStatus.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(MarkStatus.valueOf(text));
+        //// setValue(MarkStatus.findByName(text));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // MarkMode 类型转换
+        // binder.registerCustomEditor(MarkMode.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(MarkMode.valueOf(text));
+        //// setValue(MarkMode.findByName(text));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        //
+        // // HistoryStatus 类型转换
+        // binder.registerCustomEditor(HistoryStatus.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(HistoryStatus.valueOf(text));
+        //// setValue(HistoryStatus.findByValue(Integer.valueOf(text)));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        //
+        // // CheckType 类型转换
+        // binder.registerCustomEditor(CheckType.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(CheckType.valueOf(text));
+        //// setValue(CheckType.findByValue(Integer.valueOf(text)));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // ObjectivePolicy 类型转换
+        // binder.registerCustomEditor(ObjectivePolicy.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(ObjectivePolicy.valueOf(text));
+        //// setValue(ObjectivePolicy.findByValue(Integer.valueOf(text)));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // ExamStatus 类型转换
+        // binder.registerCustomEditor(ExamStatus.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(ExamStatus.valueOf(text));
+        //// setValue(ExamStatus.findByValue(Integer.valueOf(text)));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // ExamType 类型转换
+        // binder.registerCustomEditor(ExamType.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(ExamType.valueOf(text));
+        //// setValue(ExamType.findByValue(Integer.valueOf(text)));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // LogType 类型转换
+        // binder.registerCustomEditor(LogType.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(LogType.valueOf(text));
+        //// setValue(LogType.findByValue(Integer.valueOf(text)));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // FileType 类型转换
+        // binder.registerCustomEditor(FileType.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(FileType.valueOf(text));
+        //// setValue(FileType.findByText(text));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // FormatType 类型转换
+        // binder.registerCustomEditor(FormatType.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(FormatType.valueOf(text));
+        //// setValue(FormatType.findByText(text));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // ExamSource 类型转换
+        // binder.registerCustomEditor(ExamSource.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(ExamSource.valueOf(text));
+        //// setValue(ExamSource.findByValue(Integer.valueOf(text)));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // SubjectiveStatus 类型转换
+        // binder.registerCustomEditor(SubjectiveStatus.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(SubjectiveStatus.valueOf(text));
+        //// setValue(SubjectiveStatus.findByText(text));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // SelectiveStatus 类型转换
+        // binder.registerCustomEditor(SelectiveStatus.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(SelectiveStatus.valueOf(text));
+        //// setValue(SelectiveStatus.findByValue(Integer.valueOf(text)));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // SystemAuthType 类型转换
+        // binder.registerCustomEditor(SystemAuthType.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(SystemAuthType.valueOf(text));
+        //// setValue(SystemAuthType.findByValue(Integer.valueOf(text)));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // QuestionType 类型转换
+        // binder.registerCustomEditor(QuestionType.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(QuestionType.valueOf(text));
+        //// setValue(QuestionType.findByValue(Integer.valueOf(text)));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
+        // // TrackCountPolicy 类型转换
+        // binder.registerCustomEditor(TrackCountPolicy.class, new
+        // PropertyEditorSupport() {
+        //
+        // @Override
+        // public void setAsText(String text) {
+        // try {
+        // setValue(TrackCountPolicy.valueOf(text));
+        //// setValue(TrackCountPolicy.findByValue(Integer.valueOf(text)));
+        // } catch (Exception e) {
+        // setValue(null);
+        // }
+        // }
+        // });
         binder.registerCustomEditor(Boolean.class, new CustomBooleanEditor(true));
     }
 

+ 1 - 1
stmms-web/src/main/java/cn/com/qmth/stmms/common/support/CustomExceptionHandler.java

@@ -37,7 +37,7 @@ public class CustomExceptionHandler {
 
         if (e instanceof StatusException) {
             StatusException se = (StatusException) e;
-            body = new StatusResponse(se.getCode(), se.getMessage());
+            body = new StatusResponse(se.getCode() == null ? 500 : se.getCode(), se.getMessage());
         } else if (e instanceof ApiException) {
             response.addHeader(ERROR_MESSAGE_HEADER_KEY, StringUtils.trimToEmpty(e.getMessage()));
             ApiException teme = (ApiException) e;

+ 27 - 0
stmms-web/src/main/java/cn/com/qmth/stmms/common/support/ValuedEnumConverter.java

@@ -0,0 +1,27 @@
+package cn.com.qmth.stmms.common.support;
+
+import org.springframework.core.convert.converter.Converter;
+import org.springframework.core.convert.converter.ConverterFactory;
+
+@SuppressWarnings("rawtypes")
+public class ValuedEnumConverter implements ConverterFactory<String, Enum> {
+
+    @Override
+    public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
+        return new StringToEnum<>(targetType);
+    }
+
+    private class StringToEnum<T extends Enum<T>> implements Converter<String, T> {
+
+        private Class<T> targetType;
+
+        public StringToEnum(Class<T> targetType) {
+            this.targetType = targetType;
+        }
+
+        @Override
+        public T convert(String source) {
+            return (T) Enum.valueOf(targetType, source);
+        }
+    }
+}

+ 8 - 3
stmms-web/src/main/webapp/WEB-INF/spring-mvc.xml

@@ -33,8 +33,6 @@
         <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
     </context:component-scan>
 
-    <mvc:annotation-driven/>
-
     <mvc:interceptors>
         <mvc:interceptor>
        		<mvc:mapping path="/logout"/>
@@ -112,7 +110,14 @@
             </list>
         </property>
     </bean> -->
-
+<mvc:annotation-driven conversion-service="conversionService"/>
+ <bean class="org.springframework.context.support.ConversionServiceFactoryBean" id="conversionService">
+        <property name="converters">
+            <list>
+                <bean class="cn.com.qmth.stmms.common.support.ValuedEnumConverter"/>
+            </list>
+        </property>
+    </bean>
     <!-- 视图文件解析配置 -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/views/"/>