Hi,

I am having a problem with a JSP not displaying the latest ActionForm 
data.

A very light use case:
-------------------------
The application is simple; a user views their Contact information.
The user can change the information and save it.

The application does the following:
------------------------------------------
1 Gets the user's Contact information from the DB including a TimeStamp
2 Validates any changes and then updates the information in the DB
3 Finally, the application gets the user's Contact information again to 
obtain the latest TimeStamp (for technical difficulties the latest 
TimeStamp cannot be returned during a update).

Problem:
----------
In step 3 above, the latest data is never shown; only the data that was 
entered by the user is what is shown. I discovered this due to the 
TimeStamp not changing?

Details:
--------
Here are some details:
<action-mappings>
<action path="/Contact"
   type="secure.contact.ContactAction"
   parameter="/WEB-NF/secure/contact/contact.jsp"
   name="contactForm"
   scope="request"
   validate="false">
   <forward name="contactForm"
      path="secure/ContactForm"
      contextRelative="false">
   </forward>
   <forward name="contactPrintableVersionForm"
      path="secure/ContactPrintableVersionForm"
      contextRelative="false">
   </forward>
</action>

<action path="/ContactForm"
   parameter="/WEB-INF/secure/contact/contact.jsp"
   type="org.apache.struts.actions.ForwardAction"
   name="contactForm"
   scope="request"
   validate="false">
</action>

<action path="/ContactSubmit"
   type="secure.contact.contactSubmitAction"
   input="/WEB-INF/secure/contact/contact.jsp"
   name="contactForm"
   scope="request"
   validate="true">
   <forward name="cancel"
      path="MyData"
      contextRelative="true"
      redirect="true">
   </forward>
   <forward
      name="update"
      path="secure/Contact"
      contextRelative="false"
      redirect="false">
   </forward>
</action>

<action path="/ContactPrintableVersionForm"
   type="org.apache.struts.actions.ForwardAction"
   parameter="/WEB-INF/secure/contact/contactPrintableVersion.jsp"
   name="contactForm"
   scope="request"
   validate="false">
</action>
</action-mappings>
<action-mappings>

ContactForm.java
----------------------
package secure.contact;

import ..

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.util.ImageButtonBean;
import org.apache.struts.util.MessageResources;

public class ContactForm extends ActionForm {

   private ImageButtonBean cancelButton = new ImageButtonBean();
   private String firstName = null;
   private String timeStamp = null;
   private String token = null;
   private ImageButtonBean updateButton = new ImageButtonBean();

 
   public void reset(ActionMapping mapping,
                     HttpServletRequest request) {
   }

   public ActionErrors validate(ActionMapping mapping,
                                HttpServletRequest request) {

      ActionErrors errors = new ActionErrors();

      HttpSession httpSession = request.getSession();
      OnlineVO onlineVO = (OnlineVO) httpSession.getAttribute("OnlineVO");

      // Get the user's locale
      OnlineLocale onlineLocale = new OnlineLocale(request);

      // Exit if the Cancel button is clicked?
      if (getCancelButton().isSelected()) {
         httpSession.removeAttribute("contactBean");
         return errors;
      }

      // First Name is required and must only be composed of alphabetic 
characters
      if (getFirstName().trim() == null || getFirstName().trim().length() 
< 1) {
         errors.add("firstName", new 
ActionError("errors.general.alphabetic.required", 
messages.getMessage(onlineLocale.getLocaleLocale(), "label.first.name")));
      }

      public String getFirstName() {
         return firstName;
      }

      public String getTimeStamp() {
         return timeStamp;
      }

      public void setTimeStamp(String string) {
         timeStamp = string;
      }

      public ImageButtonBean getCancelButton() {
         return cancelButton;
      }

      public ImageButtonBean getUpdateButton() {
         return updateButton;
      }

      public void setCancelButton(ImageButtonBean bean) {
         cancelButton = bean;
      }

      public void setUpdateButton(ImageButtonBean bean) {
         updateButton = bean;
      }

      public String getToken() {
         return token;
      }

      public void setToken(String string) {
      token = string;
   }

}


ContactAction.java
----------------------
package secure.contact;

import ..

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.Globals;
import org.apache.struts.util.MessageResources;


public class ContactAction extends Action {

