因为本人美工不行,假如要做出界面来的话不知何年马月才有成果,所以,我决定,
采用类似 mvc(asp.net上面的mvc我用过asp.net mvc及monorail,所以参考他们的模型来)模型来进行定义,然后根据这个模型来生成前台输入页面,前台逻辑验证,后台验证及赋值。
我们先研究一下应该如何描述这个过程里面用到的模型或者数据。请看下图:
很明显一个表单包含有多个参数,每个参数可以对应1到n个验证规则,每个参数也各自拥有自己的显示形式与数据类型,所以我们可以根据这些来编写一些基础数据类。
为了可以描述一个表单数据,我写了这些看上去无意义的注解了。注解类本身就是用于解释的,最重要的地方是如何将注解的信息读取出来,放到其他地方,写模板时如何调用这些信息。
【下面是一个表单模型,这次的目标就是要将它演变成为前台显示、验证,后台验证,赋值的代码】
package CodeGen.customModel.samples; import java.util.Date; import CodeGen.customModel.Annotations.*; @FormDetail(prefix = "eform.") public class jSampleForm { @Display(name = "主键",display = HtmlUI.InputHiddenField) @Validate_isInteger(ErrorMessage = "主键必须为数字") @Validate_NumberRange(ErrorMessage = "必须大于3",max = "3") public int id; @Display(name="用户姓名",display =HtmlUI.InputNormalText) @Validate_isRequired(ErrorMessage = "必须填写用户帐号") @Validate_CustomRegex(Value = "[a-zA-Z0-9_]+",ErrorMessage = "用户名称只能包含英文数字及下划线!") @Validate_StringRange(minLen = "6",maxLen = "15",ErrorMessage = "用户帐号长度为6到15位!") public String userName; @Display(name="年龄",display = HtmlUI.InputNormalText) @Validate_NumberRange(min="16",max="99",ErrorMessage = "年龄段必须大于16和小于99!") public int age; @Display(name="是否男性",display = HtmlUI.Radio_Boolean) @Validate_isRequired(ErrorMessage = "必须选择性别!") public boolean isMale; @Display(name="是否是学生",display = HtmlUI.Checkbox_Boolean) public boolean isStudent; @Display(name="余额",display = HtmlUI.InputNormalText) public float balance; @Display(name="生日",display = HtmlUI.InputDateTimePicker) @Validate_isDateTime(ErrorMessage = "生日格式错误!") public Date birthday; @Display(name="喜爱颜色",display = HtmlUI.InputColorPicker) @Validate_isColor(ErrorMessage = "喜爱颜色的填写格式错误!") @Validate_isRequired(ErrorMessage = "必须填写喜爱的颜色!") public String favoriteColor; @Display(name="邮箱地址",display = HtmlUI.InputEmailText) @Validate_isEmailAddr(ErrorMessage = "邮箱地址格式错误!") @Validate_isRequired(ErrorMessage = "必须填写邮箱地址。") public String email; @Display(name="手机号码",display = HtmlUI.InputNormalText) @Validate_isPhoneNumber(ErrorMessage = "手机号码格式错误!") @Validate_isRequired(ErrorMessage = "必须填写手机号码!") public String mobile; @Display(name="个人首页",display = HtmlUI.InputNormalText) @Validate_isRequired(ErrorMessage = "必须填写个人首页地址") @Validate_isURL(ErrorMessage = "个人首页的格式错误!") public String myHomePage; @Display(name="我的附件",display = HtmlUI.InputFileURL) @Validate_isRequired(ErrorMessage = "必须选择或填写附件地址") @Validate_isURL(ErrorMessage = "附件地址格式错误!") public String attachFileURL; @Display(name="我的头像",display = HtmlUI.InputImgURL) @Validate_isRequired(ErrorMessage = "必须上传或填写头像图片!") @Validate_isURL(ErrorMessage = "头像地址格式错误!") public String headPhoto; @Display(name="个人简介",display = HtmlUI.InputRichEditor) public String description; }
package CodeGen.customModel.Annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Display { String name() default ""; HtmlUI display() default HtmlUI.InputNormalText; }
package CodeGen.customModel.Annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Target; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface FormDetail { String prefix() default "model."; String formID() default "form1"; String formName() default "myform"; String formName_ZH() default "默认表单"; }
package CodeGen.customModel.Annotations; public enum HtmlUI { InputNormalText, InputEmailText, InputHiddenField, InputImgURL, InputFileURL, InputRichEditor, InputDateTimePicker, InputColorPicker, Checkbox_Boolean, Radio_Boolean }
package CodeGen.customModel.Annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Validate_CustomRegex { String Value(); String ErrorMessage() default ""; }
package CodeGen.customModel.Annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Validate_isBoolean { String ErrorMessage() default ""; }
package CodeGen.customModel.Annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Validate_isColor { String ErrorMessage() default ""; }
package CodeGen.customModel.Annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Validate_isDateTime { String ErrorMessage() default ""; }
package CodeGen.customModel.Annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Validate_isEmailAddr { String ErrorMessage() default ""; }
package CodeGen.customModel.Annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Validate_isInteger { String ErrorMessage() default ""; }
package CodeGen.customModel.Annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Validate_isNumberic { String ErrorMessage() default ""; }
package CodeGen.customModel.Annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Validate_isPhoneNumber { String ErrorMessage() default ""; }
package CodeGen.customModel.Annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Validate_isRequired { String ErrorMessage() default ""; }
package CodeGen.customModel.Annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Validate_isURL { String ErrorMessage() default ""; }
package CodeGen.customModel.Annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Validate_NumberRange { String min() default ""; String max() default ""; String ErrorMessage() default ""; }
package CodeGen.customModel.Annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Validate_StringRange { String maxLen() default ""; String minLen() default ""; String ErrorMessage() default ""; }
package CodeGen.customModel.util; public class DisplayType { public final String InputNormalText="InputNormalText", InputEmailText="InputEmailText", InputHiddenField="InputHiddenField", InputImgURL="InputImgURL", InputFileURL="InputFileURL", InputRichEditor="InputRichEditor", InputDateTimePicker="InputDateTimePicker", InputColorPicker="InputColorPicker", Checkbox_Boolean="Checkbox_Boolean", Radio_Boolean="Radio_Boolean"; public String getInputNormalText(){ return InputNormalText; } public String getInputHiddenField(){ return InputHiddenField; } public String getInputEmailText(){ return InputEmailText; } public String getInputImgURL(){ return InputImgURL; } public String getInputFileURL(){ return InputFileURL; } public String getInputRichEditor(){ return InputRichEditor; } public String getInputDateTimePicker(){ return InputDateTimePicker; } public String getInputColorPicker(){ return InputColorPicker; } public String getCheckbox_Boolean(){ return Checkbox_Boolean; } public String getRadio_Boolean(){ return Radio_Boolean; } }
package CodeGen.customModel.util; import java.util.ArrayList; public class FormInfo { public ArrayList<FormParameter> paralist=new ArrayList<FormParameter>(); public String prefix="model."; public String formID="form1"; public String formName="myform"; public String formName_ZH="默认表单"; public String getChineseName(){ return formName_ZH; } public String getEnglishName(){ return formName; } public String getFormID(){ return formID; } public String getPrefix(){ return prefix; } public ArrayList<FormParameter> getParameterList(){ return paralist; } }
package CodeGen.customModel.util; import java.util.ArrayList; import java.util.Hashtable; import java.util.LinkedHashMap; public class FormParameter { public String parameterName; public String parameterType; public String displayType=""; public String name_ZH=""; public LinkedHashMap<String,ValidateInput> validatelist=new LinkedHashMap<String, ValidateInput>(); public String getParameterName(){ return parameterName; } public String getParameterType(){ return parameterType; } public String getDisplayType(){ return displayType; } public String getName_ZH(){ return name_ZH; } public LinkedHashMap<String,ValidateInput> getValidatelist(){ return validatelist; } }
package CodeGen.customModel.util; public class JavaDataType { public final String Type_Integer="integer"; public final String Type_Boolean="boolean"; public final String Type_Float="float"; public final String Type_Double="double"; public final String Type_String="String"; public final String Type_Date="Date"; public final String Type_Long="long"; public final String Type_Object="Object"; public String getType_Integer(){ return Type_Integer; } public String getType_Boolean(){ return Type_Boolean; } public String getType_Float(){ return Type_Float; } public String getType_Double(){ return Type_Double; } public String getType_String(){ return Type_String; } public String getType_Date(){ return Type_Date; } public String getType_Long(){ return Type_Long; } public String getType_Object(){ return Type_Object; } public String getDefaultValueStrByType(String Type_Str){ if(Type_Str==null){ return "new Object()"; } else if(Type_Str.equals(Type_Integer)){ return "0"; } else if(Type_Str.equals(Type_Boolean)){ return "false"; } else if(Type_Str.equals(Type_Float)){ return "0.0"; } else if(Type_Str.equals(Type_Double)){ return "0.0"; } else if(Type_Str.equals(Type_String)){ return "\"\""; } else if(Type_Str.equals(Type_Date)){ return "new Date()"; } else if(Type_Str.equals(Type_Long)){ return "0"; } else{ return "new Object()"; } } }
package CodeGen.customModel.util; import java.util.Hashtable; public class ValidateInput implements IValidateRule { private Hashtable<String,String> _paras=new Hashtable<String, String>(); ValidateRuleType vtype=new ValidateRuleType(); public String VRuleType=""; private String _ruleName=""; private String _errormsg=""; public String getRuleName(){ return _ruleName; } public void setRuleName(String _name){ _ruleName=_name; VRuleType=_name; } public String getValidateRuleType(){ return VRuleType; } public String get(String key){ return _paras.get(key); } public void set(String key,String value){ _paras.put(key,value); } public String getErrorMessage(){ return _errormsg; } public void setErrorMessage(String errorMessage){ _errormsg=errorMessage; } }
package CodeGen.customModel.util; public class ValidateRuleType { public final String rule_isInteger="rule_isInteger"; public final String rule_isNumberic="rule_isNumberic"; public final String rule_isColor="rule_isColor"; public final String rule_isBoolean="rule_isBoolean"; public final String rule_isDateTime="rule_isDateTime"; public final String rule_isEmailAddr="rule_isEmailAddr"; public final String rule_isRequired="rule_isRequired"; public final String rule_isURL="rule_isURL"; public final String rule_NumberRange="rule_NumberRange"; public final String rule_StringRange="rule_StringRange"; public final String rule_CustomRegex="rule_CustomRegex"; public final String rule_isPhoneNumber="rule_isPhoneNumber"; public String getSth(){ return ""; } public String getRule_isInteger(){ return rule_isInteger; } public String getRule_isNumberic(){ return rule_isNumberic; } public String getRule_isColor(){ return rule_isColor; } public String getRule_isBoolean(){ return rule_isBoolean; } public String getRule_isDateTime(){ return rule_isDateTime; } public String getRule_isEmailAddr(){ return rule_isEmailAddr; } public String getRule_isRequired(){ return rule_isRequired; } public String getRule_isURL(){ return rule_isURL; } public String getRule_NumberRange(){ return rule_NumberRange; } public String getRule_StringRange(){ return rule_StringRange; } public String getRule_CustomRegex(){ return rule_CustomRegex; } public String getRule_isPhoneNumber(){ return rule_isPhoneNumber; } }