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.
Related
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.
I have CXF Interceptor that checks field in SOAP header. I want to skip this check for a single method.
Is it possible to do it without parsing soap and checking for method name (for example, annotation).
Thank you!
If you put your interceptor fairly late in the chain (USER_LOGICAL for example), you can grab the BindingOperationInfo object from the exchange to determine which operation was used to process the body. From there, decide wether to look at the SOAP headers or not.
An interceptor gets executed even before CXF has started parsing the xml message (actually I use them to change the xml parser secure factory implementation class :P ), so I think what you need is not supported by the architecture (or at least I am unable to find it, if someone wants to bring some light here I will thank it too).
http://cxf.apache.org/docs/interceptors.html
May you separe your functionality in 2 webservices, each one with different interceptors and validation levels?
Consider the page below. As in the image I have attached.
Now my problem is that I have multiple clients accessing the same page as given below. Now consider that each client has their own requirement such as:-
Name is mandatory for some clients not for others.
Age is mandatory for some clients but not for others along with some specific validations like age<20 or age>30 etc.
Search is also optional depending upon the clients.
Now I am searching for any such tools or technologies or methods that could help me to sort out the issue of dynamically validating the fields as well as hiding and showing the fields depending upon my clients. Please let me know any tools or technologies that can help in order to solve the above problem. I also heard about rule engine and templating .... Is it possible to work together with it to achieve the same. Please suggest.
I have no idea why hibernate-validator would not work here. You implement the validation as you want, by implementing an interface and creating your own validation annotations if needed. This is a server-side validation btw.
Showing or not some content in case of JSP is done with conditional :
c:when/c:choose
for example, assuming it's JSP you are using.
In Spring, after validation we get a BindingResult object in the controller.
Simple enough, if I get validation errors I want to re-display my form with the error message above each afflicted field.
So to check for field errors on field username of my FormObject I call:
FieldError usernameFieldError = bindingResult.getFieldError("username");
Great, now I hold a FieldError object which, assuming I'm using the DefaultMessageCodeResolver now contains something like 4 possible error codes.
How do I go from FieldError -> A string that is consumable to the user?
I have a MessageSource defined in my webapplication context, so I can map a single error code to a message.
But sometimes the default message will be best, and sometimes I expect that two of the error codes might have a relevant message, so we need to choose the best one.
What method do I use to determine the best possible error message to present for a field error?
Do I need to write some algorithm to go through all the error codes and pick from the most specific?
Does spring provide any support for helping determine the most specific error message?
This whole process seems so long and convoluted, I thought spring was supposed to make this stuff easy. Maybe I'm totally off base somehow?
You are, as you guessed, making it way harder on yourself than it needs to be. The FieldError object is itself a MessageSourceResolvable. You don't need to get the codes off of it then take individual codes manually to your message source and go looking. You can just pass it to your MessageSource and it will find the most specific one that has a translation defined in your locale. (assuming your code resolver put them on in the right order.)
You really don't even need to do that in most cases though. Putting the Errors on your backing object and translating them yourself isn't usually needed. The form namespace in the jsp library provides a tag that looks up error messages for you. All you need to do is put the Errors in the ModelMap. See docs:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html#view-jsp-formtaglib-errorstag
I have a Struts 1 application using standard Struts internationalization, with a property file and everything. I need to change a specific message for only a select group of users, so I want to extend PropertyMessageResources. However, I can't find a way to connect the current request (so that I know whether it's one of those select users) to the message lookup.
1) Is there a better way to get this functionality? I thought about putting logic in the jsp's, but that doesn't get some situations, like messages obtained through the validator.
2) Is there a possible way to connect the request to the various getMessage() methods of my extended PropertyMessageResources object?
Here's what I ended up doing:
In the login script, check if it's one of the select users, and add a special String to the Variant part of their Locale, and put it in the session as their locale.
request.getSession(true).setAttribute("org.apache.struts.action.LOCALE", new Locale(locale.getLanguage(), locale.getCountry(), customVariant);
In the overridden PropertyMessageResources class, in the getMessage(Locale,String) method, just check the locale passed in to see if it has the special variant. I made all my custom variants start with a "_", and I check the first character of the variant before looking it up (so as to not slow down other users).
If anyone has a better solution, please post it.