0

I have around 90 menus each having at least 4 screens representing CRUD actions. Out of these, 13 menus have baltic character support & others don't. But still plenty of user's input baltic characters in those 87 menus due to which garbage data is being populated at Back end. BE is a different team and they can't accommodate changes atm, due to which I have to handle it at Front end.

I am looking for a generic solution, so I just write a method & reuse everywhere else.

The Generic solution I have decided is to iterate the request bean object & through reflection iterate over its methods & check each value for Baltic characters.

Another solution I had is to iterate HttpServlet request parameters & check them for baltic character presence.

I am using Struts2 and java 7. I wanted to know which of the above two options would be better to go with. I am aware that reflection comes with performance caveats. But I feel its a better approach then httpservlet request parameters.

If there is any other solution, request you to please share your idea/suggestion.

EDIT: For some menus, in our application, we have used jsp-servlet architecture, the same needs to be implemented in these kinds of menus as well.

cFrags
  • 11
  • 5

1 Answers1

0

Not sure what you're referring to by "request bean".

In any case, an interceptor is likely the best bet since you have access to the parameter map. If your actual parameters are nested into a bean that doesn't have map-like access, you'd still need to reflect.

This is one reason why I view S2 as a thin layer between the web and the business logic: I tend not to pre-optimize what's sent to the business logic, this way I can provide filtering at the web level for situations like this (e.g., a whitespace trimming filter may be applicable throughout the app).

A trimming interceptor looks roughly like this (as of Struts 2.1):

public class TrimInterceptor extends MethodFilterInterceptor {

    private List<String> excluded = new ArrayList<String>();

    protected String doIntercept(ActionInvocation invocation) throws Exception {
        Map<String, Object> parameters = invocation.getInvocationContext().getParameters();

        for (String param : parameters.keySet()) {
            if (isIncluded(param)) {
                String[] vals = (String[]) parameters.get(param);
                for (int i = 0; i < vals.length; i++) {
                    vals[i] = vals[i].trim();
                }
            }
        }

        return invocation.invoke();
    }

    private boolean isIncluded(String param) {
        for (String exclude : excluded) {
            if (param.startsWith(exclude)) {
                return false;
            }
        }

        return true;
    }

    public void setExcludedParams(String excludedParams) {
        for (String s : StringUtils.split(excludedParams, ",")) {
            excluded.add(s.trim());
        }
    }
}

There's a little bit of additional code in there, but primarily what you're looking for is where we get the parameter values:

String[] vals = (String[]) parameters.get(param);

If it's Baltic you do whatever it is you need to do with it and set it back to the value:

vals[i] = vals[i].trim();

Here it's trimming whitespace, but you get the idea.

There are likely a few differences now (this is from the Struts 2.1 era), but it should be enough to get you through.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302