rmaitipe
rmaitipe

Reputation: 186

Struts 2 Action not getting called

I'm coming across a scenario where if the form allowing editing an item has an empty field, Struts action is not invoked. The page in customized through freeMarker template. The call comes to the interceptor and pass successfully and then gets dropped before it can call the action method. Because of this I don't see any error messages to figure out whats going wrong. If the field is not empty, the action goes through and the item gets updated.

This is my Struts configuration for the Interceptor and it seems to be fine.

<interceptors>
    <interceptor name="loginInterceptor" class="login.interceptor.LoginInterceptor" />      
    <interceptor-stack   name="appStack">
        <interceptor-ref name="defaultStack"/>
        <interceptor-ref name="loginInterceptor"/>
    </interceptor-stack>
</interceptors>
<default-interceptor-ref name="appStack" />

And this is the action in Struts.xml

<action name="upDate" method="upDate" class="com.coll.Item">
    <result name="success">jsp/item.jsp</result>
    <result name="input">jsp/item.jsp</result>
</action>

The field in the item.ftl looks like this

<tr>        
    <td>Prior Order Number</td>
    <td><@s.textfield name="item.prevOrdNum" id="prevOrdNum" theme="simple"/></td>
</tr>

Another puzzling factor is since this is a small internal application it has not being updated for years but this problem started several weeks ago. If I remove that field completely in the webpage or if I put a value for the field in the Database so it's not empty when loaded up - the update goes through. I'd appreciate any ideas that could explain whats going on.

Upvotes: 0

Views: 1878

Answers (1)

Andrea Ligios
Andrea Ligios

Reputation: 50281

You're most likely ending in one of the following cases:

  • a conversion error, because your variable is a primitive number and you're sending a null, that is not allowed, or
  • a validation error, because you have some validation somewhere (validate() methods, XML, annotations etc...) that specify that that field must not be null.

In either case, the Workflow interceptor will return an INPUT result, that you've mapped to the same JSP page, which is not wrong but which alone is not enough to know which one between an INPUT and a SUCCESS results has been returned.

Conversion errors and Field Validation errors carries error messages in the FieldErrors map, not in the ActionErrors one (that you probably prints on top of the page with an <s:actionerror /> tag).

With the default theme, XHTML, every fieldError is printed near to the field it belongs; you however have used the simple theme for your tag, hence the field error will not be printed automatically.

You have two choices:

  1. Print all the field errors on top of the page:

    <style type="text/css">
        .error { border: 5px solid red; }
    </style>
    
    <@s.if test="hasFieldErrors()">
        <div class="error">
            <@s.fielderror />
        </div>
    </@s.if>
    
  2. Or manually attach <s:fielderror/> tags to your fields:

    <tr>        
        <td>Prior Order Number</td>
        <td>
            <@s.textfield name="item.prevOrdNum" id="prevOrdNum" theme="simple"/>
            <@s.fielderror fieldName="item.prevOrdNum" />
        </td>
    </tr>
    

Upvotes: 1

Related Questions