0

I am following the tutorial on struts 2 at http://www.simplecodestuffs.com/integrating-jquery-datatable-with-struts2-using-ajax-to-implement-gridview/

I understood the core concepts like using POJO action classes struts.xml configurations etc. But whats troubling me is in this tutorial, there is a jsp result mentioned in struts.xml called " grid.jsp". But this page is not at all present in the application and it still works.

How can this happen? does struts 2 generate some jsp templates? Please check the attached screenshot. It has the project structure and struts.xml.

I have tried various books and tutorials and google searches but this topic has never been discussed. Please help. I would like to learn the flow from when the request reaches the action class.

These kind of examples are also present in struts2 showcase.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="default" extends="json-default">
        <action name="dataTablesAction" class="com.action.GridViewAction">
            <result type="json">grid.jsp</result>
        </action>
    </package>

</struts>

Project structure and struts.xml

DJR
  • 454
  • 2
  • 8
  • 29
  • Did you check the WEB-INF directory? – realUser404 Dec 03 '14 at 21:21
  • yes. i checked all directories in the project. You can also download the project from the URL provided in the post. – DJR Dec 03 '14 at 21:23
  • When it says *"Now create the jsp file to display the data..."* it means this file – zapl Dec 03 '14 at 21:23
  • no. thats the index.jsp file you see in the screenshot. – DJR Dec 03 '14 at 21:24
  • as a matter of fact.. if you change the name " grid.jsp" in struts.xml to bird.jsp.. which dosent exist for sure, it still works.. it works even if you take out the jsp name completely also. – DJR Dec 03 '14 at 21:26
  • The tutorial indeed doesn't explain much it seems. For better tuto, I strongly recommend following website : http://www.mkyong.com/tutorials/struts-2-tutorials/ – realUser404 Dec 03 '14 at 21:26
  • i have gone thru numerous tutorials but didnt find a direct reference to this technique. i think its a technique enabled by the use of struts 2 framework because even stru2 showcase examples have this kinda configurations for their examples – DJR Dec 03 '14 at 21:27
  • I really doubt so :) My guess is you action defined in struts.xml isn't even called – realUser404 Dec 03 '14 at 21:30
  • let me debug and test it.. at first thought its impossible because there is data coming from that action class. – DJR Dec 03 '14 at 21:35
  • 3
    The tutorial is "wrong", all you need to specify is `` any text in there will probably just be ignored because the json thingy does not expect any text there. See "Write the mapping for the action" section in http://struts.apache.org/release/2.2.x/docs/json-plugin.html – zapl Dec 03 '14 at 21:39
  • nice.. thanks for the direction. so if i declare a result type as json, i cannot give a jsp name to forward it to? – DJR Dec 03 '14 at 21:45
  • could you please explain it in a few lines.. that reference link has loads of other things i dont need. im sure others will also find it useful if the answer is here in a short and sweet format – DJR Dec 03 '14 at 21:46
  • http://struts.apache.org/release/2.3.x/docs/json-plugin.html – Pravin Dec 03 '14 at 21:49
  • 1
    From what I've gathered (I've actually never used struts or similar): `sAjaxSource` in the index jsp's script part results in an AJAX http request to your server ( basically like http://api.jquery.com/jquery.getjson/ ) with an url that contains `dataTablesAction`. The server maps that to the action class which produces json in the end. That json then arrives back in your script to update the table. Ajax isn't redirecting, it's an asynchronous update of the current page, wikipedia should have more about that. So no need to specify a jsp. – zapl Dec 03 '14 at 22:02
  • @zapl consider posting your comments in an answer, then I'll delete mine – Andrea Ligios Dec 03 '14 at 23:31
  • @AndreaLigios +1 for doing the work for me :) Your answer also contains information I didn't know (well find in google, I have really never done any web developing). – zapl Dec 03 '14 at 23:42
  • @zapl thanks man.. clear explanation – DJR Dec 04 '14 at 06:31

1 Answers1

1

@zapl already answered you, I'll delete this answer if he will post his comments as an answer.

When you write

 <result type="json">grid.jsp</result>

it will be translated to

 <result name="success" type="json"></result>

"success" will be added because of Intelligent Default ("success" is taken if no name specified) and the entire Action will be serialized into JSON, because that's how the JSON plugin works. You can specify a root object, but you can't specify a JSP, because JSP != JSON, and because raw text (like a jsp name) is not expected to be found there in the config. Your grid.jsp will be simply ignored and cropped at all (not tested, but I'm pretty sure).

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