4

In my project I am updating details so I created action, but it gives me exception in response as

No result defined for action org.employee.actions.EmployeeMyProfileAction and result input

In struts.xml (Before)

<action name="savePersonalDetails"  class="org.employee.actions.EmployeeMyProfileAction" method="updateEmployeeDetails">
        <result name="success">empMyProfile.jsp</result>
</action>

(After)

<action name="savePersonalDetails" class="org.employee.actions.EmployeeMyProfileAction" method="updateEmployeeDetails">
    <result name="success">empMyProfile.jsp</result>
    <result name="input">emp-personal-form.jsp</result>
</action>

Ajax Call

function checkPersonal(id) {

    if (checkEverythingP()) {
        $.ajax({
            type : 'POST',
            url : 'savePersonalDetails',
            data : $('#personalform').serialize(),
            success : function(data) {
                alert('success');
            },
            error : function() {
                alert('error');
            }
        });
    }
}

It gives me success message in JQuery but It is not going to the action class declared. I didn't understand why it is happening after everything is correct. I referred many sites for this but not resolved. Please suggest me what is going wrong.

Roman C
  • 49,761
  • 33
  • 66
  • 176
MegaBytes
  • 6,355
  • 2
  • 19
  • 36

2 Answers2

2

Not everything is correct as you thought, because in the success callback function you have received INPUT result. This result is returned by workflow interceptor, which is in the defaultStack - the stack of interceptors used by default if your action doesn't override the interceptors configuration. It checks if an action invocation has validation errors like action errors or field errors (the conversion errors) then returns a result specified by the parameter inputResultName. By default this parameter is set to "input". If the interceptor returns a result it breaks a chain of interceptors and invocation of action method. You noted it saying It is not going to the action class declared.

The solution is to override interceptors configuration of the action to use basic stack, i.e. without validation and/or workflow interceptors.

<action name="savePersonalDetails"  class="org.employee.actions.EmployeeMyProfileAction" method="updateEmployeeDetails">
  <interceptor-ref name="basicStack"/>
  <result name="success">empMyProfile.jsp</result>
</action>

If you still need to perform validations you can do it programmatically or configure workflow interceptor to filter your action method. The last option you should use only if you have enough reasons to do so, because it overcomes the purpose of the interceptor itself.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 3
    After I added the interceptor-ref tag, the control not goes in the specified method of action class. I don't know why it not goes. I checked request and response, in response it giving page. – MegaBytes Oct 08 '14 at 04:45
  • Ok, try again. Assume you don't understand what is a custom stack. – Roman C Oct 08 '14 at 08:05
  • 3
    Thank you Roman,It is working now after adding ` error emp-personal-form.jsp` – MegaBytes Oct 09 '14 at 08:48
  • @MegaBytes: So you are hacking around error instead of fixing it. – Aleksandr M Oct 09 '14 at 09:20
  • @Aleksandr M, I m new in struts2 and I am working on the project where I was facing this problem, as Roman Suggested me, I read about and tried to understand what is does, and applied but It doesn't not worked. I didn't understand what is going but It works now. – MegaBytes Oct 10 '14 at 10:24
1

Assumning you know what the INPUT result is and how it works, you are doing the wrong thing here.

When you perform an AJAX call, 1) the result will be parsed (and then eventually injected) in your current page, or, alternatively, you can use that result to perform a redirect by using javascript (window.location = "newUrl";).

You can't return a whole page and then use that response to make a new page (unless inside an iframe or similar, but that's DOM / page manipulation, then case 1).

Then this

<action name="savePersonalDetails" class="org.employee.actions.EmployeeMyProfileAction" method="updateEmployeeDetails">
    <result name="success">empMyProfile.jsp</result>
    <result name="input">emp-personal-form.jsp</result>
</action>

can't be right, because both the result should be an entire page (in case of a classic POST) or a JSP snippet / JSON / whatever (in case of an AJAX CALL).

You should change it to something like

<action name="savePersonalDetails" class="org.employee.actions.EmployeeMyProfileAction" method="updateEmployeeDetails">
    <result name="success">emp-personal-form.jsp</result>
    <result name="input">emp-personal-form.jsp</result>
    <result name="error">emp-personal-form.jsp</result>
</action>

and include in the first rows of emp-personal-form.jsp a message with the errors (in case of INPUT or ERROR results), or a message of success (in case of SUCCESS result) and then provide a link to navigate away from the page.

Otherwise, use standard POST and return the same page in case of INPUT or ERROR, or a success page in case of SUCCESS:

<action name="savePersonalDetails" class="org.employee.actions.EmployeeMyProfileAction" method="updateEmployeeDetails">
    <result name="success">profileCorrectlyUpdated.jsp</result>
    <result name="input">empMyProfile.jsp</result>
    <result name="error">empMyProfile.jsp</result>
</action>

But nothing prevent you to use standard POST and return in the same page also in case of SUCCESS.

Note: to know how display a (success or error) message only when needed, read this.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243