I have a form made in JSP, here I have multiple buttons – “Approve, Reject, Save, Cancel”.
For every submit, the control goes to a single Controller(Servlet), and there I am handling this submit as ::
String methodName = (String) request.getParameter("methodName");
if(methodName.trim.toLower().equals("approve"))
{
approve_Claim(parameters);
}
else if(methodName.trim.toLower().equals("reject"))
{
reject_Claim(parameters);
}
else if(methodName.trim.toLower().equals("save"))
{
save_Claim(parameters);
}
else if(methodName.trim.toLower().equals("cancel"))
{
cancel_Claim(parameters);
}
Is there a way to remove these multiple if’s,
please suggest
It looks like you basically want a mapping from
methodName.trim().toLower()to some sort of “claim action” which takes parameters. Create an interface like this:Then implement this with classes of
CancelClaimAction,ApproveClaimActionetc. Next create aMap<String, ClaimAction>mapping “approve” to anApproveClaimActionetc. Then:You could use enums to do this, but I’d expect each of these classes to have enough work to do that it’s worth separating them out and testing each one individually.