2013年10月30日水曜日

Springでページのエンコーディング設定方法

<filter>  
    <filter-name>encodingFilter</filter-name>  
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    <init-param>  
       <param-name>encoding</param-name>  
       <param-value>UTF-8</param-value>  
    </init-param>  
    <init-param>  
       <param-name>forceEncoding</param-name>  
       <param-value>true</param-value>  
    </init-param>  

</filter>

をweb.xmlに設定すれば、エンコーディングはUTF-8で出力。

http://stackoverflow.com/questions/5928046/spring-mvc-utf-8-encoding

2013年10月25日金曜日

単一テーブル継承(Single Table Inheritance)

Hibernateの@Entityを実装している時に、RDBの特定のカラム値を元にポリモーフしたサブクラスを割り当てたい。つまり、抽象クラスを@Entityにして、サブクラスを実装するにはどうしたら良いか調べた。

あっさりとJPA仕様にやり方が書いてあるのを見つけたところ、どうやらSingle Table Inheritance(単一テーブル継承)というやり方がある事が分かった。

そのやり方をWikiから抜粋:

Single Table Inheritance[edit]

Single table inheritance is the simplest and typically the best performing and best solution. In single table inheritance a single table is used to store all of the instances of the entire inheritance hierarchy. The table will have a column for every attribute of every class in the hierarchy. A discriminator column is used to determine which class the particular row belongs to, each class in the hierarchy defines its own unique discriminator value.

Example single table inheritance table in database[edit]

PROJECT (table)
IDPROJ_TYPENAMEBUDGET
1LAccounting50000
2SLegalnull

Example single table inheritance annotations[edit]

@Entity
@Inheritance
@DiscriminatorColumn(name="PROJ_TYPE")
@Table(name="PROJECT")
public abstract class Project {
  @Id
  private long id;
  ...
}
@Entity
@DiscriminatorValue("L")
public class LargeProject extends Project {
  private BigDecimal budget;
}
@Entity
@DiscriminatorValue("S")
public class SmallProject extends Project {
}


ポイントは@DiscripnatorColumnを抽象クラスで指定して、@DiscriminatorValueを実装クラスで教えてあげる。これはかなり便利。



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

jQueryのTabウィジェットでデフォルトで開けるタブを変更するには?

インデックスは0, 1, 2,...

1
$( ".selector" ).tabs({ active: 1 });

http://api.jqueryui.com/tabs/

MySQLに外部キー制約を途中から追加/削除する方法

追加:

ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)

削除:

ALTER TABLE Orders
DROP FOREIGN KEY fk_PerOrders

http://www.w3schools.com/sql/sql_foreignkey.asp