Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1014
  • Last Modified:

Struts Iterate Tag with Form Validations

I have a scenario where I have to show some list of configuration items in the updatable listing format. That is the listing contains the configuration Item Name (uneditable display text), Configuration Item (editable text box).

I am getting the list from the database and when user hits save, I have to do some form validations and if valiadtion fails then I have to navigate back to the listing with the error message.

I am getting the configuration Items in the form of ConfigurationVo objects and holding the collection in request scope. In my Jsp I am using <logic:iterate> to loop through the collection and render information as static text or set value in a text field. Now, when user updates some data and clicks "Save" button and if the validation fails, then it is navigating back to the same page but there is nothing being showed in the listing. I guess, it is no more able to find the collection in request scope. When I try to add the collection in session scope I am able to see the listing upon validation error. But, the values are defaulted to the original values but not the one's which user has edited.

I would appreciate of someone could help me in showing the listing with values that user has edited by adding the collection to request scope.

I have also tried using index property of iterate tag. but I guess I not quitely confident in my approach. What is the correct approach of using indexed property?

In summary:
This is what is currently have:
SysConfigAction: Action class that gets the configuration Items from the database. Arraylist of ConfiguartionVO objects
-------------------------------------------
public class ConfigurationVO {

  public void setCode(String code) {
    this.code = code;
  }

  public void setItem(String item) {
    this.item = item;
  }

  public void setValue(String value) {
    this.value = value;
  }

  public void setUserId(String userId) {
    this.userId = userId;
  }

  public void setUserMaintainIndicator(boolean userMaintainIndicator) {
    this.userMaintainIndicator = userMaintainIndicator;
  }

  public String getCode() {
    return code;
  }

  public String getItem() {
    return item;
  }

  public String getValue() {
    return value;
  }

  public String getUserId() {
    return userId;
  }

  public boolean isUserMaintainIndicator() {
    return userMaintainIndicator;
  }
  public ConfigurationVO() {
  }

  private String code;
  private String item;
  private String value;
  private String userId;
  private boolean userMaintainIndicator;
------------------------------------------
SystemConfigForm:
-----------------------------------------
import java.text.*;
import javax.servlet.http.*;

import org.apache.struts.action.*;

/**
 *
 * <p>Title: SystemConfigForm</p>
 * <p>Description: This form is used to hold the values that are enterd by the
 * usr in the System Configuration User Interface.
 * The validate method of this class is used to validate the format of the data
 * that has been keyed by the user.
 * </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: UPS</p>
 * @author Saritha Ventrapragada
 * @version 1.0
 */
public class SystemConfigForm
    extends ActionForm {
  public void setCode(String[] code) {
    this.code = code;
  }

  public void setItem(String[] item) {
    this.item = item;
  }

  public void setValue(String[] value) {
    this.value = value;
  }

  public void setUserMaintainIndicator(boolean[] userMaintainIndicator) {
    this.userMaintainIndicator = userMaintainIndicator;
  }

  public void setMethodToCall(String methodToCall) {
    this.methodToCall = methodToCall;
  }

  public String[] getCode() {
    return code;
  }

  public String[] getItem() {
    return item;
  }

  public String[] getValue() {
    return value;
  }
  public boolean[] getUserMaintainIndicator() {
    return userMaintainIndicator;
  }

  public String getMethodToCall() {
    return methodToCall;
  }

  /*default constructor*/
  public SystemConfigForm() {
  }

  private String[] code;
  private String[] item;
  private boolean[] userMaintainIndicator;

  private String methodToCall;

  public ActionErrors validate(ActionMapping mapping,
                         HttpServletRequest request) {
    ActionErrors errors = new ActionErrors();
    ActionError ae = null;
    int i = 0;
    try {

      for (i = 0; i < value.length; i++) {
      /*checking if the value of the configuration item is zero or empty*/
      if (value[i] == null) {
        ae = new ActionError(Constants.ERROR_FORM_TEXT_VALIDATION, value[i],
                         item[i]);
        errors.add(Constants.FORM_VALIDATION, ae);
      }
      else if ((Integer.parseInt(value[i])) < 1) {
        ae = new ActionError(Constants.ERROR_FORM_TEXT_VALIDATION, value[i],
                         item[i]);
        errors.add(Constants.FORM_VALIDATION, ae);
      }
      }
    }catch (Exception e) {
      logger.fatal("1000", e);
      ae = new ActionError(Constants.ERROR);
    }
    return errors;
  }
}
-----------------------------------------
SystemConfigAction
----------------------------------------
    SystemConfigSL scl = new SystemConfigSL();
      ConfigurationVO cfgvo = null;

      ArrayList sysConfig = scl.getConfig();
      (request.getSession()).setAttribute("sysConfig",sysConfig);
-----------------------------------------

Can you please help me in solving my problem. I should be able to show the user with the values he have updated on error conditions.

Thank you
0
vsaritha_9
Asked:
vsaritha_9
  • 4
  • 3
1 Solution
 
vsaritha_9Author Commented:
seems no one knows solution to my problem or atleast knows approach to solve it
0
 
bloodredsunCommented:
Switch your validation from the frame work to the action e.g. set validation to false in the config and in the action call

ActionErrors ae = ( (ActionForm) form ).validate();

If ae != null, it has failed validation so you can forward back to the jsp but you can resave the collection in the request again and then forward.
0
 
bloodredsunCommented:
Did my recommendation work? Or do you need an example?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
vsaritha_9Author Commented:
Hello,
I really appreciate your response.

I guess even if I call validate from Action class, I will not have my list in my request scope as a new request will be started when user clicks on some button.

Thank you
0
 
bloodredsunCommented:
Yes. In that case you may wish to put the list in the session scope for viewing over multiple pages.
0
 
vsaritha_9Author Commented:
And also I need to update the collection with the values that user has updated. Which I am currently doing. But I am not sure if this is the correct approach.

I was thinking of using indexed property and loop through the collection. But I am not sure how this can be acheived.
0
 
bloodredsunCommented:
An ArrayList or other datastruture would be fine for you. Arraylist is good as it implements the Collections interface as well as List

All you do if do a quick check for updated values and if so, enter them into the object stored in session and that's it.

as for looping over a structure, here's a fragment

         ArrayList employees = new ArrayList();
         employees.add("Fred");
         employees.add("Jen");
         employees.add("Henry");
         for(Iterator iter = employees.iterator(); iter.hasNext();) {
           String emp = (String)iter.next();
           System.out.println(emp);
         }
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now