سؤال

Please Note, I am not a Java developer so my question may have some heart breaking assumptions & invalid piece of code.

I have an interceptor which takes care of login authentication.

<global-results>
   <result name="login">/admin/login.jsp</result>
</global-results>

As shown, it redirects to login.jsp when authentication fails i.e. the return value is "login" (in this case)

What I want to achieve is, I return some other value (say "loginAjax") and it should not send an HTTP 302, instead it should return back a JSON response.

I don't know if that possible at all or not. I have this other piece in struts.xml which returns json when a particular value is returned from Action.

<result name="success" type="json">
    <param name="includeProperties">
        return_id, return_message
    </param>
</result>

I don't know how should I tie both the pieces together and get it working for the same interceptor.

هل كانت مفيدة؟

المحلول

A global result is not tied to one or more Interceptors or Actions: it is global to everyone, so it will be rendered when returned, no matter who is returning it.

You need to add a global (keeping it consistent with your other login result, that is global) result of type json, like this:

<global-results>
    <result name="login">/admin/login.jsp</result>
    <result name="loginAjax" type="json" />
</global-results>

Then, you can send a particular statusCode, or errorCode, like described here.

If you need to understand what kind of result you should return from within the Interceptor, you should proceed like described in this other answer, by checking the type of the result that it would be returned in case of success.

P.S: Oh, and consider hiring a Java dev to do Java stuff properly :)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top