wh0
wh0

Reputation: 289

making flow from AJAX call to Action class and then to a jsp page which return html to AJAX as response in struts2

I'm very new to struts.This is what i'm trying to achieve.

JavaScript function written in JSP-1 fires an AJAX which uses action class to get data from the database and jsp-2 uses data from action class and forms certain elements using struts tags and outputs this HTML data to JavaScript function which add HTML data to the JSP-1.

enter image description here

Now, the reason i'm using JSP-2 is to make the HTML data using struts tags. JSP-2 just acts like a function which forms HTML data for JSP-1 which will never be displayed. I know normal flow in which action class returns a JSON object to ajax but i'm not able to figure out how to do the middle JSP-2 page.

This is my struts.xml for jsp to action and action to ajax flow.

<action name="ajaxAction" class="ActionClass">
            <result name="success" type="json"/>
 </action>

My question here is how to go from action class to JSP-2 and then response to AJAX ?.

Upvotes: 0

Views: 3185

Answers (3)

chad
chad

Reputation: 7537

There's no need to think about JSP-1. It was only the delivery mechanism for your Ajax application. The question is what kind of response the Ajax request you are making needs. Does your Javascript Ajax client want a JSON response or an HTML fragment?

If it wants json, then I don't see the need for JSP-2 ( though you can create json with a jsp -- but there's little reason to do so when struts provides the json result type to do that for you ).

If it needs a HTML fragment, the best way to create that is with a JSP, presumable your jsp-2. If this is the case, you need to change the result type to "dispatcher", which is the default type actually, i.e. you don't need to specify it.

<action name="ajaxAction" class="ActionClass">
            <result name="success">/path/to/jsp-2</result>
 </action>

Upvotes: 0

Ash
Ash

Reputation: 1649

If you are using jquery, you can do it like this

$(document).ready(function(){
    var url ="MyAjaxAction.action";
    $("#sectionWhereJSP2WillbeThere").load(url);  
 });

And in your struts action class, just use normal success (i.e. treat as normal action)

Upvotes: 1

Aleksandr M
Aleksandr M

Reputation: 24406

So use dispatcher result type not json:

<action name="ajaxAction" class="ActionClass">
   <result name="success">JSP-2</result>
</action>

Upvotes: 0

Related Questions