|
ok,
let me first explain the concept that i used. I have a menu item to add a new
taxpayer, this link calls a handler, RequestAdd
I have
the following code in this actionhandler. Make sure your actionhandler
class extends Action!! NB
All
important code is in bold
red
here
is the save token method in this request add handler code
//***************************************
public
final class TaxPayerActionHandlerRequestAdd extends
Action implements IActionHandler{
TaxPayerActionForm actionFormObject = null;
HttpServletRequest aRequest = null; UserForm user = null;
public ActionHandlerResponse
executeAction(ActionMapping mapping, ActionForm form, HttpServletRequest
request){
System.out.println("In " +
this.getClass());
actionFormObject = (TaxPayerActionForm)form; aRequest = request; ActionErrors errors = null; errors = doTaxPayerRequestAdd(); if (errors == null){ return new ActionHandlerResponse(errors,mapping.findForward("success")); } else { return new ActionHandlerResponse(errors, mapping.findForward("failure")); } }
public ActionErrors doTaxPayerRequestAdd(){
ActionErrors errors = null;
try {
HttpSession session =
null;
if (aRequest.getSession(false) == null){ System.out.println("Session is null"); session = (HttpSession)aRequest.getSession(true); } else{ session = (HttpSession)aRequest.getSession(false); } actionFormObject.setAction(Constants.ACTION_ADD); saveToken(aRequest); }
catch (Exception ex) { ex.printStackTrace(); errors = new ActionErrors(); errors.add("taxPayerReqAdd", new ActionError("error.taxPayerAdd",ex.toString())); } return errors; } } //***************************************
then when you r ready to do the add action to actually
add this taxpayer i.e. database save, then you have some more code to evaluate
this token, here is the code from my Add action
handler
//***************************************
public final class
TaxPayerActionHandlerAdd extends
Action implements IActionHandler
{
TaxPayerActionForm actionFormObject = null;
HttpServletRequest aRequest = null; UserForm
user = null;
public ActionHandlerResponse executeAction(ActionMapping mapping, ActionForm
form, HttpServletRequest request){
System.out.println("In " +
this.getClass());
actionFormObject = (TaxPayerActionForm)form; aRequest = request; ActionErrors errors = null; errors = doTaxPayerAdd(); if (errors == null){ return new ActionHandlerResponse(errors,mapping.findForward("success")); } else { actionFormObject.setAction(Constants.ACTION_ADD); return new ActionHandlerResponse(errors, new ActionForward(mapping.getInput())); }
}
public
ActionErrors doTaxPayerAdd(){
ActionErrors errors = null;
try
{
HttpSession
session = null;
if (aRequest.getSession(false) == null){ System.out.println("Session is null"); session = (HttpSession)aRequest.getSession(true); } else{ session = (HttpSession)aRequest.getSession(false); } user = new UserForm(); user = (UserForm)session.getAttribute(Constants.USER_KEY); try{ Object obj = (Object)EJBUtil.getBeanHome(EFilerSessionHome.JNDI_NAME, user.getUsername(), user.getPassword()); EFilerSessionHome eFilerSessionHome = (EFilerSessionHome)PortableRemoteObject.narrow(obj, EFilerSessionHome.class); EFilerSession eFilerSession = eFilerSessionHome.create(); TaxPayerForm emptyFormObject = eFilerSession.getEmptyTaxPayerForm(); TaxPayerForm populatedFormObject = actionFormObject.getFormProperties(emptyFormObject);
FieldErrorList fldErr =
null;
//Do validations fldErr = populatedFormObject.validate(); if (fldErr !=null && fldErr.size() > 0) { return ErrorUtil.toActionErrors(fldErr); }
ActionErrors tokenErrors = new
ActionErrors();
if (!isTokenValid(aRequest)) { tokenErrors.add("addTaxpayer", new ActionError("error.transaction.token.add.taxpayer")); } resetToken(aRequest); if (!tokenErrors.empty()) { return tokenErrors; } eFilerSession.registerNewTaxPayer(user, populatedFormObject); } catch (PaymentSessionException ex) { errors = ErrorUtil.toActionErrors(ex.getFieldErrors()); } catch (RemoteException ex) { if (errors==null) { errors = new ActionErrors(); } errors.add("remoteEx", ErrorUtil.toActionError(ex)); } } catch (Exception ex) { ex.printStackTrace(); if (errors==null) { errors = new ActionErrors(); } errors.add("taxPayerAdd", new ActionError("error.taxPayerAdd")); } return errors; } }
|
Title: Message

