code_junkie

How do i get a field value inside a custom Struts Validator, when the field's type is FormFile?

Nov 12th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. String value = ValidatorUtils.getValueAsString(bean,field.getProperty());
  2.  
  3. FormFile myFile = (FormFile) // TODO
  4.  
  5. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
  6. <struts-config>
  7. <form-beans>
  8. <form-bean name="ChangePictureForm" type="struts.administration.ChangePictureForm">
  9. <form-property name="id" type="java.lang.String" />
  10. <form-property name="file" type="org.apache.struts.upload.FormFile" />
  11. </form-bean>
  12. </form-beans>
  13. ...
  14. ..
  15. .
  16.  
  17. <!-- User Custom Validation Rules -->
  18. <validator name="size"
  19. classname="struts.StrutsValidator"
  20. method="validateSize"
  21. methodParams="java.lang.Object,
  22. org.apache.commons.validator.Validator,
  23. org.apache.commons.validator.ValidatorAction,
  24. org.apache.commons.validator.Field,
  25. org.apache.struts.action.ActionMessages,
  26. javax.servlet.http.HttpServletRequest"
  27. depends=""
  28. msg="errors.size"/>
  29.  
  30. package struts;
  31.  
  32. import javax.servlet.http.HttpServletRequest;
  33. import org.apache.commons.validator.Field;
  34. import org.apache.commons.validator.Validator;
  35. import org.apache.commons.validator.ValidatorAction;
  36. import org.apache.struts.action.ActionMessages;
  37. import org.apache.struts.upload.FormFile;
  38. import org.apache.struts.validator.Resources;
  39.  
  40. public class StrutsValidator {
  41. public static boolean validateSize (
  42. Object bean,
  43. Validator validator,
  44. ValidatorAction validatorAction,
  45. Field field,
  46. ActionMessages errors,
  47. HttpServletRequest request) {
  48.  
  49. FormFile myFile = null; //TODO (FormFile) get it from somewhere
  50.  
  51. String type = field.getVarValue("type");
  52. int size = myFile.getFileSize();
  53.  
  54. // MySQL Storage Requirements http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html
  55. int allowed_size = 65538; //Blob L + 2 bytes, where L < 2 ^ 16
  56. if(type.equalsIgnoreCase("tinyblob")) {
  57. allowed_size = 257; // L + 1 bytes, where L < 2 ^ 8
  58. } else if(type.equalsIgnoreCase("mediumblog")) {
  59. allowed_size = 16777219; // 16 Mb - L + 3 bytes, where L < 2 ^ 24
  60. } else if(type.equalsIgnoreCase("longblob")) {
  61. allowed_size = 16777219; // 4096 Mb - L + 4 bytes, where L < 2 ^ 32
  62. }
  63. try {
  64. if(size>=allowed_size) {
  65. errors.add(field.getKey(),Resources.getActionMessage(validator,request,validatorAction,field));
  66. return false;
  67. }
  68. } catch (Exception e) {
  69. errors.add(field.getKey(),Resources.getActionMessage(validator,request,validatorAction,field));
  70. return false;
  71. }
  72. return true;
  73. }
  74. }
  75.  
  76. <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">
  77. <form-validation>
  78. <formset>
  79. <form name="ChangePictureForm">
  80. <field property="file" depends="required,size">
  81. <arg0 key="file" />
  82. <arg1 name="type" key="${var:type}" resource="false" />
  83. <var>
  84. <var-name>type</var-name>
  85. <var-value>blob</var-value>
  86. </var>
  87. </field>
  88. </form>
  89. </formset>
  90. </form-validation>
Add Comment
Please, Sign In to add comment