Struts2 Jquery网格无法正常工作

时间:2016-05-19 10:05:31

标签: java jquery jsp struts2 struts2-jquery-grid

我想显示网格但是当我执行jsp页面时,没有显示任何网格或任何数据...... JSP页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome !</title>


</head>
<body>
    <s:url id="remoteurl" action="listStudents.action" />
    <sjg:grid id="gridtable" caption="Students..." dataType="json"
        href="%{remoteurl}" pager="true" gridModel="listStudentUtils"
        rowNum="10" navigator="true" navigatorAdd="false"
        navigatorDelete="false" navigatorEdit="false" navigatorRefresh="true"
        navigatorSearch="false" navigatorView="false" rownumbers="true"
        rowList="10,20,30" viewrecords="true" autowidth="true">
        <sjg:gridColumn name="object.studentID" hidden="true" search="false"
            key="true" index="studentID" title="Student ID" sortable="false" />
        <sjg:gridColumn name="object.studentName" align="left"
            index="studentName" title="Student Name" sortable="false" />
        <sjg:gridColumn name="object.studentAddress" align="left"
            index="studentAddress" title="Student Address" sortable="false" />
    </sjg:grid>
</body>
</html>  

以下是我的动作类

package pckg.action;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class StudentAction extends ActionSupport {
    Student st;
    List<Student> listStudentUtils;

    public StudentAction() {
        // TODO Auto-generated constructor stub
    }

    public Student getSt() {
        return st;
    }

    public void setSt(Student st) {
        this.st = st;
    }

    public List<Student> getListStudentUtils() {
        return listStudentUtils;
    }

    public void setListStudentUtils(List<Student> listStudentUtils) {
        this.listStudentUtils = listStudentUtils;
    }

    @Override
    public String execute() throws Exception {
        String type = "";
        System.out.println("Into Action Class...");
        // TODO Auto-generated method stub
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/sample", "root", "siddheshkk");
            System.out.println("Driver Loaded");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from emp");
            listStudentUtils = new ArrayList<Student>();
            while (rs.next()) {
                listStudentUtils.add(new Student(rs.getString("name"), rs
                        .getString("password")));
            }
            System.out.println("Data Collected na Baa...!!");
            type = SUCCESS;
            con.close();
        } catch (Exception e) {
            System.out.println(e);
        }

        return type;
    }

}  

下面是我的struts.xml

<?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.devMode" value="true" />
    <constant name="struts.ui.theme" value="simple" />
    <package name="myPack" extends="struts-default,json-default">
        <interceptors>
            <interceptor-stack name="defaultStack">
                <interceptor-ref name="params">
                    <param name="acceptParamNames">
                        (\[\\d+\]\\.)*\\w+((\\.\\w+)|(\[\\d+\])|(\(\\d+\))|(\['\\w+'\])|(\('\\w+'\)))*
                    </param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <action name="listStudents" class="pckg.action.StudentAction">
            <interceptor-ref name="defaultStack" />
            <result name="success" type="json">index.jsp</result>
            <result name="error" type="json">error.jsp</result>
        </action>
    </package>
</struts> 

没有显示错误。它没有执行动作类。我添加了3个jar文件以及基本的struts2 jar文件。它们是:

struts2-jquery-grid-plugin-3.1.1.jar  
struts2-jquery-plugin-3.1.1.jar  
struts2-json-plugin-2.3.20.jar

1 个答案:

答案 0 :(得分:1)

我通过加载jquery标签解决了我的问题

<%@taglib uri="/struts-jquery-tags" prefix="sj" %>  

并在头标记下方放置

<sj:head jqueryui="true" jquerytheme="redmond" />