java smartcare,Spring MVC 3.2.2 +easyui 返回JSON数据格式

最近花了很多时间,研究Spring MVC 3.2.2 +easyui,实现了这个,前端页面实现个系统就很简单了。

Spring MVC 返回JSON数据的方法

1). 直接 PrintWriter 输出

2). 使用 JSP 视图

3). 使用Spring内置的支持

本文介绍的是第3种方法,方法如下:

1:首先下载JSON的包

这是spring MVC处理json数据时,所必须的jar依赖。

2:spring mvc的配置文件中加入配置

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

说明:

org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter的Bean配置中,我们加入了messageConverters属性,在该属性中我们有配置jsonHttpMessageConverter这个Bean,它就是用来处理json数据转换的。

在jsonHttpMessageConverter的Bean配置中,有一个supportedMediaTypes属性,在这个属性可以添加了text/html;charset=UTF-8这个值,它是为了处理返回的json数据的编码,默认是ISO-88859-1的,如果出现乱码,可以把它设置为UTF-8,参考如下:

text/html;charset=UTF-8

3:控制器Controller的注解

package com.kingmed.jusmartcare.health.web;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

public class MemberShipController {

public static final String LOGIN_SUCCESS = "vipcontent";//Index";// 登陆成功

public static final String LOGIN_FAIL = "../../index";//"LoginFail";// 登陆失败

public static final String LOGOUT = "../../index";//

public static final String REGISTER_SUCCESS = "RegisterSuccess";// 注册成功

public static final String REGISTER_FAIL = "RegisterFail";// 注册成功

private static final Logger logger=LoggerFactory.getLogger(MemberShipController.class);

@Autowired

private HealthconsultService healthconsultService;

@RequestMapping("jsp/welcome.do")

public String welcom(@RequestParam(value = "ln", required = false) String ln) {

String en = ln==null?"":ln+'/';

return en+"welcome";

}

@RequestMapping(value="user/list.do",method=RequestMethod.GET)

public String list(Model model) throws Exception {

return "user/list";

}

@RequestMapping(value="jsp/user/queryList.do")

@ResponseBody

public Map queryList(HttpSession session,@RequestParam(value = "pageNo", required = false) Integer pageNo,@RequestParam(value = "ln", required = false) String ln) throws Exception{

//spring太方便了,可以自动装配两个对象  会自动的装返回的Map转换成JSON对象

Map result = new HashMap(2);

String en = ln==null?"":ln+'/';

pageNo = pageNo==null?1:pageNo;

Page healths=healthconsultService.getPageList("cardno","1001", pageNo,2);

int total=healths.getTotalCount();

result.put("total", total);

result.put("rows", healths.getResult());

return result;

}

@RequestMapping(value="jsp/welcome1.do")

@ResponseBody

public Object test(HttpSession session){

System.out.println("test....................");

MemberInfo logined=(MemberInfo)session.getAttribute("USER_INFO");

if(logined ==null){

logined=new MemberInfo();

logined.setCardNo("999999");

System.out.println("test1....................");

}

System.out.println("test2....................");

return  logined;

}

}

说明:使用了一个@ResponseBody的注解,Spring3.0 MVC @ResponseBody的作用是把返回值直接写到HTTP response body里,

返回json数据的可以是一个类(看test),或者list(看queryList)。

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值