2013年10月25日金曜日

Spring 3.xでJSON型を返す@Controllerのクラスを実装する時に気をつけたい事

Spring 3.xで@Controllerのクラスを実装して、JSON型を返すRESTfulなサービスを作る時に気をつけたい事がある。ここは少しハマった。。。

まず仕様の抜粋から、

Underneath the covers, Spring MVC delegates to a HttpMessageConverter to perform the serialization. In this case, Spring MVC invokes a MappingJacksonHttpMessageConverter built on the Jackson JSON processor. This implementation is enabled automatically when you use the mvc:annotation-driven configuration element with Jackson present in your classpath.

つまり、Spring MVCはJSONの場合、JavaオブジェクトのシリアライズをMappingJacksonHttpMessageConverterに委譲する。その為には、applicationContext.xmlで<mvc:annotation-driven/>を追加しないと委譲が行われない。そして、これはメソッドの戻り値が@ResponseBodyで指定される。

そして、ハマったところ。。。

JSONにマップするクラスがgetter/setterを持っていない場合、HTTP 406のエラーコードが返されてうまくいかない。つまりJavaBeansでなければならないよう。おそらくMessageConverterの仕様でしょうが、ここがブラックボックスで分からない。。(?)

ということで、ここには気をつけましょう。


  1. <mvc:annotation-driven/>
  2. jackson-mapper-asl.jar, jackson-xc.jarが必要
  3. @ResponseBody
  4. JavaBeans(?)パターンの実装 -------> 調査中


http://spring.io/blog/2010/01/25/ajax-simplifications-in-spring-3-0/

2013年10月24日木曜日

jQueryでラジオボタンの無効化やチェックのやり方


$('#radioButtonId').prop('disabled', true);
$('#radioButtonId').prop('checked'true);

http://stackoverflow.com/questions/17057481/enable-disable-a-button-on-select-of-radio-button-in-jquery

jQueryのセレクタで複数のタグ属性を指定する使い方


例えば、

<div>
  <input type="radio" name="radio1" value="somevalue"/>
</div>

$('#divId input[type=radio][name=radio1]').prop('disabled', true);

こんな感じで、[]を複数使う。

http://api.jquery.com/multiple-attribute-selector/

jQueryのCDN

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>                                                

<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>                                        
<link rel="stylesheet" type="text/css" media="all" href="http://code.jquery.com/ui/1.10.3/themes/ui-lightness/jquery-ui.css" />

jQueryのDataTablesを使うのに、どのCDNを使えば良いか?


<script type="text/javascript" src="http://datatables.net/download/build/jquery.dataTables.js"></script>                  
<link rel="stylesheet" type="text/css" media="all" href="http://code.jquery.com/ui/1.10.3/themes/ui-lightness/jquery-ui.css" />                       
<link rel="stylesheet" type="text/css" media="all" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css" /> 

jQueryのDataTablesで各種ウィジェットを隠すには?


$('#tableId').dataTable({
    "bFilter": false,
    "bInfo": false
    "bPaginate": false

});

http://datatables.net/usage/features

jQueryのDataTablesで必要なHTMLタグの前提条件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<table id="table_id">
    <thead>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Row 1 Data 1</td>
            <td>Row 1 Data 2</td>
        </tr>
        <tr>
            <td>Row 2 Data 1</td>
            <td>Row 2 Data 2</td>
        </tr>
    </tbody>
</table>

※thead, tbodyが抜けててハマる事があるので注意。

http://datatables.net/blog/Getting_started_with_DataTables%3a_First_steps