springmvc整合json

依赖包:

<!--springmvc 转 Json-->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.12</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.12</version>
    </dependency>

springmvc.xml

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
     
     <!--  
         spring json注解@ResponseBody
         注意:当springMVC-servlet.xml中使用<mvc:annotation-driven />时,
         如果是3.1之前已经默认注入AnnotationMethodHandlerAdapter,
         3.1之后默认注入RequestMappingHandlerAdapter只需加上上面提及的jar包即可!
         
         <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。
         <mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
        并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)
     -->
     <!---->  
     <mvc:annotation-driven />
     
    <!-- springmvc 扫包  @Controller @Service  .....-->
    <context:component-scan base-package="com.sniper.springmvc.**.web" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- jsp视图 -->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
     
</beans>

action

package com.sniper.springmvc.web.action;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.sniper.springmvc.model.User;

@Controller
@RequestMapping
public class IndexAction {
    
    @RequestMapping("/index.do")
    public String index() {
        return "index";
    }
    
    @RequestMapping("/getUser.do")
    public @ResponseBody User getUser() {
        User user = new User();
        user.setId(1L);
        user.setName("李明");
        user.setPasswd("111111");
        
        return user;
    }
    
    @RequestMapping("/getUserNormal.do")
    public void getUserNormal(HttpServletResponse resp) throws IOException {
        User user = new User();
        user.setId(1L);
        user.setName("李明");
        user.setPasswd("111111");
        
        Object json = JSONObject.toJSON(user);
        //发送json
        resp.setContentType("application/json;charset=UTF-8");
        //发送xml
        resp.setContentType("application/json;charset=UTF-8");
        //发送text
        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write(json.toString());
    }
    
}

index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<body>
<h2>Hello World!</h2>

<div id="content"></div>
<div id="content2"></div>

<script src="${pageContext.request.contextPath}/resources/js/jquery/jquery-2.1.1.js"></script>
<script type="text/javascript">
$.ajax({
    type:"POST",
    cache: false,
    url:'${pageContext.request.contextPath}/getUser.do',
    data:{},
    beforeSend: function(XMLHttpRequest){
        alert("请求将要开始,我们可以在此处显示加载条");
    },
    success:function(data){
        $("#content").html(data.id + '  ' + data.name + '  ' + data.passwd);
    },
    complete: function(XMLHttpRequest, textStatus){
        alert("请求完毕了,我们可以隐藏加载条 ");
    },
    error:function(XMLHttpRequest){
        alert("请求失败了   "+XMLHttpRequest.status);
    }
});

$.ajax({
    type:"POST",
    cache: false,
    url:'${pageContext.request.contextPath}/getUserNormal.do',
    data:{},
    beforeSend: function(XMLHttpRequest){
        alert("请求将要开始,我们可以在此处显示加载条");
    },
    success:function(data){
        $("#content2").html(data.id + '  ' + data.name + '  ' + data.passwd);
    },
    complete: function(XMLHttpRequest, textStatus){
        alert("请求完毕了,我们可以隐藏加载条 ");
    },
    error:function(XMLHttpRequest){
        alert("请求失败了   "+XMLHttpRequest.status);
    }
});
</script>

</body>
</html>


你可能感兴趣的:(springmvc整合json)