官方主页:http://sastruts.seasar.org/
截止当前,SAStruts已经更新到1.0.4-sp2,下面的代码仍是1.0.4-sp1版本。
如下图所示建立工程:
需要用到的lib列表如下:
antlr.jar
aopalliance-1.0.jar
commons-beanutils-1.7.0.jar
commons-collections-3.1.jar
commons-digester-1.6.jar
commons-el-1.0.jar
commons-fileupload-1.2.jar
commons-io-1.3.2.jar
commons-logging-1.1.jar
commons-validator-1.2.0.jar
geronimo-annotation_1.0_spec-1.0.jar
geronimo-ejb_3.0_spec-1.0.jar
geronimo-interceptor_3.0_spec-1.0.jar
geronimo-jpa_3.0_spec-1.0.jar
geronimo-jta_1.1_spec-1.0-tsr.jar
geronimo-jta_1.1_spec-1.0.jar
jakarta-oro-2.0.8.jar
javassist-3.4.ga.jar
jstl-1.1.2.jar
log4j-1.2.13.jar
ognl-2.6.9-patch-20070908.jar
poi-3.0-FINAL.jar
s2-extension-2.4.28.jar
s2-framework-2.4.28.jar
s2-tiger-2.4.28.jar
sa-struts-1.0.4-sp1.jar
standard.jar
struts-1.2.9.jar
代码依次为:
AddAction.java
packagetutorial.action;
importjavax.annotation.Resource;
importorg.seasar.struts.annotation.ActionForm;
importorg.seasar.struts.annotation.Execute;
importtutorial.form.AddForm;
publicclassAddAction{
publicIntegerresult;
@ActionForm
@Resource
protectedAddFormaddForm;
@Execute(validator=false)
publicStringindex(){
return"index.jsp";
}
@Execute(input="index.jsp")
publicStringsubmit(){
result=Integer.valueOf(addForm.arg1)+Integer.valueOf(addForm.arg2);
return"index.jsp";
}
}
AddForm.java
packagetutorial.form;
importorg.seasar.struts.annotation.IntegerType;
importorg.seasar.struts.annotation.Required;
publicclassAddForm{
@Required
@IntegerType
publicStringarg1;
@Required
@IntegerType
publicStringarg2;
}
app.dicon
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEcomponentsPUBLIC"-//SEASAR//DTDS2Container2.4//EN"
"http://www.seasar.org/dtd/components24.dtd">
<components>
<includepath="convention.dicon"/>
<includepath="aop.dicon"/>
<includepath="j2ee.dicon"/>
<!--
<includepath="s2jdbc.dicon"/>
-->
<componentname="actionMessagesThrowsInterceptor"class="org.seasar.struts.interceptor.ActionMessagesThrowsInterceptor"/>
</components>
application_ja.properties
errors.header=<fontcolor="red"><ul>
errors.footer=</ul></font>
errors.prefix=<li>
errors.suffix=</li>
errors.invalid={0}が不正です。
errors.maxlength={0}の長さが最大値({1})を超えています。
errors.minlength={0}の長さが最小値({1})未満です。
errors.maxbytelength={0}のバイト長が最大値({1})を超えています。
errors.minbytelength={0}のバイト長が最小値({1})未満です。
errors.range={0}は{1}と{2}の間でなければいけません。
errors.required={0}は必須です。
errors.byte={0}はバイトでなければいけません。
errors.date={0}は日付でなければいけません
errors.double={0}は倍精度実数でなければいけません。
errors.float={0}は単精度実数でなければいけません。
errors.integer={0}は整数でなければいけません。
errors.long={0}は長整数でなければいけません。
errors.short={0}は短整数でなければいけません。
errors.creditcard={0}はクレジットカード番号として不正です。
errors.email={0}はメールアドレスとして不正です。
errors.url={0}はURLとして不正です。
errors.required.other={0}は{1}が入力されたときは必須です。
errors.phone={0}は電話番号({1})として不正です。
errors.norole=適切なロールがありません。
errors.debug=例外のデバッグ用。
errors.upload.size=上限が{1}バイトなのに実際は{0}バイトだったのでアップロードできませんでした。
messages.upload.complete={0}にアップロードしました。
#labels.arg1=引数1
application.properties
errors.header=<fontcolor="red"><ul>
errors.footer=</ul></font>
errors.prefix=<li>
errors.suffix=</li>
errors.invalid={0}isinvalid.
errors.maxlength={0}cannotbegreaterthan{1}characters.
errors.minlength={0}cannotbelessthan{1}characters.
errors.maxbytelength={0}cannotbegreaterthan{1}bytes.
errors.minbytelength={0}cannotbelessthan{1}bytes.
errors.range={0}isnotintherange{1}through{2}.
errors.required={0}isrequired.
errors.required.other={0}isrequired({1}).
errors.byte={0}mustbeanbyte.
errors.date={0}isnotadate.
errors.double={0}mustbeandouble.
errors.float={0}mustbeanfloat.
errors.integer={0}mustbeaninteger.
errors.long={0}mustbeanlong.
errors.short={0}mustbeanshort.
errors.creditcard={0}isnotavalidcreditcardnumber.
errors.email={0}isaninvalide-mailaddress.
errors.url={0}isaninvalidurl(webaddress).
errors.upload.size=Uploadingfailed,becauseactualsize{0}bytesexceededlimitsize{1}bytes.
labels.name=Name
convention.dicon
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEcomponentsPUBLIC"-//SEASAR//DTDS2Container2.4//EN"
"http://www.seasar.org/dtd/components24.dtd">
<components>
<componentclass="org.seasar.framework.convention.impl.NamingConventionImpl">
<initMethodname="addRootPackageName">
<arg>"tutorial"</arg>
</initMethod>
</component>
<componentclass="org.seasar.framework.convention.impl.PersistenceConventionImpl"/>
</components>
creator.dicon
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEcomponentsPUBLIC"-//SEASAR//DTDS2Container2.4//EN"
"http://www.seasar.org/dtd/components24.dtd">
<components>
<includepath="convention.dicon"/>
<includepath="customizer.dicon"/>
<componentclass="org.seasar.framework.container.creator.ActionCreator"/>
<componentclass="org.seasar.struts.creator.FormCreator"/>
<componentclass="org.seasar.framework.container.creator.ServiceCreator"/>
<componentclass="org.seasar.framework.container.creator.DtoCreator"/>
<componentclass="org.seasar.framework.container.creator.InterceptorCreator"/>
</components>
customizer.dicon
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEcomponentsPUBLIC"-//SEASAR//DTDS2Container2.4//EN"
"http://www.seasar.org/dtd/components24.dtd">
<components>
<includepath="default-customizer.dicon"/>
<componentname="actionCustomizer"
class="org.seasar.framework.container.customizer.CustomizerChain">
<initMethodname="addAspectCustomizer">
<arg>"aop.traceInterceptor"</arg>
</initMethod>
<initMethodname="addAspectCustomizer">
<arg>"actionMessagesThrowsInterceptor"</arg>
</initMethod>
<initMethodname="addCustomizer">
<arg>
<component
class="org.seasar.framework.container.customizer.TxAttributeCustomizer"/>
</arg>
</initMethod>
<initMethodname="addCustomizer">
<arg>
<component
class="org.seasar.struts.customizer.ActionCustomizer"/>
</arg>
</initMethod>
</component>
<componentname="formCustomizer"
class="org.seasar.framework.container.customizer.CustomizerChain">
</component>
<componentname="serviceCustomizer"
class="org.seasar.framework.container.customizer.CustomizerChain">
<initMethodname="addAspectCustomizer">
<arg>"aop.traceInterceptor"</arg>
</initMethod>
<initMethodname="addCustomizer">
<arg>
<component
class="org.seasar.framework.container.customizer.TxAttributeCustomizer"/>
</arg>
</initMethod>
</component>
</components>
env_ut.txt : ut
env.txt : ct
jdbc.dicon
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEcomponentsPUBLIC"-//SEASAR2.1//DTDS2Container//EN"
"http://www.seasar.org/dtd/components21.dtd">
<componentsnamespace="jdbc">
<includepath="jta.dicon"/>
<!--forHSQLDB-->
<!--
<componentname="xaDataSource"
class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
<propertyname="driverClassName">
"org.hsqldb.jdbcDriver"
</property>
<propertyname="URL">
"jdbc:hsqldb:file:"
+application.getRealPath("/WEB-INF/data")
+"/test"
</property>
<propertyname="user">"sa"</property>
<propertyname="password">""</property>
</component>
-->
<!--forH2-->
<!--
<componentname="xaDataSource"
class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
<propertyname="driverClassName">
"org.h2.Driver"
</property>
<propertyname="URL">
"jdbc:h2:file:"
[email protected]@getBuildDir(@examples.entity.JdbcManagerTest@class).getCanonicalPath()
+"/data/demo;DB_CLOSE_ON_EXIT=FALSE"
</property>
<propertyname="user">"sa"</property>
<propertyname="password"></property>
</component>
-->
<!--forOracle-->
<!--
<componentname="xaDataSource"
class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
<propertyname="driverClassName">
"oracle.jdbc.driver.OracleDriver"
</property>
<propertyname="URL">
"jdbc:oracle:thin:@xxx:1521:xxx"
</property>
<propertyname="user">"xxx"</property>
<propertyname="password">"xxx"</property>
</component>
-->
<!--forDB2-->
<!--
<componentname="xaDataSource"
class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
<propertyname="driverClassName">
"com.ibm.db2.jcc.DB2Driver"
</property>
<propertyname="URL">
"jdbc:db2://foo.bar.com:50000/SAMPLE"
</property>
<propertyname="user">"db2user"</property>
<propertyname="password">"db2password"</property>
<initMethodname="addProperty">
<arg>"currentSchema"</arg>
<arg>"SCHEMA"</arg>
</initMethod>
</component>
-->
<!--forPostgreSQL-->
<!--
<componentname="xaDataSource"
class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
<propertyname="driverClassName">
"org.postgresql.Driver"
</property>
<propertyname="URL">
"jdbc:postgresql://localhost/TEST"
</property>
<propertyname="user">"xxxx"</property>
<propertyname="password">"xxxx"</property>
</component>
-->
<!--MySQL
-MySQL4.0以下でマルチバイト文字を扱う場合には,URL指定の後に,
以下の接続パラメータを追加で指定してください.
useUnicode=true
characterEncoding=[MySQLのエンコーディングに対応した
Javaのエンコーディング名]
例:"jdbc:mysql://localhost:3306/test?useUnicode=true"+
"&characterEncoding=Windows-31J"
-MySQL5.0以降で,エンコーディングがeucjpmsの列を扱う場合には,
URL指定の後に,以下の接続パラメータを追加で指定してください.
characterEncoding=UTF-8またはWindows-31J
characterSetResults=UTF-8またはWindows-31J
例:"jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8"+
"&characterSetResults=UTF-8"
この指定をしない場合,Java側でUCS-2⇔EUC_JP_Solarisの変換が
行なわれるため,ユーザー定義外字などの一部の文字が化けます.
この指定をすることで,MySQL側でeucjpms⇔ucs2⇔utf8(cp932)の
変換が行なわれ,Java側でUCS-2⇔UTF-8(Windows-31J)の変換が
行なわれるようになります.この結果,文字化けを防げます.
なおJISX0212(補助漢字)を使用する場合は,Windows-31Jではなく
UTF-8を指定する必要があります.
-上記以外の場合は,my.cnfでdefault-character-setの設定を適切に
(cp932やutf8など.デフォルトのlatin1は不可)行なっていれば,
文字化けは防げます.
<componentname="xaDataSource"
class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
<propertyname="driverClassName">
"com.mysql.jdbc.Driver"
</property>
<propertyname="URL">
"jdbc:mysql://localhost:3306/test"
</property>
<propertyname="user">"xxx"</property>
<propertyname="password">"xxx"</property>
</component>
-->
<!--forSQLServer-->
<!--
<componentname="xaDataSource"
class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
<propertyname="driverClassName">
"net.sourceforge.jtds.jdbc.Driver"
</property>
<propertyname="URL">
"jdbc:jtds:sqlserver://localhost/TEST;instance=SQLEXPRESS"
</property>
<propertyname="user">"xxxx"</property>
<propertyname="password">"xxxx"</property>
</component>
-->
<!--
<componentname="connectionPool"
class="org.seasar.extension.dbcp.impl.ConnectionPoolImpl">
<propertyname="timeout">600</property>
<propertyname="maxPoolSize">10</property>
<propertyname="allowLocalTx">true</property>
<destroyMethodname="close"/>
</component>
<componentname="DataSource"
class="org.seasar.extension.dbcp.impl.DataSourceImpl"
/>
-->
<!--fromJNDI-->
<!--
<componentname="DataSource"
class="javax.sql.DataSource">
@org.seasar.extension.j2ee.JndiResourceLocator@lookup("java:comp/env/jdbc/DataSource")
</component>
-->
<!--
<componentname="DataSource"
class="org.seasar.extension.datasource.impl.SelectableDataSourceProxy"/>
-->
</components>
log4j.properties
log4j.category.org.seasar=DEBUG,C
log4j.additivity.org.seasar=false
log4j.category.tutorial=DEBUG,C
log4j.additivity.tutorial=false
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.Target=System.out
log4j.appender.C.ImmediateFlush=true
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%-5p%d[%t]%m%n
log4j.rootLogger=INFO,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d[%t]%-5p%c-%m%n
s2container.dicon
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEcomponentsPUBLIC"-//SEASAR//DTDS2Container2.4//EN"
"http://www.seasar.org/dtd/components24.dtd">
<components>
<includecondition="#ENV=='ut'"path="warmdeploy.dicon"/>
<includecondition="#ENV=='ct'"path="hotdeploy.dicon"/>
<includecondition="#ENV!='ut'and#ENV!='ct'"path="cooldeploy.dicon"/>
</components>
s2jdbc.dicon
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEcomponentsPUBLIC"-//SEASAR//DTDS2Container2.4//EN"
"http://www.seasar.org/dtd/components24.dtd">
<components>
<includepath="jdbc.dicon"/>
<includepath="s2jdbc-internal.dicon"/>
<componentname="jdbcManager"class="org.seasar.extension.jdbc.manager.JdbcManagerImpl">
<propertyname="maxRows">0</property>
<propertyname="fetchSize">0</property>
<propertyname="queryTimeout">0</property>
<propertyname="dialect">hsqlDialect</property>
<!--
<propertyname="dialect">db2390Dialect</property>
<propertyname="dialect">db2400Dialect</property>
<propertyname="dialect">db2Dialect</property>
<propertyname="dialect">derbyDialect</property>
<propertyname="dialect">firebirdDialect</property>
<propertyname="dialect">h2Dialect</property>
<propertyname="dialect">hsqlDialect</property>
<propertyname="dialect">interbaseDialect</property>
<propertyname="dialect">maxdbDialect</property>
<propertyname="dialect">mssql2005Dialect</property>
<propertyname="dialect">mssqlDialect</property>
<propertyname="dialect">mysqlDialect</property>
<propertyname="dialect">oracleDialect</property>
<propertyname="dialect">postgreDialect</property>
<propertyname="dialect">standardDialect</property>
<propertyname="dialect">sybaseDialect</property>
-->
</component>
</components>
sa.css
@CHARSET"utf-8";
body{background-color:#EEEEEE;}
h1{
border-left:10pxsolid#308faa;
padding-left:156px;
height:33px;
font-size:x-large;
font-family:"TimesNewRoman",Times,serif;
background-image:url("../images/seasar_banner.gif");
background-repeat:no-repeat;
}
ulli{list-style:square;}
a:hover{background-color:white;}
input.err{background-color:orange;}
.layout.header{background-color:orange;}
.layout.menu{background-color:aqua;height:300px;vertical-align:top;}
.layout.content{background-color:white; text-align:center;}
.layout.footer{background-color:black;color:white;}
index.jsp
<%@pagepageEncoding="UTF-8"%>
<html>
<head>
<title>Tutorial:Add</title>
<linkrel="stylesheet"type="text/css"href="${f:url('/css/sa.css')}"/>
</head>
<body>
<h1>Tutorial:Add</h1>
<html:errors/>
<s:form>
<html:textproperty="arg1"/>+
<html:textproperty="arg2"/>
=${f:h(result)}<br/>
<inputtype="submit"name="submit"value="サブミット"/>
</s:form>
</body>
</html>
common.jsp
<%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglibprefix="fmt"uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglibprefix="fn"uri="http://java.sun.com/jsp/jstl/functions"%>
<%@taglibprefix="html"uri="http://struts.apache.org/tags-html"%>
<%@taglibprefix="bean"uri="http://struts.apache.org/tags-bean"%>
<%@taglibprefix="tiles"uri="http://jakarta.apache.org/struts/tags-tiles"%>
<%@taglibprefix="s"uri="http://sastruts.seasar.org"%>
<%@taglibprefix="f"uri="http://sastruts.seasar.org/functions"%>
struts-config.xml
<?xmlversion="1.0"?>
<!DOCTYPEstruts-configPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<!--========================================FormBeanDefinitions-->
<form-beans>
</form-beans>
<!--=================================GlobalExceptionDefinitions-->
<global-exceptions>
<exceptionpath="/WEB-INF/view/error/norole.jsp"key="errors.norole"type="org.seasar.struts.exception.NoRoleRuntimeException"/>
</global-exceptions>
<!--===================================GlobalForwardDefinitions-->
<global-forwards>
</global-forwards>
<!--===================================ActionMappingDefinitions-->
<action-mappings>
</action-mappings>
<!--=====================================ControllerConfiguration-->
<controller
maxFileSize="1024K"
bufferSize="1024"
processorClass="org.seasar.struts.action.S2RequestProcessor"
multipartClass="org.seasar.struts.upload.S2MultipartRequestHandler"/>
<!--================================MessageResourcesDefinitions-->
<message-resourcesparameter="application"
factory="org.seasar.struts.util.S2PropertyMessageResourcesFactory"/>
<!--=======================================PlugInsConfiguration-->
<plug-inclassName="org.seasar.struts.validator.S2ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml"/>
</plug-in>
</struts-config>