简体   繁体   中英

Struts2 result type=“redirectAction” doesn't redirect

I have a program where the following code (not written by myself) works as intented:

.JSP code:

<input type="submit" value="add sample" name="action:dataAddSample" id="buttonAddSample"/>

STRUTS.XML code:

<action name="dataAddSample" class="com.invenso.xperido.controller.DataCRUD" method="addSample">
    <result name="success" type="redirectAction">
        <param name="actionName">dataform</param>
        <param name="id">${data.id}</param>
    </result>
</action>

After the actionmethod addSample returns "success", the action dataform is performed, and this action redirects the user to the following url (that uses parameter id):

http://localhost:8080/Test/data/dataform.action?id=11476

I copied this code in the same package & namespace to achieve a similar workflow, only with different action method (delete instead of add):

<action name="dataDeleteSample" class="com.invenso.xperido.controller.DataCRUD" method="deleteSample">
    <result name="success" type="redirectAction">
        <param name="actionName">dataform</param>
        <param name="id">${data.id}</param>
    </result>
</action>

I changed the way the action is invoked by using ajax instead of directly linking the input button to the action:

JAVASCRIPT/AJAX code:

$.ajax({
type: "POST",
url: "<s:property value='urlDeleteSample'/>",
data: { sampleDBName: sampleName, sampleDBQueryName: queryName },
success: function(data, textStatus, jqXHR){ ......

The URL is exactly the link to the action so that works. With the use of debug messages I can confirm the action dataDeleteSample triggers, and the method deleteSample is invoked. But for some reason the user is not redirected to the url like above. I even removed all business logic in the method deleteSample and just let it return Action.SUCCES, but still the result does not seem to trigger the dataform action and redirect the user.

What could cause this? Is it the fact I used AJAX? I'm not familiar enough with struts to know why the first code works, but mine doesn't. Any insights on how the struts framework handles this scenario is welcome.

You won't notice the redirect because you are calling it with AJAX, which means that the DATA you get must be appended into the DOM in order to see the result. In the success event handler you must do that. It might be failing there, debug it.

You have to replace with type="chain"

<action name="remove" class="com.action.firstAction" method="remove">
     <result name="success" type="chain">secondaction</result>
</action>
<action name="second action" class="com.action.secondAction" method="result">
    <result name="success">result.jsp</result>
</action>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM