0
Springコントローラとhtmlに問題があります:送信ボタンがクリックされた場所でバインドされていません。Spring Controllerはフォームバインドを行いません
これはフォームである:
<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クラスメソッドで行われます。
このRowMapperの使用/**
* 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;
}
:
アプリを実行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;
}
}
を、コンソールに私が持っている:選択された一つのOS名settedないと、当日のタイムスタンプを持つ
Object { osnamevalue: undefined, datevalue: 1503646503530 }
とされていません。
これは私のアプリでは最初のフォームバインディングではありません。私は他の人と同じスキーマに従いますが、問題はありませんでした。私の印象は、私が何か忘れていること、いくつかの特定のステップ、あるいは何らかの設定が間違っていることです。しかし、私は何を理解できません。
あなたのデータは、関数であるが、https://api.jquery.com/jquery.post/に応じて文字列またはプレーンオブジェクトが期待されています。 '{osnamevalue:" test "、datevalue:111}'を送信し、コントローラが正しく結果を取得するかどうかを確認するように単純化してください。それがサーバーかクライアントの問題かどうかを理解する – StanislavL