这篇文章在前边项目的基础上来整合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. Mybatis源码之RoutingStatementHandler

    /** * @author Clinton Begin */ public class RoutingStatementHandler implements StatementHandler { pr ...

  2. Linux input子系统分析

    输入输出是用户和产品交互的手段,因此输入驱动开发在Linux驱动开发中很常见.同时,input子系统的分层架构思想在Linux驱动设计中极具代表性和先进性,因此对Linux input子系统进行深入分 ...

  3. 如何用Dreamweaver编辑rails的html.erb文件

    默认情况下用dw是以普通的text文件打开html.erb文件,这多少让人有点不爽.其实dw打开erb文件也是相当的容易,下面就简单说下在mac os X下如何让dw支持erb文件: 首先找到dw的用 ...

  4. 3 sum closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  5. oracle 随机数(转载)

    http://blog.sina.com.cn/s/blog_6a01140c0100wimi.html 1.从表中随机取记录 select * from (select * from staff o ...

  6. Oracle数据库date类型与Java中Date的联系与转化

    以下是对Java中的日期对象与Oracle中的日期之间的区别与联系做点说明,以期对大家有所帮助.new Date():分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒),就是系统当前 ...

  7. 转发 JBPM工作流小结

    JBoss 题记:某部门领导有天突发奇想,把我们几个人叫过去,曰:最近出去开会,老有人推销自己的工作流产品,说的这好那好,你们几个给我研究研究.正好刚做完的xxx子系统里有一个申请审批的流程,你们按这 ...

  8. RocketMQ源码 — 八、 RocketMQ消息重试

    RocketMQ的消息重试包含了producer发送消息的重试和consumer消息消费的重试. producer发送消息重试 producer在发送消息的时候如果发送失败了,RocketMQ会自动重 ...

  9. PHP内核之旅-2.SAPI中的Cli

    PHP 内核之旅系列 PHP内核之旅-1.生命周期 PHP内核之旅-2.SAPI中的Cli 一.SAPI是什么? 1.1 理解SAPI (1)SAPI是PHP框架的接口层.有很多种服务器的SAPI的实 ...

  10. 拿到月薪30K,必选一些Python好书!

    论述: Python是所有编程语言中与人工智能最紧密相连的编程语言,阿尔法狗都在使用的 Python 语言. 教育部早在两个月前(自2018年3月起)就以及公布:大学生全国计算机二级考试中必考Pyth ...