   private static Logger logger = Logger.getLogger(ContactAction.class);

   public ActionForward execute(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
      throws Exception {

      HttpSession httpSession = request.getSession();
      OnlineVO onlineVO = (onlineVO) httpSession.getAttribute("OnlineVO");

      ContactForm contactForm = (ContactForm) form;
      ActionMessages messages = new ActionMessages();
      ActionErrors errors = new ActionErrors();
      OnlineLocale onlineLocale = new onlineLocale(request);

      String token = generateToken(request);
      contactForm.setToken(token);
      httpSession.setAttribute(Globals.TRANSACTION_TOKEN_KEY, token);
 
      // Get the contact
      ContactTO contactTO = new ContactTO();
      contactTO.setNumber(onlineVO.getNumberVO().getNuumber().toString());

      ContactFacade contactFacade = new ContactFacadeImpl();

      try {
         contactTO = contactFacade.getContact(contactTO);
      } catch (FatalException fe){
         // messages here
         saveErrors(request, errors);
         return 
mapping.findForward(errorMessages.getMessage("forward.systemError"));
      }


      // Set the Form values with the Transfer Object values
      contactForm.setFirstName(contactTO.getFirstName());
      contactForm.setTimeStamp(contactTO.getTimeStamp());

      // Determine which view to forward to: Printable Version or the 
Regular Page
      String printableVersion = null;
      printableVersion = request.getParameter("frmt");

      if ((printableVersion != null) && (printableVersion.length() > 0)) {
         return mapping.findForward("contactPrintableVersionForm");
      } else {
         return mapping.findForward("contactForm");
      }
   }
}

ContactSubmitAction.java
--------------------------------
package secure.contact;

import ..

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;

public class ContactSubmitAction extends Action {

   private static Logger logger = 
Logger.getLogger(ContactSubmitAction.class);

   public ActionForward execute (ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
      throws Exception {

      if (logger.isDebugEnabled()){
         logger.debug("starting");
      }
 
      ContactForm contactForm = (ContactForm) form;

      if (contactForm.getCancelButton().isSelected()) {
         return mapping.findForward("cancel");
      }

      HttpSession httpSession = request.getSession();
      OnlineVO onlineVO = (OnlineVO) httpSession.getAttribute("OnlineVO");

      ActionMessages messages = new ActionMessages();
      ActionErrors errors = new ActionErrors(); 
      OnlineLocale onlineLocale = new OnlineLocale(request);

      if (!isTokenValid(request)) {
         // messages 
         return mapping.findForward("update");
      }

      // Update the contact
      ContactTO contactTO = new ContactTO();
      contactTO.setFirstName(contactForm.getFirstName());
      contactTO.setTimeStamp(contactForm.getTimeStamp());

      contactFacade contactFacade = new ContactFacadeImpl();

      try {
         contactTO = contactFacade.updateContact(contactTO);
      } catch (FatalException fe){
         // Messages
         saveErrors(request, errors);
         return 
mapping.findForward(errorMessages.getMessage("forward.systemError"));
      }

      messages.add(ActionMessages.GLOBAL_MESSAGE, new 
ActionError("info.contact.update.success"));
      saveMessages(request, messages);
      request.setAttribute("informationalMessage", "true");
      return mapping.findForward("update");
   }

}


I would like to draw your attention to the return of the 
ContactSubmitAction.
What I am doing is forwarding the request to the /Contact in order to get 
the updated contact information. This works fine. I have traced it and the 
data returned is the latest one.

I have even traced the ContactAction and the lastest data is being moved 
to the ActionForm. But the latest data from the ActionForm is not being 
shown in the JSP only the data that the user has entered.

I have played around with the ActionForm reset() without any success.

It seems as if the ActionForm still exists even after the first request is 
completed!!!

I have found a workaround which is to redirect to the /Contact once the 
update is done.
This is not efficient and also requires me to set a message in the Session 
informing the user that the update is successful.

Any insight would be greatly appreciated.
Glenn.

Reply via email to