Struts2-Jquery Grid:数据未显示

时间:2012-01-24 08:08:09

标签: jquery struts2

我使用的是struts2-jquery网格。

我的操作类中的gridmodel正在正确更新。但是,网格上没有显示任何内容。

struts.xml中

<struts>

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

        <action name="listUsers" class="com.pilot.web.action.ListUsers" method="getJSON">
            <result name="success">/jsp/userList.jsp</result>
            <result name="input">/jsp/userList.jsp</result>
        </action>
</package> 

userList.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<sj:head jqueryui="true" jquerytheme="redmond"/>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
</head>
<body>
<h3>Application Users</h3>
<s:actionerror/>
<s:actionmessage/>
<s:url id="remoteurl" action="/searchAndUpdate/listUsers"/>
    <sjg:grid
        id="gridtable" 
        caption="Application Users" 
        dataType="json" 
        href="%{remoteurl}" 
        pager="true" 
        gridModel="gridModel" 
        rowList="10,15,20" 
        rowNum="15" 
        rownumbers="true" 
        resizable="true" 
        viewrecords="true" 
    >
        <sjg:gridColumn name="id" index="id" title="Id" formatter="integer" sortable="false"/>
        <sjg:gridColumn name="userName" index="userName" title="User Name" sortable="true"/>
        <sjg:gridColumn name="fullName" index="fullName" title="Full Name" sortable="false"/>
        <sjg:gridColumn name="email" index="email" title="EMail" sortable="false"/>
    </sjg:grid>
</body>
</html>

ListUsers.java

package com.pilot.web.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.oxxo.pilot.backend.jdbc.dao.impl.User;


public class ListUsers extends ActionSupport {
        private static final long serialVersionUID = 1L;
        private List<User> gridModel;
        //get how many rows we want to have into the grid - rowNum attribute in the grid
        private Integer rows = 0;
        //Get the requested page. By default grid sets this to 1.
        private Integer page = 0;
        // Your Total Pages
        private Integer total = 0;
        // All Records
        private Integer records = 0;
        // sorting order - asc or desc
        private String sord;
        // get index row - i.e. user click to sort.
        private String sidx;
        public String getAllApplicationData() {
                List<User> users = userListFromDb();
                if (users != null && getSord() != null
                                && getSord().equalsIgnoreCase("asc")) {
                        Collections.sort(users, null);
                }
                if (getSord() != null && "desc".equalsIgnoreCase(getSord())) {
                        Collections.sort(users, null);
                        Collections.reverse(users);
                }
                setRecord(users.size());
                int to = (getRows() * getPage());
                if (to > getRecord())
                        to = getRecord();

                setGridModel(users);
                setTotal((int) Math.ceil((double) getRecord() / (double) getRows()));
                System.out.println("Total is : " + getTotal());
                System.out.println("Record is : " + getRecord());
                for(User user: users){
                        System.out.println(user.getUserName() + ", " + user.getFullName() + ", " + user.getEmail());
                }
                if (hasActionMessages() || hasActionErrors()) {
                        return INPUT;
                }
                return SUCCESS;
        }

        private List<User> userListFromDb() {
           // MOCKED List for now
                List<User> users = new ArrayList<User>();
                User user1 = new User();
                user1.setId(1);
                user1.setUserName("user1");
                user1.setFullName("User ABC");
                user1.setEmail("user1@email.com");

                User user2 = new User();
                user2.setId(1);
                user2.setUserName("user2");
                user2.setFullName("User DEF");
                user2.setEmail("user2@email.com");

                users.add(user1);
                users.add(user2);
                return users; // TODO FETCH LIST OF USERS FROM DB
        }

        public String getJSON() {
                System.out.println("here in getJSON");
                return getAllApplicationData();
        }

        public Integer getRows() {
                return rows;
        }

        public void setRows(Integer rows) {
                this.rows = rows;
        }

        public Integer getPage() {
                return page;
        }

        public void setPage(Integer page) {
                this.page = page;
        }

        public Integer getTotal() {
                return total;
        }

        public void setTotal(Integer total) {
                this.total = total;
        }

        public Integer getRecord() {
                return records;
        }

        public void setRecord(Integer records) {
                this.records = records;
                if (this.records > 0 && this.rows > 0) {
                        this.total = (int) Math.ceil((double) this.records
                                        / (double) this.rows);
                } else {
                        this.total = 0;
                }
        }
        public List<User> getGridModel() {
                return gridModel;
        }
        public void setGridModel(List<User> gridModel) {
                this.gridModel = gridModel;
        }
        public String getSord() {
                return sord;
        }
        public void setSord(String sord) {
                this.sord = sord;
        }
        public String getSidx() {
                return sidx;
        }
        public void setSidx(String sidx) {
                this.sidx = sidx;
        }
        public void setSearchField(String searchField) {
        }

        public void setSearchString(String searchString) {
        }

        public void setSearchOper(String searchOper) {
        }

        public void setLoadonce(boolean loadonce) {
        }
        public void setSession(Map<String, Object> session) {
        }
}

我确实参考了struts2 jquery grid data not load?

上的解决方案

但是这个解决方案不起作用。它不是在网格上显示数据,而是提示保存文件&amp;该文件包含以下信息:

  

