这篇文章在前边项目的基础上来整合json,最新项目资料见:http://download.csdn.net/detail/u012116457/8436187

首先需要的jar包:jackson-core-asl-1.7.2.jar  jackson-mapper-asl-1.7.2.jar

然后是配置文件json-servlet.xml,该文件得在web.xml中配置使其在tomcat启动时调用:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 配置对Json的支持 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<!-- 为了处理返回的JSON数据的编码,默认是ISO-88859-1的,这里把它设置为UTF-8,解决有乱码的情况 -->
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean> </beans>

控制类如下:

package module.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import module.entity.User;
import module.service.LoginService; import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/login")
public class LoginController { private static String LOGINSUCCESS="module/jsp/index"; //@Autowired() @Qualifier("loginServiceImpl2")
@Resource(name="loginServiceImpl2")
private LoginService loginService; @RequestMapping(value = "/login.do", method = RequestMethod.POST)
@ResponseBody //此注解表明返回值跳过视图处理部分,直接写入 http response body中
public User login(User user) {
System.out.println("用户:"+user.getName()+" 请求登陆");
User u = loginService.findByName(user.getName());
if(u==null){
u=new User();
u.setName("");
}
return u;
} @RequestMapping("/loginSuccess.do")
public String start(HttpServletRequest request){
return LOGINSUCCESS;
}
}

在spring中,如果使用了@RequestMapping注解,会根据该方法的返回值映射到实际的url,如果同时还使用了@ResponseBody注解,则会跳过视图处理部分,将返回值直接写入http response body中,将其返回到客户端页面。如上边的代码,会将User格式的数据自动转换为json格式返回到客户端。

简单来说,如果需要返回json数据,则用@ResponseBody来注解该方法,返回类型为实体类即可。

下面是jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML >
<html>
<head> <title>登陆</title>
<script type="text/javascript" src="<%=path %>/js/common/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8"> function login(){ $.ajax({
url:'login/login.do',
data:{
name:document.getElementById("name").value,
password:document.getElementById("password").value
},
type:'POST',
dataType:'json',
success:function(data){
alert(data.name);
if(data.name!="")
window.location.href ='http://localhost:8080/springMVC/login/loginSuccess.do';
else
alert("用户名或密码错误");
},
error:function(){
alert("error");
}
});
} </script>
</head>
<body>
<center>
<form>
姓名:<input type="text" name="name" id="name"><br/>
密码:<input type="password" name="password" id="password"></br>
<button type="button" onclick="login()">登陆</button>
</form>
</center>
</body>
</html>

版权声明:本文为博主原创文章,未经博主允许不得转载。

玩转spring MVC(八)----spring MVC整合json的更多相关文章

  1. spring入门(八) spring mvc设置默认首页

    1.web.xml配置如下 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...

  2. Spring学习(八)-----Spring注入值到集合类型的例子

    下面例子向您展示Spring如何注入值到集合类型(List, Set, Map, and Properties). 支持4个主要的集合类型: List – <list/> Set – &l ...

  3. 玩转单元测试之Testing Spring MVC Controllers

    玩转单元测试之 Testing Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/4311657.html The Spri ...

  4. SpringMVC系列(十五)Spring MVC与Spring整合时实例被创建两次的解决方案以及Spring 的 IOC 容器和 SpringMVC 的 IOC 容器的关系

    一.Spring MVC与Spring整合时实例被创建两次的解决方案 1.问题产生的原因 Spring MVC的配置文件和Spring的配置文件里面都使用了扫描注解<context:compon ...

  5. 2017.3.31 spring mvc教程(八) <mvc:annotation-driven />所做的工作

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  6. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:SSM(Spring+Spring MVC+MyBatis)框架整合搭建详细步骤

    因为 Spring MVC 是 Spring 框架中的一个子模块,所以 Spring 与 SpringMVC 之间不存在整合的问题.实际上,SSM 框架的整合只涉及 Spring 与 MyBatis ...

  7. springboot学习笔记:5.spring mvc(含FreeMarker+layui整合)

    Spring Web MVC框架(通常简称为"Spring MVC")是一个富"模型,视图,控制器"的web框架. Spring MVC允许你创建特定的@Con ...

  8. Spring MVC + Spring MongoDB + Querydsl 通过maven整合实例

    效果图 一共3个页面:注册页,欢迎页,用户列表页 很简单的例子,主要是为了把流程走通,没有各种验证. 注册页: 欢迎页: 用户列表页: 源码地址 https://github.com/lemonbar ...

  9. Spring MVC 和 Spring 总结

    1. 为什么使用Spring ? 1). 方便解耦,简化开发 通过Spring提供的IoC容器,可以将对象之间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合. 2). AOP编程的 ...

随机推荐

  1. 【39】FlexboxLayout使用介绍

    FlexboxLayout介绍: Flexbox 也称为弹性盒子模型 或伸缩盒子模型,广泛用于前端开发,做过前端 web 的都知道Bootstrap 中有一套强大的 CSS Grid网格样式.Boot ...

  2. rails4 中使用分页的方法

    以前老版本的rails中默认自带分页方法,不过从rails2.0开始就将内置的分页pagination对象移除了,改以第三方gem提供支持.要在新的rails里使用分页也是非常简单啦,首先安装will ...

  3. 恶补web之六:javascript知识(2)

    若要向html添加新元素,必须首先创建该元素,然后向一个已存在的元素追加该元素 <div id="div1"> <p id="p1">这 ...

  4. Unity的资源管理

    本篇文章翻译自Unity的官方文档,原文地址:https://docs.unity3d.com/Manual/BehindtheScenes.html Unity自动导入资源的同时为你管理这些资源产生 ...

  5. Jquery Easing函数库

    从jQuery API 文档中可以知道,jQuery自定义动画的函数.animate( properties [, duration] [, easing] [, complete] )有四个参数: ...

  6. R实战 第七篇:绘图文本表

    文本表是显示数据的重要图形,一个文本表按照区域划分为:列标题,行标题,数据区,美学特征有:前景样式.背景央视.字体.网格线等. 一,使用ggtexttable绘图文本表 载入ggpubr包,可以使用g ...

  7. 接口文档神器之apidoc

    //@desn:apidoc linux环境  windows环境使用 //@desn:码字不宜,转载请注明出处 //@author:张慧源  <turing_zhy@163.com> / ...

  8. Golang之Context的使用

    转载自:http://www.nljb.net/default/Golang%E4%B9%8BContext%E7%9A%84%E4%BD%BF%E7%94%A8/ 简介 在golang中的创建一个新 ...

  9. (转) windows下 安装 rabbitMQ 及操作常用命令

    该博客转载自:https://blog.csdn.net/gy__my/article/details/78295943 原作者:Eric Li  出处:http://www.cnblogs.com/ ...

  10. 2010_3_1最新 完整 FFMPEG 编译详解

    在网上看了很多编译详解,都很零散.经过自己的编译,解决一些BUG,在此分享自己的一些经验... 话不多说了!直接上贴. 第一步:准备编译平台. 需要 一个 MinGW 和 一个 MSYS 安装包 以及 ...