Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

struts2 - Avoid Return "input" Automatically in Struts

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...