{&#34; JSON&#34;:&#34;成功&#34;&#34; gridModel&#34;:[{&#34;电子邮件&#34;:&#34; USER1 @电子邮件。 com&#34;,&#34; fullName&#34;:&#34;用户ABC&#34;,&#34; id&#34;:1,&#34; userName&#34;:&#34; user1&# 34;},{&#34;电子邮件&#34;:&#34; user2@email.com",&#34; fullName&#34;:&#34;用户DEF&#34;,&#34; ID&#34;:1,&#34;用户名&#34;:&#34;用户2&#34;}],&#34;页面&#34;:0,&#34;记录&#34;:2,& #34;行&#34;:0,&#34; SIDX&#34;:空,&#34; SORD&#34;:空,&#34;总&#34;:2147483647} *

根据我的理解排和&amp;页面由插件自动处理。但是根据上面的json数据,它们并没有增加。将它们硬编码为gridmodel的大小,我得到的json数据是:

  

{&#34; JSON&#34;:&#34;成功&#34;&#34; allApplicationData&#34;:&#34;成功&#34;&#34; gridModel&#34 ;:[{&#34; email&#34;:&#34; user1@email.com",&#34; fullName&#34;:&#34;用户ABC&#34;,&#34; id& #34;:1,&#34;用户名&#34;:&#34;用户1&#34;},{&#34;电子邮件&#34;:&#34; user2@email.com",&# 34; fullName&#34;:&#34;用户DEF&#34;,&#34; id&#34;:1,&#34; userName&#34;:&#34; user2&#34;}],&# 34;页面&#34;:1,&#34;记录&#34;:2&#34;行&#34;:2&#34; SIDX&#34;:空,&#34; SORD&#34; :空,&#34;总&#34;:1}

将struts.xml更新为

 <result-types>
        <result-type name="json" class="org.apache.struts2.json.JSONResult" />
        </result-types>
        <action name="listUsers" class="com.oxxo.pilot.web.action.ListUsers">
            <result name="success" type="json">/jsp/userList.jsp</result>
            <result name="input" type="json">/jsp/userList.jsp</result>
        </action>

json数据显示在浏览器上。

谢谢, Shikha

3 个答案:

答案 0 :(得分:3)

正确的解决方案是:

<强> struts.xml中:

<result-types>
            <result-type name="json" class="org.apache.struts2.json.JSONResult" />
        </result-types>

<action name="listUsers" method="getAllApplicationData"   class="com.pilot.web.action.ListUsers">
            <result name="success">/jsp/userList.jsp</result>
            <result name="input">/jsp/fail.jsp</result>
        </action>
        <action name="getData" method="getAllApplicationData"   class="com.pilot.web.action.ListUsers">
            <result name="success" type="json"></result>
        </action>

<强>的index.jsp

<s:form action="/searchAndUpdate/listUsers" method="post">
        <s:submit name="ListUsers" value="List All Users" align="right" />
    </s:form>

<强> userList.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<sj:head jqueryui="true" jquerytheme="redmond"/>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
</head>
<body>
<h3>Application Users</h3>
<s:actionerror/>
<s:actionmessage/>
<s:url id="remoteurl" action="/searchAndUpdate/getData"/>
    <sjg:grid
        id="gridtable" 
        caption="Application Users" 
        href="%{remoteurl}" 
        pager="true" 
        gridModel="gridModel" 
        rowList="10,15,20" 
        rowNum="15" 
        rownumbers="true" 
        resizable="true" 
        viewrecords="true" 
        name="gridModel" 
    >
        <sjg:gridColumn name="id" index="id" title="Id" formatter="integer" sortable="false"/>
        <sjg:gridColumn name="userName" index="userName" title="User Name" sortable="true"/>
        <sjg:gridColumn name="fullName" index="fullName" title="Full Name" sortable="false"/>
        <sjg:gridColumn name="email" index="email" title="EMail" sortable="false"/>
    </sjg:grid>
</body>
</html>

这是一个有效的代码。工作得非常好:))

问题是: jsp接受数据类型json作为输入&amp; ListUsers的动作必须呈现一个jsp。所以,我做了两个不同的动作。一个返回“json”,作为jsp&amp;的输入。一个使jsp成功的一个。

答案 1 :(得分:0)

动作应返回JSON。

 <action name="listUsers" class="com.pilot.web.action.ListUsers" method="getJSON">
            <result name="success" type="json"/>
        </action>

You can find a few grid examples here.

答案 2 :(得分:0)

尝试在动作类中使用2个返回参数的方法。如果结果名称是INPUT,则应返回正常参数。当您查询网格值时,您应该返回JSON。我的意思是将你的行动结果改为:

<action name="listUsers_*" method={1} class="com.oxxo.pilot.web.action.ListUsers">
    <result name="success" type="json">/jsp/userList.jsp</result>
    <result name="input">/jsp/userList.jsp</result>
</action>

在你的动作类中

public String execute() throws Exception { return INPUT;}
public String getJSON() { return getAllApplicationData();}

我不知道execute()方法在未实现时是否正常返回INPUT

在您的页面中,添加getJSON以在您的操作类上调用getJSON方法。

<s:url id="remoteurl" action="listUsers_getJSON"/>

希望这有帮助,如果你解决了,请告诉我