background. using spring dependency injection , struts2 running on tomcat server.
when doing bean validation take 2 layer approach.
- run validation on client side (for using jquery validation plugin) before form submitted server check formed/complete forms.
- run bean validation @ server using annotations on beans them-self stop database blowups in event circumvents client side validation.
the problem:
keeping track of 2 sets of validation criteria gets bit of pain... error message easy keep track of because can stick them messages.properties file , message lookup both bean , jsp page. real problem numbers.
for example, let have checklist string variable notes. min size 10 max size 2000 @ release. 3 months down road see 2000 not cutting , need increase size limit 4000 max. have 2 places go , change number.
java bean validation annotation:
@entity public class checklist{ @size(min = 10, max = 2000, message = "{checklist.size}") private string notes; } jquery form validation:
$("#someform").validate({ rules:{ 'checklist.notes':{ minlength:10, maxlength:2000 } } }); is there way can reuse number in same way can reuse message?
use constant generic purpose class or interface. inject jsp jquery script struts <s:property/> tag.
this way, when change constant value, new value automatically read java classes , jsp pages no need alter code or replace something.
base class, interface, or whatever containing constants
public static final int min_lenght = 20; public static final int max_lenght = 4000; action (that implements / extends class above constants)
@entity public class checklist{ @size(min = min_lenght, max = max_lenght, message = "{checklist.size}") private string notes; } jsp
$("#someform").validate({ rules:{ 'checklist.notes':{ minlength:<s:property value="@my.package.constantclass@min_lenght" />, maxlength:<s:property value="@my.package.constantclass@max_lenght" /> } } }); remember set struts.ognl.allowstaticmethodaccess true in struts.xml
Comments
Post a Comment