前端form表单中datetime类型数据传到后台

前端jsp页面用EasyUI写了个form表单,要传到后台的数据里面有个date和datetime格式的,后台直接用对象接收的,所以需要对datetime格式的数据进行处理。

js部分代码:

    //把字符串转为日期
    function dateFormat(stringTypeDate){
        var dateType = "";
        var date = new Date(stringTypeDate);
        dateType = date.getFullYear()+"-"+getMonth(date)+"-"+getDay(date);
        return dateType;
    }
    function datetimeFormat(stringTypeDate){  
        var datetimeType = "";  
        var date = new Date(stringTypeDate);  
        datetimeType = date.getFullYear()+"-"+getMonth(date)+"-"+getDay(date)+" "+
        getHours(date)+":"+getMinutes(date)+":"+getSeconds(date);//yyyy-MM-dd 00:00:00格式日期
        return datetimeType;
    }
    //返回 01-12 的月份值   
    function getMonth(date){  
        var month = "";  
        month = date.getMonth() + 1; //getMonth()得到的月份是0-11  
        if(month<10){  
            month = "0" + month;  
        }  
        return month;  
    }  
    //返回01-30的日期  
    function getDay(date){  
        var day = "";  
        day = date.getDate();  
        if(day<10){  
            day = "0" + day;  
        }  
        return day;  
    }
    //返回小时
    function getHours(date){
        var hours = "";
        hours = date.getHours();
        if(hours<10){  
            hours = "0" + hours;  
        }  
        return hours;  
    }
    //返回分
    function getMinutes(date){
        var minute = "";
        minute = date.getMinutes();
        if(minute<10){  
            minute = "0" + minute;  
        }  
        return minute;  
    }
    //返回秒
    function getSeconds(date){
        var second = "";
        second = date.getSeconds();
        if(second<10){  
            second = "0" + second;  
        }  
        return second;  
    }
    $(function() {
        $("#add").click(function() {
            var client = {
                    "client_name": $('#client_name').val(),
                    "client_id": $('#client_id').val(),
                    "client_sex": $("input[name='client_sex']:checked").val(),
                    "client_birthday":dateFormat($('#client_birthday').datebox('getValue')),
                    "client_address": $('#client_address').val(),
                    "room_no": $('#room_no').val(),
                    "isVip": $("input[name='isVip']:checked").val(),
                    "check_in":datetimeFormat($('#check_in').datetimebox('getValue'))
                }
                console.log(client)
            $.post("../insertClient.action",client,function(flag){
                if("OK"==flag){
                    window.location.href="allRoomsInfo.jsp";
                }
            })
        })
    })

form表单部分代码:

    <div id="dd" class="easyui-dialog" title="My Dialog" style="width:400px;height:500px;"  
        data-options="iconCls:'icon-save',resizable:true,modal:true">
    <form id="ff" method="post">
        <table>
            <tr>
                <td>姓名:td>
                <td><input type="text" id="client_name" name="client_name">td>
            tr>
            <tr>
                <td>身份证号:td>
                <td><input type="text" id="client_id" name="client_id">td>
            tr>
            <tr>
                <td>性别:td>
                <td>
                    <input type="radio" id="client_sex" name="client_sex" value="男"><input type="radio" id="client_sex" name="client_sex" value="女">td>
            tr>
            <tr>
                <td>出生日期:td>
                <td><input type="text" id="client_birthday"
                    name="client_birthday" class="easyui-datebox">td>
            tr>
            <tr>
                <td>籍贯:td>
                <td><input type="text" id="client_address"
                    name="client_address">td>
            tr>
            <tr>
                <td>房间号:td>
                
                <td><input type="text" id="room_no" name="room_no" class="easyui-combobox" data-options="
                        valueField: 'room_no',   
                        textField: 'room_no',   
                        url: '../queryAvailableRooms.action',   
                        onSelect: function(rec){   
                            var url = '../queryRoomByNo.action?room_no='+rec.room_no;   
                            $('#price').combobox('reload', url);   
                        }" value="请选择">td>
            tr>
            <tr>
                <td>入住时间:td>
                <td><input type="text" id="check_in" name="check_in" 
                    class="easyui-datetimebox">td>
            tr>
            
            <tr>
                <td>会员:td>
                <td>
                    <input type="radio" id="isVip" name="isVip" value="是"><input type="radio" id="isVip" name="isVip" value="否">td>
            tr>
            <tr>
                <td>价格:td>
                <td>
                    <input type="text" id="price" name="price" class="easyui-combobox" 
                    data-options="valueField: 'room_no',textField: 'price'">
                td>
            tr>
            <tr>
                <td><input type="button" id="add" value="submit">td>
            <tr>
        table>
    form>
div>

实体类Client:

package com.zh.vo;

import java.sql.Date;
import java.sql.Timestamp;

public class Client {
    private String client_name;
    private String client_id;
    private String client_sex;
    private Date client_birthday;
    private String client_address;
    private int room_no;
    private Timestamp check_in;
    private Timestamp check_out;
    private String isVip;
    private String vip_no;
    ...
    getset、toString()方法省略
}

controller部分代码:

    @RequestMapping(value="insertClient")
    @ResponseBody
    public String insertClient(@Param("client") Client client) {
        //System.out.println(client);
        clientService.insertClient(client);
        roomService.updateRoomStatus(client.getRoom_no(), "入住");
        return "OK";
    }

你可能感兴趣的:(EasyUI)