お世話になってます
現在ajaxの非同期通信で送られてきたjsonデータをjavaで取り扱えるように変換を行っているのですがうまくいきません。jsonで送られてきた文字列のキー(name)に対応するフィールドをjava側のクラスで宣言してそこに値を格納して利用したいのですが、うまくjava側に値が入りません。パーサーライブラリーはGSONを用いています。
以下ソースコードです
jsonの値を格納するJavaクラス
package jp.co.xmd.form; public class MonthLessonSerchForm { private String LessonRoomNameValue; private String InstructorNameValue; private String YearValue; private String MonthValue; private String LessonNameValue; public String getLessonNameValue() { return LessonNameValue; } public void setLessonNameValue(String lessonNameValue) { LessonNameValue = lessonNameValue; } public String getLessonRoomNameValue() { return LessonRoomNameValue; } public void setLessonRoomNameValue(String lessonRoomNameValue) { LessonRoomNameValue = lessonRoomNameValue; } public String getInstructorNameValue() { return InstructorNameValue; } public void setInstructorNameValue(String instructorNameValue) { InstructorNameValue = instructorNameValue; } public String getYearValue() { return YearValue; } public void setYearValue(String yearValue) { YearValue = yearValue; } public String getMonthValue() { return MonthValue; } public void setMonthValue(String monthValue) { MonthValue = monthValue; } }
コントローラークラスです(使用しているメソッドのみ抜粋)
RequestMapping(value="/search"
1 public String Search(String Arr2){ 2 System.out.println(Arr2);//非同期通信で送られてきたjson文字列を表示している(結果は後で載せます) 3 4 5 Gson gson = new Gson();//Jsonに変換するためのクラスのインスタンスを生成。 6 7 MonthLessonSerchForm user = gson.fromJson(Arr2.toString(), 8 MonthLessonSerchForm.class);//jsonデータを変換して上記のjavaクラスに格納している? 9 System.out.println(user);//チェック用の表示 何も表示されず。。。 10 System.out.println("LessonRoomNameValue = " + user.getLessonRoomNameValue());//同じくチェック用 フィールドの値を参照している 11 12 13 return Arr2; 14 } 15
非同期通信で送られてきたjsonデータです
[{"name":"LessonNameValue","value":"1"},{"name":"LessonRoomNameValue","value":"1"},{"name":"InstructorNameValue","value":""},{"name":"YearValue","value":"2013"},{"name":"MonthValue","value":"3"}]
どうかよろしくお願いします
不足があったら申し付けてください。追記します
追記(うまく<form>タグの内容をjsonにできていないみたいなので使っているコードを追記します。不足があったら申し付けてください。追記します)
HTML (セレクトボックスの内容はデータベースから拾ってます)
HTML
1 2<f:form modelAttribute="DayLessonSearchFormMenu" action="${pageContext.request.contextPath}/day_lesson_search" class="form-inline" method="POST"> 3 <div class="row"> 4 <div class="form-group col-lg-2 col-md-2 col-xs-12"> 5 <label>レッスン名</label> 6 </div> 7 <div class="form-group col-lg-2 col-md-2 col-xs-12"> 8 <f:select path="LessonNameValue" items="${lessonNameList}" 9 itemLabel="LessonNameLabel" itemValue="LessonNameValue" class="form-control"> 10 </f:select> 11 </div> 12 <div class="form-group col-lg-2 col-md-2 col-xs-12"> 13 <label>教室名</label> 14 </div> 15 <div class="form-group col-lg-2 col-md-2 col-xs-12"> 16 <f:select path="LessonRoomNameValue" items="${lessonRoomNameList}" 17 itemLabel="LessonRoomNameLabel" itemValue="LessonRoomNameValue" class="form-control"> 18 </f:select> 19 </div> 20 <div class="form-group col-lg-2 col-md-4 col-sm-6 col-sm-12"> 21 <label>担当講師名</label> 22 </div> 23 <div class="form-group col-lg-2 col-md-4 col-sm-6 col-sm-12"> 24 <f:select path="InstructorNameValue" items="${instructorNameList}" 25 itemLabel="InstructorNameLabel" itemValue="InstructorNameValue" class="form-control"> 26 </f:select> 27 </div> 28 </div> 29 <div class="row"> 30 <div class="form-group col-lg-2 col-md-2 col-sm-3 col-xs-12"> 31 <label>レッスン実施年</label> 32 </div> 33 <div class="form-group col-lg-2 col-md-2 col-sm-3 col-xs-12"> 34 <f:select path="YearValue" items="${yearList}" 35 itemLabel="YearLabel" itemValue="YearValue" class="form-control"> 36 </f:select> 37 </div> 38 <div class="form-group col-lg-2 col-md-2 col-sm-3 col-xs-12"> 39 <label>レッスン実施月</label> 40 </div> 41 <div class="form-group col-lg-2 col-md-2 col-sm-3 col-xs-12"> 42 <f:select path="MonthValue" items="${monthList}" 43 itemLabel="MonthLabel" itemValue="MonthValue" class="form-control"> 44 </f:select> 45 </div> 46 47 </div> 48 <div class="row"> 49 <div class="form-group col-lg-2 col-md-4 col-sm-6 col-sm-12"> 50 <label>実施時間</label> 51 </div> 52 <div class="form-group col-lg-6 col-md-4 col-sm-6 col-sm-12"> 53 <input type="text" class="form-control" placeholder="00:00">~ 54 <input type="text" class="form-control"placeholder="23:59"> 55 </div> 56 <div class="form-group col-lg-4 col-md-4 col-sm-6 col-sm-12"> 57 <input type="submit" class="btn btn-default form-control 58 search_button" value="検索"> 59 </div> 60 </div> 61 </f:form> 62
非同期通信と<form>の内容をjsonにして送るjavascriptです
javascript
1$('[value=検索]').click(function() { 2 $('form').submit(function(event) { 3 event.preventDefault(); 4 var lessonArr = $('form').serializeArray(); 5 alert(JSON.stringify(lessonArr)); 6 $('[value=検索]').attr('disabled', true); 7 $.ajax({ 8 9 10 type : "GET", 11 url : "lesson_search", // リクエストURL 12 data : "lessonArr2=" + JSON.stringify(lessonArr), 13 datatype: "json", 14 success: function(data){ 15 alert("ok"); 16 console.log(data); 17 $('[value=検索]').attr('disabled', false); 18 location.reload(); 19 }, 20 error: function(data){ 21 alert("値はコンソール参照"); 22 $('[value=検索]').attr('disabled', false); 23 location.reload(); 24 } 25 26 }); 27 }); 28 });
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/16 00:37
2016/09/16 04:16