3

My goal is to have an actions properties be null if they are set due to request parameters that are empty strings.

I created an interceptor that is run before the default stack, which trims all values in invocation.getInvocationContext().getParameters() to null. However, this did not fix my problem; despite the parameters now being null (verified in the action using ParameterAware), the properties are still being set to empty strings.

Upon further reading, I see that the parameters interceptor is likely the cause, in particular that:

While this interceptor is being invoked, a flag (ReflectionContextState#CREATE_NULL_OBJECTS) is turned on to ensure that any null reference is automatically created - if possible. See the type conversion documentation and the InstantiatingNullHandler javadocs for more information.

Is there some easy way to achieve my goal?

If worse comes to worse, it seems like it would be possible to extend the ParametersInterceptor class, and override the methods doIntercept(ActionInvocation invocation) and setParameters(Object action, ValueStack stack, final Map<String, Object> parameters). This just seems incredibly awkward for what, I assume, would be behaviour that many people would desire.

0

2 Answers 2

2

I don't think too many people would really desire this behavior since it basically subverts normal HTTP parameter behavior, where unfilled parameters are an empty string.

You have a few options.

You could try to replace or extend the StrutsObjectFactory. You could try to replace or extend the InstantiatingNullHandler. Both of these are configured in struts-default.xml and should be easily overrideable.

I might take a different approach, however, since you're basically asking to work around HTTP, and annotate action fields that should be made null when their input is blank. An interceptor running after "parameters" would check the parameters against the action's properties and annotation and null them out as appropriate.

There are any number of variations on this idea, you might just have an action that has a map and "null" parameters are indicated with a map entry. This avoids annotation and method lookup (although that could be cached on class name) at the expense of some additional code.

Sign up to request clarification or add additional context in comments.

2 Comments

I've taken your suggestion that I am working around HTTP to heart, and have instead updated the setters on the form fields set the field to null when passed an empty string.
@Matt Oh, somehow I didn't even think of that--that's much, much simpler!
0

You can try to add this RemoveEmptyParametersInterceptor to your interceptor-stack just before the ParametersInterceptor.

It works quite well for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.