Search code examples
javastruts2query-stringquerystringparameter

Is there a way of getting values of properties in an action class without supplying them as query-string parameters while using a redirect action?


When a POST request succeeds, the request is redirected as follows.

private String message; //Getter and setter.

//Required validators.
@Action(value = "AddUpadteCategory",
    results = {
        @Result(name=ActionSupport.SUCCESS, type="redirectAction", params={"namespace", "/admin_side", "actionName", "Category", "currentPage", "${currentPage}", "message", "${message}", "status", "${status}"}),
        @Result(name = ActionSupport.INPUT, location = "Category.jsp")},
    interceptorRefs={
        @InterceptorRef(value="defaultStack", params={"params.acceptParamNames", "catId, catName, currentPage, rowCount, totalPages, status", "validation.validateAnnotatedMethodOnly", "true"})
    })
public String insertOrUpdate()
{
    if(true)  //Some conditional check.
    {
         setMessage(getText("message.key.true"));
    }
    else
    {
          setMessage(getText("message.key.false"));
    }

    return ActionSupport.SUCCESS;
}

This message is supplied as a query-string parameter, if no validation/conversion errors occur.

Sometimes, this message could be long forming a long URL string.

Is there a way to get this message without passing it as a query-string parameter, while using a redirect action?

Storing this message into a session could not be an alternative.


Solution

  • Use store interceptor and set its operationMode parameter on AUTOMATIC as follows:

    <action name="actionName" class="..." method="...">
        <interceptor-ref name="store">
            <param name="operationMode">AUTOMATIC</param>
        </interceptor-ref>
        <interceptor-ref name="defaultStack" />
        <result ..../>
    </action>
    

    It will automatically set your messages in appropriate scope. For redirectAction result it will be session scope but after displaying your messages this interceptor will automatically remove them.

    EDIT (Annotation syntax):

     interceptorRefs={
            @InterceptorRef(value="store", params={"operationMode", "AUTOMATIC"}),
            @InterceptorRef(value="defaultStack", params={"params.acceptParamNames", "catId, catName, currentPage, rowCount, totalPages, status", "validation.validateAnnotatedMethodOnly", "true"})
        })