这篇文章在前边项目的基础上来整合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. HBase 二级索引与Join

    二级索引与索引Join是Online业务系统要求存储引擎提供的基本特性.RDBMS支持得比较好,NOSQL阵营也在摸索着符合自身特点的最佳解决方案. 这篇文章会以HBase做为对象来探讨如何基于Hba ...

  2. Log4j.properties 配置详解

    一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使 ...

  3. ubuntu安装最新的rails-4.2.0

    完全按照教程来,可是错误不断,还是边装边baidu吧! sudo gem install rails 安装了一大坨关联gem之后,终于好了.于是想小试一下身手,新建文件夹rails_test,cd进入 ...

  4. 使用Interlocked在多线程下进行原子操作,无锁无阻塞的实现线程运行状态判断

    巧妙地使用Interlocked的各个方法,再无锁无阻塞的情况下判断出所有线程的运行完成状态. 昨晚耐着性子看完了clr via c#的第29章<<基元线程同步构造>>,尽管这 ...

  5. MongoDB学习笔记(四)

    第四章 Mongodb聚合函数 插入 测试数据 for(var j=1;j<3;j++){ for(var i=1;i<3;i++){ var person={ Name:"ja ...

  6. Maximum Subarray(最大子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. java——多态

    多态定义:某一类事物的多种存在形态.对象的多态性.猫这类事物即具备猫的形态,又具备着动物的形态,这就是对象的多态性.简单说:就是一个对象对应着不同类型.多态在代码中的体现:父类或者接口的引用指向其子类 ...

  8. JavaScript中对象数组 根据某个属性值进行排序

    将下列对象数组,通过工资属性,由高到低排序 var BaiduUsers = [], WechatUsers = []; var User = function(id, name, phone, ge ...

  9. 彪悍开源的分析数据库-ClickHouse

    https://zhuanlan.zhihu.com/p/22165241 今天介绍一个来自俄罗斯的凶猛彪悍的分析数据库:ClickHouse,它是今年6月开源,俄语社区为主,好酒不怕巷子深. 本文内 ...

  10. SQLServer Merger Using语法使用和注意点

    SQL多表关联数据更新,如果数据量比较少的情况下,用Update也是可以的:脚本如下: UPDATE NA_AgentGrpOrder SET AttrServSIItem=b.AttrValue F ...