Create java annotation to mark bean properties to clean up html

Is it possible to use Java custom annotation to add some code to a set or getter on a bean property to clear the property from bad html input by my users? I've searched for examples, but I haven't seen one that I think I can extend.

0


a source to share


1 answer


You can define a custom annotation to add a validator to your setter, but is there a reason you don't want to just inline validation into your bean without the annotation? The annotation mechanism can be difficult for others to understand if they ever need to work with your code.

I would do it this way: instead of having your property be a String, define your own HtmlString (assuming an equivalent class doesn't already exist in the standard library) that can only be created with valid HTML. Then, your bean property must be of this type. This will solve the validation problem in your component.



Define the validation methods on the HtmlString as per your requirement so that each HtmlString instance is valid HTML; then just define a toString method. This method will probably be much easier for others.

0


a source







All Articles