Skip to content

Instantly share code, notes, and snippets.

@tamtam180
Last active December 16, 2015 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamtam180/5387530 to your computer and use it in GitHub Desktop.
Save tamtam180/5387530 to your computer and use it in GitHub Desktop.

JS

<script type="text/javascript">

var data = {
  "id" : "aaa",
  "hoge" : 100
};

var params = [];
for (var i = 0; i < 10; i++) {
  params.push({
    "name" : "nm" + i,
    "fuga" : "fuga" + i
  });
}
data["params"] = params;

$.post("/example.action", data).success(function(d){});

// Traditional=false
//   id=aaa
//   hoge=100
//   params[0][name]=nm0
//   params[0][fuga]=fuga0
//   params[1][name]=nm0
//   params[2][fuga]=fuga0
//   ...

</script>

Struts2 Action

class ExampleAction extends ActionSupport {
  private String id;
  private int hoge;
  List<AAA> params;
}
class AAA {
  private String name;
  private String fuga;
}

Struts2 Interceptor

params の acceptParamNames とか弄ってみたけど...
どうすれば上記のリクエストを受け取れるのかな・・。

@A-pZ
Copy link

A-pZ commented Apr 16, 2013

Actionクラス側:ParametersInterceptorにて定義されている許可パラメータの正規表現に次の内容で上書きします。
※以下の例はjsonValidationWorkFlowStackに対して、ParametersInterceptorの定義を追加
@InterceptorRefs({
@InterceptorRef(value = "jsonValidationWorkflowStack",
params = {
"params.acceptParamNames",
"\w+((.\w+)|([\d+])|((\d+))|(['\w+'])|(('\w+'))|([\d+]['\w+']))*" })
})
追加した内容は、一番最後の([\d+]['\w+'])、これは、パラメータ名[数値]['プロパティ名']を許可します。

\wとか、\dになっちゃってる部分は、本来バックスラッシュ2つです。(別途twitterでDします)

プロパティ名は['プロパティ名']なので、送信側のJSもちょっと修正します。

params.push({
"'name'" : "nm" + i,
"'fuga'" : "fuga" + i
});

と、シングルクォートて変数名を囲んであげれば完成です。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment