JSON-RPCでAjax - 準備編

JSONIC いろいろ試してみました。すっごい便利。ガンガン Ajax するなら TeedaSAStruts を使わずに Seasar2JSONIC だけで十分そう。

そんな訳で Seasar2JSONICExtJSAjax アプリを作っていくチュートリアルを書いてみる。まずは準備編。

S2 セットアップ

http://s2container.seasar.org/2.4/ja/setup.html を参考に EclipseTomcat プロジェクトを作成し S2 環境を整える。今回は S2 だけを利用するので Dolteng を使わない場合にしたがった。プロジェクト名は「jsonic」。あと dicon ファイルは下記のような感じで。

■app.dicon

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN"
	"http://www.seasar.org/dtd/components24.dtd">
<components>
	<include path="convention.dicon"/>
	<include path="aop.dicon"/>
	<include path="j2ee.dicon"/>
	<include path="s2jdbc.dicon"/>
	<include path="dxo.dicon"/>
</components>

■convention.dicon

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN" 
	"http://www.seasar.org/dtd/components24.dtd">
<components>
	<component class="org.seasar.framework.convention.impl.NamingConventionImpl">
		<initMethod name="addRootPackageName">
			<arg>"sample"</arg>
		</initMethod>
	</component>
	<component class="org.seasar.framework.convention.impl.PersistenceConventionImpl"/>
</components>

creator.dicon

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN" 
	"http://www.seasar.org/dtd/components24.dtd">
<components>
	<include path="convention.dicon"/>
	<include path="customizer.dicon"/>
	<component class="org.seasar.framework.container.creator.DtoCreator"/>
	<component class="org.seasar.framework.container.creator.DxoCreator"/>
	<component class="org.seasar.framework.container.creator.HelperCreator"/>
	<component class="org.seasar.framework.container.creator.LogicCreator"/>
	<component class="org.seasar.framework.container.creator.ServiceCreator"/>
	<component class="org.seasar.framework.container.creator.InterceptorCreator"/>
</components>

■customizer.dicon

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN" 
	"http://www.seasar.org/dtd/components24.dtd">
<components>

  <include path="default-customizer.dicon"/>

  <component name="serviceCustomizer" class="org.seasar.framework.container.customizer.CustomizerChain">
    <initMethod name="addCustomizer">
      <arg>traceCustomizer</arg>
    </initMethod>
    <initMethod name="addCustomizer">
      <arg>requiredTxCustomizer</arg>
    </initMethod>
  </component>

  <component name="logicCustomizer" class="org.seasar.framework.container.customizer.CustomizerChain">
    <initMethod name="addCustomizer">
      <arg>traceCustomizer</arg>
    </initMethod>
  </component>

  <component name="dxoCustomizer" class="org.seasar.framework.container.customizer.CustomizerChain">
    <initMethod name="addCustomizer">
      <arg>traceCustomizer</arg>
    </initMethod>
    <initMethod name="addCustomizer">
      <arg>s2DxoCustomizer</arg>
    </initMethod>
  </component>

  <component name="helperCustomizer" class="org.seasar.framework.container.customizer.CustomizerChain">
    <initMethod name="addCustomizer">
      <arg>traceCustomizer</arg>
    </initMethod>
  </component>

</components>

■s2container.dicon

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN" 
	"http://www.seasar.org/dtd/components24.dtd">
<components>
    <include condition="#ENV == 'ut'" path="warmdeploy.dicon"/>
    <include condition="#ENV == 'ct'" path="hotdeploy.dicon"/>
    <include condition="#ENV != 'ut' and #ENV != 'ct'" path="cooldeploy.dicon"/>
</components>

JSONIC セットアップ

http://jsonic.sourceforge.jp/ から最新の JSONIC をダウンロードし jar ファイルをプロジェクトの WEB-INF/lib へ配置、 Eclipse のビルドパスを通す。次に web.xml を作成。内容は下記の通り。

■web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	version="2.4">
	
	<description>s2jsonic sample</description>

	<filter>
		<filter-name>hotdeployfilter</filter-name>
		<filter-class>org.seasar.framework.container.hotdeploy.HotdeployFilter</filter-class>
	</filter>

	<filter>
		<filter-name>s2filter</filter-name>
		<filter-class>org.seasar.framework.container.filter.S2ContainerFilter</filter-class>
	</filter>

	<filter>
		<filter-name>encodingfilter</filter-name>
		<filter-class>org.seasar.extension.filter.EncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	
	<filter-mapping>
		<filter-name>hotdeployfilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<filter-mapping>
		<filter-name>s2filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<filter-mapping>
		<filter-name>encodingfilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<servlet>
		<servlet-name>s2servlet</servlet-name>
		<servlet-class>org.seasar.framework.container.servlet.S2ContainerServlet</servlet-class>
		<init-param>
			<param-name>configPath</param-name>
			<param-value>app.dicon</param-value>
		</init-param>
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet>
		<description>JSON-WebService</description>
		<servlet-name>json-ws</servlet-name>
		<servlet-class>net.arnx.jsonic.web.WebServiceServlet</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>
				container: 'net.arnx.jsonic.web.S2Container'
				debug: true
				mappings: {
					'/[package]/[class]/[id].[ext]': 'sample.web.${package}.${class}Service'
					'/[package]/[class].[ext]': 'sample.web.${package}.${class}Service'
					'/[class].[ext]': 'sample.service.${class}Service'
				}
			</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>s2servlet</servlet-name>
		<url-pattern>/s2servlet</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>json-ws</servlet-name>
		<url-pattern>*.json</url-pattern>
	</servlet-mapping>

	<!--
	<session-config>
		<session-timeout>1</session-timeout>
	</session-config>
	-->

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>

</web-app>

以上で S2 Hot deploy のサクサク環境で JSONIC が利用できるようになりました。