2

There is no problem when the action configuration in struts.xml is like this:

<action name="customer-form">
    <result name="success" type="tiles">/customer.tiles</result>
</action>

The problem comes when I access the action class on action configuration (struts.xml). I access the class before displaying the form because I want the dropdown option in the form to display the appropriate value as I instantiate it in action class. But it turns out, it will return "input" and I must handle it.

method in action class:

public String addCusto(){
    custoType = new ArrayList<String>();
    custoType.add("ABC");
    custoType.add("EFG");
    System.out.println(custoType.size());
    return SUCCESS;
}

struts.xml:

<action name="customer-form" class="com.satunol.struts.template.action.CustomerAction" method="addCusto">
    <result name="success" type="tiles">/customer.tiles</result>
    <result name="input" type="tiles">/customer.tiles</result>
</action>

form in jsp

<s:form action="savecusto" method="post" validate="true">
<s:select
   label="Customer Type"
   list="custoType"
   emptyOption="true"
   headerKey="-1"
   headerValue="None"/>
<s:textfield name="custo.name" key="custo.name" size="20" />
<s:textfield name="custo.email" key="email" size="20" />
<s:textfield name="custo.address" key="address" size="20" />
<s:textfield name="custo.phone" key="phone" size="20" />
<s:submit method="addCustomer" key="label.add.customer" align="center" />

The result? The addCusto method is not executed, and my form is directly/automatically validated although not yet submitted.

How can I solve this?

Mahadi Siregar
  • 615
  • 3
  • 17
  • 38

1 Answers1

3

If your Action can return an input result, then you must handle it in the struts.xml configuration.

input result is returned by the Validation Interceptor when some validation error occurs, or when you are trying to set the wrong type of an Action's properties, for example when you try to set a int to a Date field.

When an Interceptor is returning a result instead of proceeding to the next Interceptor (or the Action if it is the last), the Action method called won't be executed, because it won't be reached.

Check out carefully your code and your request to see where it is failing and returning the input result.

P.S:

If with

I access the class before displaying the form because I want the dropdown option in the form to display the appropriate value as I instantiate it in action class.

you mean that you need to prepopulate fields before the execution of any method (or when input result is returned), you should use prepare() method for that, run by the Prepare Interceptor that runs before the Validation Interceptor. That way, your prepare() code will be executed even when validation will fail.

For more info, read How do we repopulate controls when validation fails

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • It solve when I add to action configuration in struts.xml. Whether that is the right way? – Mahadi Siregar Jun 19 '13 at 09:43
  • 1
    Yes, but it is in the Default Interceptor Stack by default... if your stack wasn't running it, it was because you created a custom Interceptor Stack without it. Just use the default, or remember to include the Prepare Interceptor in the custom stacks you create. – Andrea Ligios Jun 19 '13 at 09:52
  • Yes, I create default custom interceptor without including it into default. Thank you, it solve my problem. – Mahadi Siregar Jun 19 '13 at 10:29