spring data binding from POST request to Model - java

I am trying to understand the concept of binding data from the POST to the model. I want to know whether we can have a control over how the binding is happening. I guess we can control the binding parameters using registering custom editors. But I want to have a finer control over the individual fields of the form. I mean when we register a custom editor, say for date, then all the instances of the, say date, will be treated the same. But may be I want to treat a particular date differently from the others. Moreover, I want to get the POST request parameters in raw string format and may be manipulate one or two fields and delegate the rest to the spring. I guess in the pre-annotation version of spring there was the provision of similar control for some method, may be processFormSubmission. So please let me know whether we can accomplish this in spring 3 annotation version.
Thanks

For the format of Date's you can use the #DateTimeFormat annotation. The annotation is specified on each field, so each field can have it's own format.
You can format and convert whatever fields you want using Formatter and ConversionService.
The documentation on all this can be found here.

Related

wicket form changes are not visible when setDefaultFormProcessing(true)

I have a form in wicket which has two buttons. I would like one to have validation over the fields (if they are left null or not) which I already did. Now I would like the second button NOT to have this validation. I have seen few examples where people use the method setDefaultFormProcessing() which is a method of class Button in wicket.
However when I use this method my form seems to also ignore changes done in the fields of the form.
Any idea how I actually can achive bypassing the validation but still be able to see changes in my form??
Thanks!!
This is the defined behavoir. If data is not valid it is not possible update model anyway. Consider an example when you have a date field and you type there '99/9/YYYYY' that is not a valid date, thus wicket has no chance just to bypass validation and update model. The only chance is to keep input data as String and give you an option, how to convert or update model by your own implementation.
If you have a reference to your form components, you could invoke updateModel().
See http://apache-wicket.1842946.n4.nabble.com/Turn-off-form-validation-td1877661.html
And check API doc for FormComponent that is a base class of all fields and other form components. https://ci.apache.org/projects/wicket/apidocs/7.x/org/apache/wicket/markup/html/form/FormComponent.html

Bean Validation: How to localize messages programmatically?

In my current rest-service scenario I need to be able to provide localized error messages to individual clients.
Think of a method like void validate(Locale locale) which is called on the parameter object when receiving some request.
The locale information can be squeezed out of the http headers.
How can I instruct/configure a validator to use a certain language when violation messages are getting interpolated?
As far as I know a validator chooses the used Locale by calling Locale.getDefault().
So far I couldn't find anything other than this article which does not exactly fit to my needs (a story too long to tell).
Thanks in advance!
According to the spec you have two possibilities:
Use interpolate(String, Context, Locale) or
implement a custom message interpolator.
A custom message interpolator may be provided (e.g., to interpolate contextual data, or to adjust the default Locale used).
See also:
Example 5.10. Use MessageInterpolator to use a specific Locale value
Example 5.11. Contextual container possible MessageInterpolator implementation

Dynamic custom validation with Play framework 2.2.x

This post makes it easy to set up a custom validation tag using Play 2.1(or 2).x "How to create a custom validator in Play Framework 2.0?". My use case, however, is a little different and I am hoping for a UI Play Framework Jedi Master that can provide some sort of direction. We have the normal MVC pattern to create reports, and for most scenarios, having validation in the play model using custom tags works fine. One of our use cases, however, is for custom "dynamic" validation. If we use the "Required" case, as an example, its easy to say that when form ABC loads, then fields X, Y and Z are required - But what if you dont know the list of required fields until just before the form is rendered and the list of required fields is provided to you in a json file from a db read in the controller. How, then, would you cater for this with the Play framework?
I may have a solution but without annotation. Create a validate method on your form (see http://www.playframework.com/documentation/2.2.x/JavaForms)
You'll be able to do all the checks you want there.

Design decision as to provide a single ejb component which invokes other specific ejb components

we have multiple buisiness format converter ejb components which do the following tasks
if the data submitted to them is xml , then convert from xml to corresponding format
else assume it is the buisiness format and convet it to the xml.
now we have a requirement to have single converter component which can pick up the right converter component based on the format of xml passed to it.
how would I decide which component to choose, because the ability to understand the format lies within the specific component?
You could implement this using a chain of responsibility pattern. Inject all the possible EJBs to the single Converter EJB and let the latter build up the chain. Each of the concrete converters implement an interface which provides some method like boolean canHandle(XML xml). Once one returns true let it handle the xml and return.

Custom #RequestParam return messages and conditional parameters

I am not really a fan of the default messages used if the #RequestParam fails to validate (type, required, etc). I would like to use my own custom messages.
I also have several parameters that are conditionally required.
I am thinking to achieve this I will need to roll my own HandlerMethodInvoker. resolveHandlerArguments using a modified version of RequestParam.
Is there an easy way to 'inject' my new version of HandlerMethodInvoker into Spring? If not, will I need to create my own DispatcherServlet and the various pieces between it and HandlerMethodInvoker?
If you only want to replace the validation messages then you only need to add some properties to the message files.
For more details read Spring Reference Chapter 5.3 Resolving codes to error messages and have a look at the java doc of org.springframework.validation.DefaultMessageCodesResolver, it explain the used message codes very vell.

Categories