• SpringMVC+ajax返回JSON串


    一、引言

    本文使用springMVC和ajax做的一个小小的demo,实现将JSON对象返回到页面,没有什么技术含量,纯粹是因为最近项目中引入了springMVC框架。

    二、入门例子

    ①. 建立工程,并导入相应spring jar包和解析json的包fastjson。

    ②. 在web.xml文件中配置Spring的核心类DispatcherServlet

    ③. 配置Spring的核心配置文件spring-servlet.xml

    ④. 编写实体类Person

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    public class Person { 
        private String name; 
        private Integer age; 
       
        public String getName() { 
            return name; 
        
       
        public void setName(String name) { 
            this.name = name; 
        
       
        public Integer getAge() { 
            return age; 
        
       
        public void setAge(Integer age) { 
            this.age = age; 
        
       
        public String toString() { 
            return "[name: " + name + ", age: " + age + "]"
        
    }

    ⑤. 编写控制器类PersonControll

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    @Controller 
    public class PersonControll { 
        @RequestMapping("toAjax.do"
        public String toAjax() { 
            return "ajax"
        
           
        @RequestMapping(value = "ajax.do", method = RequestMethod.GET) 
        public void ajax(@ModelAttribute Person person,PrintWriter printWriter) { 
            System.out.println(person); 
            String jsonString = JSON.toJSONString(person, SerializerFeature.PrettyFormat); 
            printWriter.write(jsonString); 
            printWriter.flush(); 
            printWriter.close(); 
        
    }

    ⑥. 编写访问页面ajax.jsp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
    <html> 
        <head> 
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8"
            <title>ajax for springMVC</title> 
            <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script> 
            <script type="text/javascript"
                $(function() { 
                    $("#click").click(function() { 
                        $.ajax( { 
                            type : "GET"
                            url : "ajax.do"
                            data : "name=zhangsan&age=20"
                            dataType: "text"
                            success : function(msg) { 
                                alert(msg); 
                            
                        }); 
                    }); 
                }); 
            </script> 
        </head> 
        <body> 
            <input id="click" type="button" value="click to show person" /> 
        </body> 
    </html>

    ⑦. 访问url: http://localhost:8080/springMVC/toAjax.do

  • 相关阅读:
    oracle数据段详解
    监听静态注册与动态注册
    Oracle网络相关概念与常用配置文件
    pycharm社区版安装及遇到的问题
    强化学习-K摇臂赌博机
    概率图模型
    半监督学习
    卷积神经网络
    递归神经网络
    玻尔兹曼机及其相关模型
  • 原文地址:https://www.cnblogs.com/likeju/p/4775568.html
Copyright © 2020-2023  润新知