以前的例子:http://blog.csdn.net/kunshan_shenbin/archive/2008/11/04/3220007.aspx
参照上面的例子,创建如下工程:
jar包一览如下图:
代码如下:
AddAction.java
- package tutorial.action;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- import org.seasar.struts.annotation.ActionForm;
- import org.seasar.struts.annotation.Execute;
- import tutorial.dto.AddDto;
- import tutorial.form.AddForm;
- import tutorial.service.AddDtoService;
- import tutorial.service.AddService;
- /**
- * @author shenbin
- */
- public class AddAction {
- public Integer result;
- public AddService addService;
- public AddDtoService addDtoService;
- public HttpServletRequest request;
- public HttpSession session;
- @ActionForm
- @Resource
- protected AddForm addForm;
- @Execute(validator = false)
- public String index() {
- request.setAttribute("r_name", addForm.r_name);
- session.setAttribute("s_name", addForm.s_name);
- return "index.jsp";
- }
- @Execute(input = "index.jsp")
- public String submit() {
- addForm.r_name = (String)request.getAttribute("r_name");
- addForm.s_name = (String)session.getAttribute("s_name");
- result = addService.add(Integer.valueOf(addForm.arg1), Integer.valueOf(addForm.arg2));
- return "index.jsp";
- }
- @Execute(input = "index.jsp")
- public String dtosvc() {
- String arg1 = addForm.arg1;
- String arg2 = addForm.arg2;
- int _result = addService.add(Integer.valueOf(arg1), Integer.valueOf(arg2));
- String result = String.valueOf(_result);
- AddDto addDto = new AddDto();
- addDto.arg1 = arg1;
- addDto.arg2 = arg2;
- addDto.result = result;
- addDtoService.persistence(addDto);
- return "index.jsp";
- }
- }
Add.dicon
- <?xml version="1.0" encoding="Shift_JIS"?>
- <!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN"
- "http://www.seasar.org/dtd/components21.dtd">
- <components>
- <component class="tutorial.service.impl.AddServiceImpl"/>
- <component class="tutorial.service.impl.AddDtoServiceImpl"/>
- </components>
AddDto.java
- package tutorial.dto;
- /**
- * @author shenbin
- */
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- @Entity
- public class AddDto {
- @Id
- @GeneratedValue
- public Integer id;
- public String arg1;
- public String arg2;
- public String result;
- }
AddForm.java
- package tutorial.form;
- import org.seasar.struts.annotation.IntegerType;
- import org.seasar.struts.annotation.Required;
- /**
- * @author shenbin
- */
- public class AddForm {
- public String r_name;
- public String s_name;
- @Required
- @IntegerType
- public String arg1;
- @Required
- @IntegerType
- public String arg2;
- }
AddDtoServiceImpl.java
- package tutorial.service.impl;
- import java.util.List;
- import org.seasar.extension.jdbc.JdbcManager;
- import tutorial.dto.AddDto;
- import tutorial.service.AddDtoService;
- /**
- * @author shenbin
- */
- public class AddDtoServiceImpl implements AddDtoService {
- public JdbcManager jdbcManager;
- public void persistence(AddDto addDto) {
- System.out.println("================INSERT START==================");
- jdbcManager.insert(addDto).execute();
- System.out.println("================INSERT END==================");
- System.out.println("================SELECT START==================");
- long count = jdbcManager.from(AddDto.class).getCount();
- System.out.println(count);
- List<AddDto> results = jdbcManager.from(AddDto.class).getResultList();
- for (AddDto add : results) {
- System.out.println(add.result);
- }
- System.out.println("================SELECT END==================");
- }
- }
AddServiceImpl.java
- package tutorial.service.impl;
- import tutorial.service.AddService;
- /**
- * @author shenbin
- */
- public class AddServiceImpl implements AddService {
- public int add(int arg1, int arg2) {
- return arg1 + arg2;
- }
- }
AddDtoService.java
- package tutorial.service;
- import tutorial.dto.AddDto;
- /**
- * @author shenbin
- */
- public interface AddDtoService {
- public void persistence(AddDto addDto);
- }
AddService.java
- package tutorial.service;
- /**
- * @author shenbin
- */
- public interface AddService {
- public int add(int arg1, int arg2);
- }
配置文件基本和上例一致,只有部分文件需要修改。
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="tutorial/dicon/Add.dicon"/>
- <component name="actionMessagesThrowsInterceptor" class="org.seasar.struts.interceptor.ActionMessagesThrowsInterceptor"/>
- </components>
jdbc.dicon
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN"
- "http://www.seasar.org/dtd/components21.dtd">
- <components namespace="jdbc">
- <include path="jta.dicon"/>
- <!-- for HSQLDB -->
- <component name="xaDataSource"
- class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
- <property name="driverClassName">
- "org.hsqldb.jdbcDriver"
- </property>
- <property name="URL">
- "jdbc:hsqldb:file:"
- + application.getRealPath("/WEB-INF/data")
- + "/test"
- </property>
- <property name="user">"sa"</property>
- <property name="password">""</property>
- </component>
- <!-- for H2 -->
- <!--
- <component name="xaDataSource"
- class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
- <property name="driverClassName">
- "org.h2.Driver"
- </property>
- <property name="URL">
- "jdbc:h2:file:"
- + @org.seasar.framework.util.ResourceUtil@getBuildDir(@examples.entity.JdbcManagerTest@class).getCanonicalPath()
- + "/data/demo;DB_CLOSE_ON_EXIT=FALSE"
- </property>
- <property name="user">"sa"</property>
- <property name="password"></property>
- </component>
- -->
- <!-- for Oracle -->
- <!--
- <component name="xaDataSource"
- class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
- <property name="driverClassName">
- "oracle.jdbc.driver.OracleDriver"
- </property>
- <property name="URL">
- "jdbc:oracle:thin:@xxx:1521:xxx"
- </property>
- <property name="user">"xxx"</property>
- <property name="password">"xxx"</property>
- </component>
- -->
- <!-- for DB2 -->
- <!--
- <component name="xaDataSource"
- class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
- <property name="driverClassName">
- "com.ibm.db2.jcc.DB2Driver"
- </property>
- <property name="URL">
- "jdbc:db2://foo.bar.com:50000/SAMPLE"
- </property>
- <property name="user">"db2user"</property>
- <property name="password">"db2password"</property>
- <initMethod name="addProperty">
- <arg>"currentSchema"</arg>
- <arg>"SCHEMA"</arg>
- </initMethod>
- </component>
- -->
- <!-- for PostgreSQL -->
- <!--
- <component name="xaDataSource"
- class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
- <property name="driverClassName">
- "org.postgresql.Driver"
- </property>
- <property name="URL">
- "jdbc:postgresql://localhost/TEST"
- </property>
- <property name="user">"xxxx"</property>
- <property name="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)の変換が
- 行なわれるようになります.この結果,文字化けを防げます.
- なおJIS X 0212(補助漢字)を使用する場合は,Windows-31Jではなく
- UTF-8を指定する必要があります.
- - 上記以外の場合は,my.cnfでdefault-character-setの設定を適切に
- (cp932やutf8など.デフォルトのlatin1は不可)行なっていれば,
- 文字化けは防げます.
- <component name="xaDataSource"
- class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
- <property name="driverClassName">
- "com.mysql.jdbc.Driver"
- </property>
- <property name="URL">
- "jdbc:mysql://localhost:3306/test"
- </property>
- <property name="user">"xxx"</property>
- <property name="password">"xxx"</property>
- </component>
- -->
- <!-- for SQLServer -->
- <!--
- <component name="xaDataSource"
- class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
- <property name="driverClassName">
- "net.sourceforge.jtds.jdbc.Driver"
- </property>
- <property name="URL">
- "jdbc:jtds:sqlserver://localhost/TEST;instance=SQLEXPRESS"
- </property>
- <property name="user">"xxxx"</property>
- <property name="password">"xxxx"</property>
- </component>
- -->
- <component name="connectionPool"
- class="org.seasar.extension.dbcp.impl.ConnectionPoolImpl">
- <property name="timeout">600</property>
- <property name="maxPoolSize">10</property>
- <property name="allowLocalTx">true</property>
- <destroyMethod name="close"/>
- </component>
- <component name="DataSource"
- class="org.seasar.extension.dbcp.impl.DataSourceImpl"
- />
- <!-- from JNDI -->
- <!--
- <component name="DataSource"
- class="javax.sql.DataSource">
- @org.seasar.extension.j2ee.JndiResourceLocator@lookup("java:comp/env/jdbc/DataSource")
- </component>
- -->
- <!--
- <component name="DataSource"
- class="org.seasar.extension.datasource.impl.SelectableDataSourceProxy"/>
- -->
- </components>
其他无需变化。
jsp文件部分
/view/add/index.jsp
- <%@page pageEncoding="UTF-8"%>
- <html>
- <head>
- <title>Tutorial: Add</title>
- <link rel="stylesheet" type="text/css" href="${f:url('/css/sa.css')}" />
- </head>
- <body>
- <h1>Tutorial: Add</h1>
- <html:errors/>
- <s:form>
- request:<html:text property="r_name" value="request"/>
- session:<html:text property="s_name" value="session"/>
- <br/>
- <br/>
- <html:text property="arg1"/> +
- <html:text property="arg2"/>
- = ${f:h(result)}<br />
- <input type="submit" name="submit" value="サブミット"/>
- <br>
- <input type="submit" name="dtosvc" value="Persistence"/>
- </s:form>
- </body>
- </html>
数据库初始文件:
WEB-INF/data/test.properties
- #HSQL Database Engine
- #Sat Nov 03 19:49:53 JST 2007
- hsqldb.script_format=0
- runtime.gc_interval=0
- sql.enforce_strict_size=false
- hsqldb.cache_size_scale=8
- readonly=false
- hsqldb.nio_data_file=true
- hsqldb.cache_scale=14
- version=1.8.0
- hsqldb.default_table_type=memory
- hsqldb.cache_file_scale=1
- hsqldb.log_size=200
- modified=yes
- hsqldb.cache_version=1.7.0
- hsqldb.original_version=1.8.0
- hsqldb.compatible_version=1.8.0
test.script
- CREATE SCHEMA PUBLIC AUTHORIZATION DBA
- CREATE USER SA PASSWORD ""
- GRANT DBA TO SA
- SET WRITE_DELAY 20
- SET SCHEMA PUBLIC
- create table ADD_DTO (ID integer generated by default as identity, ARG1 varchar(255) not null, ARG2 varchar(255) not null, RESULT varchar(255) not null)
如次,在原来的基础上追加了services层和dto层协同工作。