1

I'm trying to implement struts2-jquery grid tag in my application but I'm not familiar with JSON so have some trouble with this process.

What is wrong?

Original example uses annotation on action class @Result(name = "success", type = "json") but I'm using XML configuration:

   <package name="default" namespace="/" extends="struts-default">
   ...
   </package>
   <package name="showcase" extends="struts-default, json-default" namespace="/">
        <action name="jgrid" class="com.user.action.GridDataProvider" method="execute" > //line 106
            <result name="success" type="json">/tabs.jsp</result>//line 107
        </action>
   </package>

Result:

Unable to load configuration. - action -
file:/C:/struts2worksp/Struts2HiberQuize_4/target/classes/struts.xml:106:84
... Caused by: Unable to load configuration. - action -
file:/C:/struts2worksp/Struts2HiberQuize_4/target/classes/struts.xml:106:84
... Caused by: There is no result type defined for type 'json' mapped
with name 'success'.  Did you mean 'json'? - result -
file:/C:/struts2worksp/Struts2HiberQuize_4/target/classes/struts.xml:107:45

From Action class:

public String execute() {
        log.debug("Page " + getPage() + " Rows " + getRows()
                + " Sorting Order " + getSord() + " Index Row :" + getSidx());
        log.debug("Search :" + searchField + " " + searchOper + " "
                + searchString);

        Object list = session.get("mylist");
        if (list != null) {
            myCustomers = (List<Customer>) list;
        } else {
            log.debug("Build new List");
            myCustomers = new CustomerDAO().getList();
        }

        if (sord != null && sord.equalsIgnoreCase("asc")) {
//          Collections.sort(myCustomers);
        }
        if (sord != null && sord.equalsIgnoreCase("desc")) {
//          Collections.sort(myCustomers);
//          Collections.reverse(myCustomers);
        }

        // Count all record (select count(*) from your_custumers)
        records = CustomerDAO.getCustomersCount(myCustomers);

        if (totalrows != null) {
            records = totalrows;
        }

        // Calucalate until rows ware selected
        int to = (rows * page);

        // Calculate the first row to read
        int from = to - rows;

        // Set to = max rows
        if (to > records)
            to = records;

        if (loadonce) {
            if (totalrows != null && totalrows > 0) {
                setGridModel(myCustomers.subList(0, totalrows));
            } else {
                // All Custumer
                setGridModel(myCustomers);
            }
        } else {
            // Search Custumers
            if (searchString != null && searchOper != null) {
                int id = Integer.parseInt(searchString);
                if (searchOper.equalsIgnoreCase("eq")) {
                    log.debug("search id equals " + id);
                    List<Customer> cList = new ArrayList<Customer>();
                    Customer customer = CustomerDAO.findById(myCustomers, id);
                    

                    if (customer != null)
                        cList.add(customer);

                    setGridModel(cList);
                } else if (searchOper.equalsIgnoreCase("ne")) {
                    log.debug("search id not " + id);
//                  setGridModel(CustomerDAO.findNotById(myCustomers, id, from, to));
                } else if (searchOper.equalsIgnoreCase("lt")) {
                    log.debug("search id lesser then " + id);
//                  setGridModel(CustomerDAO.findLesserAsId(myCustomers, id, from, to));
                } else if (searchOper.equalsIgnoreCase("gt")) {
                    log.debug("search id greater then " + id);
//                  setGridModel(CustomerDAO.findGreaterAsId(myCustomers, id, from, to));
                }
            } else {
//              setGridModel(CustomerDAO.getCustomers(myCustomers, from, to));
            }
        }

        // Calculate total Pages
        total = (int) Math.ceil((double) records / (double) rows);

        // only for showcase functionality, don't do this in production
        session.put("mylist", myCustomers);

        return SUCCESS;
    }

JSP:

<s:url id="remoteurl" action="jgrid" namespace="/grid"/>
    <sjg:grid
        id="gridtable"
        caption="Customer Examples"
        dataType="json"
        href="%{remoteurl}"
        pager="true"
        gridModel="gridModel"
        rowList="5,10"
        rowNum="5"
        rownumbers="true"
    >
        <sjg:gridColumn name="id" index="id" title="ID" formatter="integer" sortable="false"/>
        <sjg:gridColumn name="name" index="name" title="Name" sortable="true"/>
        <sjg:gridColumn name="country" index="country" title="Country" sortable="false"/>
        <sjg:gridColumn name="city" index="city" title="City" sortable="false"/>
        <sjg:gridColumn name="creditLimit" index="creditLimit" title="Credit Limit" formatter="currency" sortable="false"/>
    </sjg:grid>
Roman C
  • 49,761
  • 33
  • 66
  • 176
andy007
  • 907
  • 1
  • 15
  • 41

1 Answers1

1

You have loading wrong configuration file struts.xml. The json result type is defined by the struts2-json plugin in the package json-default.

If you are using this type for results in your package you should either extend the package where this result type is defined or define this result type in the package that contains results with that type.

The json result doesn't have a default attribute, so you shouldn't use it. The body of the tag can be used for different parameters used by this result.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Pakage "showcase" in my example extends "struts-default" pakage and as you say this already defines Json resut type. And as I understand it, it need to change the line of cofig to this /tabs.jsp ? – andy007 Sep 07 '14 at 13:51
  • 1
    *The json result type is defined by the struts2-json plugin in the package `json-default`*. if you remove the `type="json"` the grid won't work, but if you remove `/tabs.jsp` from the result it would be right. – Roman C Sep 07 '14 at 14:57
  • if you remove the jsp, it does not change the situation, the errors are the same. struts speaks what cannot load configuration. but if you remove type="json", it starts without errors and even displays a page with an empty grid. – andy007 Sep 07 '14 at 19:17
  • Are you sure you have `json-default` package is loaded? – Roman C Sep 07 '14 at 20:34
  • project's pom contains ` es.cenobit.struts2.json struts2-json-plugin 2.3.16` – andy007 Sep 07 '14 at 20:43
  • 1
    You can also use group `org.apache.struts`. – Roman C Sep 07 '14 at 20:53
  • 1
    thanks. after changed the dependency, the project began working as should! – andy007 Sep 07 '14 at 21:42