SpringMVC与JQuery EasyUI DataGrid传递JSON数据

本文默认您会使用EasyUI, 仅仅讲解如何通过SpringMVC与EasyUI DataGrid传递数据。
思路是:Handler Method返回JSONObject对象,SpringMVC对此对象进行转换。

Jar包

以下所有Jar包,均可以在我的CSDN资源里下载

首先是Java操作json的类的相关jar包:

  1. commons-beanutils-1.9.2.jar
  2. commons-collections-3.2.1.jar
  3. commons-lang-2.4.jar
  4. commons-logging-1.1.3.jar’
  5. ezmorph-1.0.6.jar
  6. json-lib-2.3-jdk15.jar

Spring MVC 结果转换jar包:

  1. jackson-annotations-2.8.0
  2. jackson-core-2.8.1
  3. jackson-databind-2.8.7

配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/tx">

    <!-- 组件扫描 只扫描action -->
    <context:component-scan base-package="cn.caipengbo.action" />
    <!--注解映射器 -->
     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
    <!--注解适配器 -->
    <bean  class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <!-- 添加对JSON的支持-->
        <list>
            <bean
                  class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
        </list>
    </property>
    </bean>

    <!-- 视图解析器 解析jsp视图-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 逻辑视图名 -->
        <!-- 视图的前缀, 路径的前缀 -->
        <property name="prefix" value="/WEB-INF/pages/" />
        <!-- 视图的后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

POJO

package cn.caipengbo.domain;

/**
 * Created by Myth on 3/3/2017.
 */
public class Student {
    private String name;
    private int age;
    public Student(String name,int age) {this.name = name; this.age = age;}

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

控制器类

@Controller
public class Json {
    @RequestMapping("/outputJson")
    @ResponseBody
    public Object outputJson() {
        Student user1 = new Student("test1",10);
        Student user2 = new Student("test2",20);
        List<Student> users = new ArrayList<Student>();
        Map<String,Object> jsonMap = new HashMap<String,Object>();
        users.add(user1);
        users.add(user2);
        jsonMap.put("rows",users);
        jsonMap.put("total",users.size());
        JSONObject jsonObject = JSONObject.fromObject(jsonMap);
        System.out.println(jsonObject);
        return jsonObject;
    }
}

Web前端

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>测试JSON</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/style/easyui.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/style/icon.css">
    <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.easyui.min.js"></script>
</head>
<body>
<table id="dg" title="My Users" class="easyui-datagrid" style="width:550px;height:250px"
       url="${pageContext.request.contextPath }/outputJson.action"
       rownumbers="true"
       fitColumns="true"
       singleSelect="true">
    <thead>
    <tr>
        <th field="name" width="50">Name</th>
        <th field="age" width="50">Age</th>
    </tr>
    </thead>
</table>
</html>

测试结果

这里写图片描述

相关代码您也可以去我的GitHub上下载,如有其它疑问,欢迎留言!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用 Spring MVC 时,可以通过编写配置文件来配置应用程序。以下是一个示例,展示如何编写一个简单的 Spring MVC 配置文件,并在其中返回 JSON 数据。 首先,创建一个名为 `springmvc-servlet.xml` 的配置文件,并将其放置在 `WEB-INF` 目录下(或者根据你的项目结构进行调整)。 ```xml <!-- springmvc-servlet.xml --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 启用 Spring MVC 注解驱动 --> <mvc:annotation-driven/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 配置控制器 --> <bean class="com.example.controller.UserController"/> <!-- 配置 JSON 转换器 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven> </beans> ``` 在上面的配置文件中,我们首先启用了 Spring MVC 的注解驱动,以便能够使用 `@Controller`、`@RequestMapping` 等注解。 然后,配置了一个视图解析器,用于解析视图名称并返回相应的 JSP 视图。 接下来,我们配置了一个控制器,这里是 `com.example.controller.UserController`,你需要根据你的项目结构和控制器的位置进行调整。 最后,我们配置了一个 JSON 转换器,这里使用了 Spring 提供的 `MappingJackson2HttpMessageConverter` 类来进行 JSON 的序列化和反序列化。 接下来,在编写控制器时,你可以使用 `@ResponseBody` 注解将 JSON 数据返回给前端,就像之前提到的示例代码一样。 希望这个示例能帮助你理解如何编写 Spring MVC 配置文件并返回 JSON 数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值