java - 没有为使用 Struts 2 jQuery Grid 映射到名称 'json' 的类型 'success' 定义结果类型

标签 java json struts2 struts2-json-plugin

我正在尝试在我的应用程序中实现 struts2-jquery 网格标记,但我不熟悉 JSON,因此此过程遇到一些问题。

出了什么问题?

原始示例在操作类上使用注释 @Result(name = "success", type = "json") 但我使用的是 XML 配置:

   <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>

结果:

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

来自 Action 类:

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>

最佳答案

您加载了错误的配置文件struts.xmljson 结果类型由 struts2-json plugin 定义。在包 json-default 中。

如果您在包中使用此类型作为结果,则应扩展定义此结果类型的包,或在包含该类型结果的包中定义此结果类型。

json 结果没有默认属性,因此您不应使用它。标记的主体可用于此结果使用的不同参数。

关于java - 没有为使用 Struts 2 jQuery Grid 映射到名称 'json' 的类型 'success' 定义结果类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25708025/

相关文章:

java - 优雅的分支(任何语言)

json - 在 go 中覆盖结构标签

jquery - 设置 struts 2 自动完成器的默认值

java - Struts2性能问题

java - 从输入文件读取数字时出错

java - 使用连接或嵌套子查询更新 Hibernate (HQL)

java - JList 上 Transferable 和 Transferhandler 的自定义删除容器

javascript - 使用外部json数据在canvas js中渲染图形

java - 将动态json文件解析为对象的hashmap

java - 如何使用struts2和hibernate从数据库检索实例并显示它?