SUBMIT
controls are used in an HTML form,
it's natural to use this style:
<input type='submit' name='operation' value='Add'>
<input type='submit' name='operation' value='Change'>
<input type='submit' name='operation' value='Delete'>
Here, the server detects the desired operation by simply examining the
value of the operation
parameter.
However, this style doesn't work well when the application is multilingual,
since the value
attribute varies according to language.
A simple workaround for this problem is to use a different name for
each SUBMIT
control:
<input type='submit' name='operation_Add' value='Add'>
<input type='submit' name='operation_Change' value='Change'>
<input type='submit' name='operation_Delete' value='Delete'>
Here, the server will detect the desired operation by looking for the
presence of certain parameter names, and not by examining the value
of a single operation
parameter.
An application might define a naming convention to ease the extraction
of the desired operation. Here's an example method where request parameters
whose names start with operation_
are treated as operation
indicators. This method may be placed in a base action class, or in a utility
class:
import java.util.*; import javax.servlet.http.HttpServletRequest; import java.util.logging.*; public final class SubmitOperation { /** * Determine the desired operation. * * Returns the name of the single submitted operation_X parameter, without * the leading 'operation_'. For example, if a single parameter named * operation_Add is present in aRequest, then this method will return * 'Add'. * * This method depends on two conventions : * <ul> * <li>a naming convention, whereby the names of request parameters which * denote an operation are in the style : 'operation_blah', 'operation_XYZ', * and so on. That is, the parameter name starts with 'operation_' * <li>at most one operation_X parameter can appear in aRequest * (it may have none; in that case this method will return null). * </ul> * * This method is needed only when the app is multilingual, and * multiple SUBMIT buttons appear on a single page. */ String getOperation(HttpServletRequest request){ String result = null; String OPERATION = "operation_"; List<String> operationParamNames = new ArrayList<>(); Enumeration<String> allParamNamesEnum = (Enumeration<String>)request.getParameterNames(); while (allParamNamesEnum.hasMoreElements()) { String paramName = allParamNamesEnum.nextElement(); if (paramName.startsWith(OPERATION)) { operationParamNames.add(paramName); } } if (! operationParamNames.isEmpty()) { if (operationParamNames.size() > 1) { throw new IllegalArgumentException( "Expecting at most one parameter starting with '" + OPERATION + "'. Actually found : " + operationParamNames ); } String operationParamName = operationParamNames.get(0); result = operationParamName.substring(OPERATION.length()); } logger.fine("Operation : " + result); return result; } // PRIVATE private static final Logger logger = Logger.getLogger( SubmitOperation.class.getName() ); }