Problem in reading data into the grid

524 views
Skip to first unread message

SJ.Jafari

unread,
Aug 1, 2010, 9:24:29 AM8/1/10
to struts2-jquery
Hi
I've got just this tag in my index.jsp:
<s:url id="remoteurl" action="jsontable"/>

here is the struts action in my struts.xml file:
<action name="jsontable" class="strutsAction.JsonTable" >
<result type="json" name="success">ajax1.jsp</result>
</action>

refering to this Struts Action:
@Actions( { @Action(value = "/jsontable", results = { @Result(name =
"success", type = "json") }) })
public String execute() {

// Count Rows (select count(*) from costumer)
records = 2;
rows = 2;
// Your logic to search and select the required data.
gridModel = new ArrayList<Integer>();
gridModel.add(15);
gridModel.add(80);
// calculate the total pages for the query
total = (int) Math.ceil((double) records / (double) rows);
System.out.println("I'm a JSON Action ");
return SUCCESS;
}

public String getJSON() {
return execute();
}
And my Grid tag in the ajax1.jsp:

<sjg:grid
id="gridtable"
caption="Stocks Examples"
dataType="json"
href="%{remoteurl}"
pager="true"
gridModel="gridModel"
rowList="10,15,20"
rowNum="15"
rownumbers="true"
width="300"
height="300"
>
<sjg:gridColumn name="id" index="id" title="ID"
formatter="integer" sortable="false"/>
</sjg:grid>

I expect to see two rows in my one columned grid representing the
values: 15 and 80 as I set them in the Struts Action.
But What I get is two row both with value of 0.
What am I missing?

I'm getting confused understanding the logic of loading JSON Data into
the grid using Struts action, what happens at the end of execute? what
does it return? how the grid gets this data?

jogep

unread,
Aug 2, 2010, 2:08:29 PM8/2/10
to struts2-jquery
Why do you use a mix of Struts2 XML Config and Annotation based
Config?
One of this should be enough.

try to monitor your xhr network traffic with firebug to see if your
load
url is successfully.

Johannes
---
web: http://www.jgeppert.com
twitter: http://twitter.com/jogep

SJ.Jafari

unread,
Aug 7, 2010, 8:35:38 AM8/7/10
to struts2-jquery
Hi
Actually I just use Struts2 XML Config, I was testing other ways
above.
I checked the XML Http Request with firebug, it loads successfully and
the JSON result in response is this:

{"JSON":"success","gridModel":[15,80],"page":1,"records":2,"rows":
2,"searchField":null,"searchOper":null,"searchString":null,"sidx":"","sord":"asc","total":
1}

Again my Struts Action is:
public String execute() {

// Count Rows (select count(*) from costumer)
records = 2;
rows=2;
// Your logic to search and select the required data.
gridModel = new ArrayList<Integer>();
gridModel.add(15);
gridModel.add(80);
// calculate the total pages for the query
total = (int) Math.ceil((double) records / (double) rows);
System.out.println("I'm a JSON Action ");
return SUCCESS;
}

public String getJSON() {
return execute();
}


And here's the Grid in the index.jsp:

<s:url id="remoteurl" action="jsontable"/>
<sjg:grid
id="gridtable"
caption="Stocks Examples"
dataType="json"
href="%{remoteurl}"
pager="true"
gridModel="gridModel"

rowNum="2"
rownumbers="true"
>
<sjg:gridColumn name="id" index="id" title="ID"
formatter="integer" sortable="false"/>
</sjg:grid>

Finally the struts Config file:

<?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>
<constant name="struts.enable.DynamicMethodInvocation" value="true" /
>
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources"
value="ApplicationResources" />

<package name="default" extends="struts-default,json-default" >

<action name="jsontable" class="strutsAction.JsonTable" >
<result type="json" name="success"></result>
</action>

</package>
</struts>


nevertheless I get a grid with two 0 valued rows.
Waht's wrong?
Message has been deleted

jogep

unread,
Aug 8, 2010, 2:27:54 AM8/8/10
to struts2-jquery
your gridcolumn can only handle beans, every column must match a
getter method.

so try this:

1.) Create a Bean
public class Column {
private Integer id;

// constructor
public Column (int id) {
this.id = id;
}

// getter and setter
}

2.) Modify your Action

public String execute() {
// Count Rows (select count(*) from costumer)
records = 2;
rows=2;
// Your logic to search and select the required data.
gridModel = new ArrayList<Integer>();
gridModel.add(new Column(15));
gridModel.add(new Column(80));
// calculate the total pages for the query
total = (int) Math.ceil((double) records / (double)
rows);
System.out.println("I'm a JSON Action ");
return SUCCESS;
}
public String getJSON() {
return execute();
}

3.) JSP

<s:url id="remoteurl" action="jsontable"/>
<sjg:grid
id="gridtable"
caption="Stocks Examples"
dataType="json"
href="%{remoteurl}"
pager="true"
gridModel="gridModel"
rowNum="2"
rownumbers="true"
>
<sjg:gridColumn name="id" index="id" title="ID"
formatter="integer" sortable="false"/>
</sjg:grid>

SJ.Jafari

unread,
Aug 8, 2010, 12:13:58 PM8/8/10
to struts2-jquery
Thanks for the hint, It works fine this way.
Reply all
Reply to author
Forward
0 new messages