Spring Controller不进行表单绑定

时间:2017-08-25 07:56:37

标签: java spring-mvc html-form

我的Spring控制器和html有问题:单击提交按钮时没有绑定。

这是表格:

<form class="form-horizontal" role = "form" id="searchtodayeventsform">


                    <div class = "form-group">
                        <label for = "note" class = "col-sm-3 control-label">Date</label>
                             <div class = "col-sm-8">
                                <input type = "date" class = "form-control" id = "todaydatevalue" placeholder = "MM/DD/AAAA">
                             </div>
                    </div>


                    <div class = "form-group">
                        <label for = "type" class = "col-sm-3 control-label">OS</label>
                             <div class = "col-sm-8">
                                <select class = "form-control" id = "osnamevalue" name="todayostypeoptions">
                                <option value="Linux">Linux</option>
                                <option value="Windows">Windows</option>
                                </select>
                             </div>
                    </div>


                    <div class = "form-group">
                        <div class = "col-sm-offset-2 col-sm-10" id="submit">
                            <button type = "submit" class = "btn btn-default">Cerca</button>
                        </div>
                    </div>

这是用于控制日期参数并将所有内容发送到控制器的函数:

DTevents = $('#eventsosdatedata').DataTable( 
            { 
                "serverSide": true,
                "ajax":{
                            url: "../geteventsosdate.json",
                            type: "post",
                            "data": function (d)
                            {
                                 var param = {osnamevalue :  $('#osnamevalue').val()};

                                 if(moment($('#todaydatevalue').val()).isValid())
                                 param = $.extend(param, {datevalue :  moment($('#todaydatevalue').val()).toDate().getTime()});
                                 console.log(param);
                                 return $.extend(d, param);
                            }

                       },

这是控制器:

/**
     * This method is used when a list of events of speficic day and os are requested
     * 
     * @param model the model data
     * @param request the http request
     * @param os the os choosed
     * @param date the date desired
     * @return the updated model
     * @throws IOException
     */
    @PostMapping(value="/geteventsosdate.json")
    @ResponseBody
    public ModelAndView showEventsOsDate(ModelAndView model, HttpServletRequest request,
            @RequestParam(name="osnamevalue", required=false) String os, 
            @RequestParam(name="todaydatevalue", required=false) Long date) throws IOException
    {
        Timestamp date_value;
        Date effective_date = null;

        if(date != null)
        {
            date_value = new Timestamp(date); 
            effective_date = new Date(date_value.getTime());
        }

        //First, we must populate the list of Events 
        List<Events> listEvents = networks.getEventsWithDateAndOs(os, effective_date);

        //Second, we put this list in the model and set properties for jquery datatables
                model.addObject("recordsTotal", listEvents.size());
                model.addObject("recordsFiltered", listEvents.size());
                model.addObject("data", listEvents);

        //Finally, we return the model
        return model;
    }

在控制器内执行的查询是在这个DAO类方法中完成的:

/**
     * Return the events with same format of the current day table, 
     * where users can choose day and os.
     * 
     * @return a list with join query results
     */
    public List<Events> getEventsWithDateAndOs(String os, Date date)
    {
        /**
         * This is the string that contain the query to obtain the data from join of
         * hosts and events with aggregator operations.
         */
        String SQL = "SELECT hosts.name, hosts.os,"
                + " MAX(case when events.type = 'Applications' then events.status end) as Applications,"
                + " MAX(case when events.type = 'OS' then events.status end) as OS_Type,"
                + " MAX(case when events.type = 'Running Services' then events.status end) as Running_Services,"
                + " MAX(case when events.type = 'TCP Services' then events.status end) as TCP_Services,"
                + " MAX(case when events.type = 'File Integrity' then events.status end) as File_Integrity,"
                + " MAX(case when events.type = 'Services' then events.status end) as Services"
                + " FROM hosts JOIN events ON hosts.id = events.host_id"
                + " WHERE events.date = ? AND (? is null or hosts.os = ?)"
                + " GROUP BY hosts.name";

        /**
         * The list containing the results is obtained using the method query on jdcbtemplate, giving in in input to it the query string, the array of object 
         * containing the input variabile of the method and the rowmapper implemented.
         */
        List<Events> theEvents = jdbcTemplate.query(SQL, new Object[]{os, os, date}, new EventTypeCountMapper());

        return theEvents;
    }

使用此rowmapper:

import java.sql.ResultSet;
import java.sql.SQLException;

import org.consorziotriveneto.networks.entity.Events;
import org.springframework.jdbc.core.RowMapper;

public class EventTypeCountMapper implements RowMapper<Events>
{
        //This method must be implemented when we use a row mapper
        public Events mapRow(ResultSet rs, int rowNum) throws SQLException
        {
            Events events = new Events();


            //mapping of hosts attributes
            events.setName(rs.getString("hosts.name"));
            events.setOs(rs.getString("hosts.os"));
            //mapping od count query
            events.setApplications(rs.getString("Applications"));
            events.setOs_type(rs.getString("OS_Type"));
            events.setServices(rs.getString("Services"));
            events.setRunning_services(rs.getString("Running_Services"));
            events.setTcp_services(rs.getString("TCP_Services"));
            events.setFile_integrity(rs.getString("File_Integrity"));

            return events;

        }
}

在控制台中执行应用程序我有:

Object { osnamevalue: undefined, datevalue: 1503646503530 }

未设置操作系统名称和当前日期的时间戳,而不是所选时间戳。

这不是我应用中的第一个表单绑定;我跟其他人一样遵循相同的架构,没有问题。我的印象是,我忘记了某些事情,某些特定的步骤,或者我做了一些错误的设置,但我无法理解。

1 个答案:

答案 0 :(得分:0)

它没有进行绑定,因为未指定name参数。

请尝试这样:

<input type = "date" class = "form-control" id = "todaydatevalue" name"todaydatevalue" placeholder = "MM/DD/AAAA">