views:

794

answers:

5

Hi All, I am working on struts2. I have two fields in my action-validation.xml. I want if validation get fails at first field it will go to some jsp page (say a.jsp) and if validation get fails at second field then it will go to another jsp (say b.jsp). As it always return "input" when validation fails so currently I can target only one jsp page against it. Please provide solution if possible? Thanks in advance.

A: 

You will need to create custom validation method in your action, return a custom resut:

public void validate() {
  if(!isFieldAValid()) {
    return "DISPLAY_A";
  }

  if(!isFieldBValid()){
    return "DISPLAY_B";
  }
}

Then in your struts.xml you will need to add the custom results:

<result name="DISPLAY_A">/a.jsp</result>
<result name="DISPLAY_B">/b.jsp</result>
Rich Kroll
A: 

Re Rich's suggestion - a void method can't return a String so validate() can't return anything surely?

A: 

How this will work? validate() is a void method, no return allow, how can you return "DISPLAY_A" ?

A: 

rich's solution is quiet practicable if the logic is applied in the action being called on submit....

public String actionBeingCalledOnSubmit() {

if(!isFieldAValid()) { return "DISPLAY_A"; }

if(!isFieldBValid()){ return "DISPLAY_B"; } }

A: 

void return types can't return anything

Did you design a work around?

Kris