bootstrap通过ajax请求JSON数据后填充到模态框

1.   JSP页面中准备模态框

            
            <div>
                <div class="modal fade" id="queryInfo" tabindex="-1" role="dialog"
                    aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal"
                                    aria-hidden="true">×button>
                                <h4 class="modal-title" id="myModalLabel">详细信息h4>
                            div>
                            <form
                                action="${pageContext.request.contextPath }/productServlet?type=info"
                                method="post">
                                <div class="modal-body">
                                    <div class="input-group">
                                        <span class="input-group-addon">         span>
                                        <input type="text" class="form-control" placeholder="请输入名称"
                                            id="name1" readonly="readonly">
                                    div>
                                    <br />
                                    <div class="input-group">
                                        <span class="input-group-addon">规格及型号span> <input
                                            type="text" class="form-control" placeholder="请输入规格及型号"
                                             id="xinghao1" readonly="readonly">
                                    div>
                                    <br />
                                    <div class="input-group">
                                        <span class="input-group-addon">   span>
                                        <input type="text" class="form-control"
                                            placeholder="请输入存放地点名称" id="address1"
                                            readonly="readonly">
                                    div>
                                    <br />
                                    <div class="input-group">
                                        <span class="input-group-addon">使   span>
                                        <input type="text" class="form-control"
                                            placeholder="请输入使用部门名称" id="department1"
                                            readonly="readonly">
                                    div>
                                    <br />
                                    <div class="input-group">
                                        <span class="input-group-addon">         span>
                                        <input type="text" class="form-control" placeholder="请输入单位名称"
                                            id="unit1" readonly="readonly">
                                    div>
                                    <br />
                                    <div class="input-group">
                                        <span class="input-group-addon">         span>
                                        <input type="text" class="form-control" placeholder="请输入数量"
                                            id="number1" readonly="readonly">
                                    div>
                                    <br />
                                    <div class="input-group">
                                        <span class="input-group-addon">         span>
                                        <input type="text" class="form-control" placeholder="请输入单价"
                                            id="price1" readonly="readonly">
                                    div>
                                    <br />
                                    <div class="input-group">
                                        <span class="input-group-addon">         span>
                                        <input type="text" class="form-control" placeholder="请输入金额"
                                            id="totalprice1" readonly="readonly">
                                    div>
                                    <br />
                                    <div class="input-group">
                                        <span class="input-group-addon">         span>
                                        <input type="text" class="form-control" placeholder="请输入来源"
                                            id="come1" readonly="readonly">
                                    div>
                                    <br />
                                    <div class="input-group">
                                        <span class="input-group-addon">   span>
                                        <input type="text" class="form-control" placeholder="请输入购建日期"
                                            id="buytime1" readonly="readonly">
                                    div>
                                    <br />
                                    <div class="input-group">
                                        <span class="input-group-addon">使      span>
                                        <input type="text" class="form-control" placeholder="请输入使用人名称"
                                            id="useperson1" readonly="readonly">
                                    div>
                                    <br />
                                    <div class="input-group">
                                        <span class="input-group-addon">      span>
                                        <input type="text" class="form-control" placeholder="请输入经办人名称"
                                            id="handleperson1" readonly="readonly">
                                    div>
                                    <br />
                                    <div class="input-group">
                                        <span class="input-group-addon">      span>
                                        <input type="text" class="form-control" placeholder="请输入管理员名称"
                                            id="admini1" readonly="readonly">
                                    div>
                                div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default"
                                        data-dismiss="modal">关闭button>
                                    
                                div>
                            form>
                        div>
                        
                    div>
                    
                div>

 2.准备点击请求json数据的按钮(带着id请求数据)

<a class="queryA" href="javascript:void(0)" onclick="query(${register.id})"> <span
                                    class="glyphicon glyphicon-search">span>
                            a>

 

 3.JS处理按钮点击事件

function query(id) {
    alert('ajax请求之前');
    $.ajax({
        url : "/small/productServlet",
        async : true,
        type : "POST",
        data : {
            "type" : "query",
            "id" : id
        },
        // 成功后开启模态框
        success : showQuery,
        error : function() {
            alert("请求失败");
        },
        dataType : "json"
    });
}

// 查询成功后向模态框插入数据并开启模态框。data是返回的JSON对象
function showQuery(data) {
    $("#name1").val(data.name);
    $("#xinghao1").val(data.xinghao);
    $("#address1").val(data.address);
    $("#department1").val(data.department);
    $("#unit1").val(data.unit);
    $("#number1").val(data.number);
    $("#price1").val(data.price);
    $("#totalprice1").val(data.totalprice);
    $("#come1").val(data.come);
    $("#buytime1").val(data.buytime);
    $("#useperson1").val(data.useperson);
    $("#handleperson1").val(data.handleperson);
    $("#admini1").val(data.admini);
    // 显示模态框
    $('#queryInfo').modal('show');
}

 

 4.后台接收请求返回JSON

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RegisterService service = new RegisterServiceImpl();
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        String type = request.getParameter("type");
        if (type != null && "add".equals(type)) {
            try {
                this.add(request, response, service);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        if (type != null && "query".equals(type)) {
            try {
                this.query(request, response, service);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }


    public void query(HttpServletRequest request, HttpServletResponse response, RegisterService service)
            throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Register register = service.getRegisterById(id);
        Gson gson = new Gson();
        String json = gson.toJson(register);
        System.out.println(json);
        response.getWriter().write(json);
    }

 

 

 结果:

{"id":16,"name":"测试名称16","address":"测试地址2","department":"测试部门2","unit":"测试单位2","number":1,"price":1.5,"totalprice":1.5,"come":"来源2","buytime":"八月 23, 2017","useperson":"使用人2","handleperson":"经办人2","admini":"管理员部门2"}

 

 

5.最终效果:

 

 

 

 

 

 

 

 

 

 

{"id":16,"name":"测试名称16","address":"测试地址2","department":"测试部门2","unit":"测试单位2","number":1,"price":1.5,"totalprice":1.5,"come":"来源2","buytime":"八月 23, 2017","useperson":"使用人2","handleperson":"经办人2","admini":"管理员部门2"}

转载于:https://www.cnblogs.com/qlqwjy/p/7421692.html

你可能感兴趣的:(bootstrap通过ajax请求JSON数据后填充到模态框)