Daud
Daud

Reputation: 7887

Struts2 - validation failure using xml file causes action class paramters to disappear from resulting jsp

I have a class A :

class A extends ActionSupport{

  int someId;
  // getters/setters

  public String execute(){
    setSomeId(2);
    return SUCCESS;
  }

  public String save(){
   // something
  }

}

In struts.xml, I have configured an action "ViewId" that takes us to the default method execute, where someId is set. Then, we are taken to a jsp page show.jsp where I can access the someId value. In show.jsp, I have to enter an email id and then submit the page. The action that's now called in "Save" that takes us to the save method of the action class. But, I have given some checks in the corresponding validation.xml file A-Save-validation.xml, which will check the email entered for a format. The problem is that if the xml validation fails, we are taken back to show.jsp, but the viewId parameter is now not available. Why is this so ?

The input page should appear similar to the user as before. Only the fields that are now validated should have an error page associated with them. Any workaround for this ?

Upvotes: 0

Views: 833

Answers (1)

mmalmeida
mmalmeida

Reputation: 1057

Like @Umesh said, validation happens on the corresponding interecptor, before the action's method is called.

When validation fails, the action's method is never called and you will be taken to the INPUT result.

In order to achieve what you want you have some options:

  1. Implement the preparable interface in your action
  2. Peform the validation inside your method.
  3. Use s:action in your jsp before the items you want populated to call an action that populates the relevant section

1 is probably the easiest. I like option 3 as well.

Upvotes: 1

Related Questions