日系框架之seasar2(SAStruts)升级篇

来源:互联网 发布:淘宝男士灯芯绒衬衫 编辑:程序博客网 时间:2024/04/28 19:54

以前的例子:http://blog.csdn.net/kunshan_shenbin/archive/2008/11/04/3220007.aspx

参照上面的例子,创建如下工程:

jar包一览如下图:

代码如下:

AddAction.java

  1. package tutorial.action;
  2. import javax.annotation.Resource;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpSession;
  5. import org.seasar.struts.annotation.ActionForm;
  6. import org.seasar.struts.annotation.Execute;
  7. import tutorial.dto.AddDto;
  8. import tutorial.form.AddForm;
  9. import tutorial.service.AddDtoService;
  10. import tutorial.service.AddService;
  11. /**
  12.  * @author shenbin
  13.  */
  14. public class AddAction {
  15.     
  16.     public Integer result;
  17.     public AddService addService;
  18.     public AddDtoService addDtoService;
  19.     
  20.     public HttpServletRequest request;
  21.     public HttpSession session;
  22.     
  23.     @ActionForm
  24.     @Resource
  25.     protected AddForm addForm;
  26.     @Execute(validator = false)
  27.     public String index() {
  28.         
  29.         request.setAttribute("r_name", addForm.r_name);
  30.         session.setAttribute("s_name", addForm.s_name);
  31.         
  32.         return "index.jsp";
  33.     }
  34.     @Execute(input = "index.jsp")
  35.     public String submit() {
  36.         
  37.         addForm.r_name = (String)request.getAttribute("r_name");
  38.         addForm.s_name = (String)session.getAttribute("s_name");
  39.         
  40.         result = addService.add(Integer.valueOf(addForm.arg1), Integer.valueOf(addForm.arg2));
  41.         
  42.         return "index.jsp";
  43.     }
  44.     
  45.     @Execute(input = "index.jsp")
  46.     public String dtosvc() {
  47.         
  48.         String arg1 = addForm.arg1;
  49.         String arg2 = addForm.arg2;
  50.         int _result = addService.add(Integer.valueOf(arg1), Integer.valueOf(arg2));
  51.         String result = String.valueOf(_result);
  52.         
  53.         AddDto addDto = new AddDto();
  54.         addDto.arg1 = arg1;
  55.         addDto.arg2 = arg2;
  56.         addDto.result = result;
  57.         
  58.         addDtoService.persistence(addDto);
  59.         
  60.         return "index.jsp";
  61.     }
  62. }

Add.dicon

  1. <?xml version="1.0" encoding="Shift_JIS"?>
  2. <!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN"
  3.     "http://www.seasar.org/dtd/components21.dtd">
  4. <components>
  5.     <component class="tutorial.service.impl.AddServiceImpl"/>
  6.     <component class="tutorial.service.impl.AddDtoServiceImpl"/>
  7. </components>

AddDto.java

  1. package tutorial.dto;
  2. /**
  3.  * @author shenbin
  4.  */
  5. import javax.persistence.Entity;
  6. import javax.persistence.GeneratedValue;
  7. import javax.persistence.Id;
  8. @Entity
  9. public class AddDto {
  10.     @Id
  11.     @GeneratedValue
  12.     public Integer id;
  13.     
  14.     public String arg1;
  15.     public String arg2;
  16.     public String result;
  17. }

AddForm.java

  1. package tutorial.form;
  2. import org.seasar.struts.annotation.IntegerType;
  3. import org.seasar.struts.annotation.Required;
  4. /**
  5.  * @author shenbin
  6.  */
  7. public class AddForm {
  8.     public String r_name;
  9.     public String s_name;
  10.     
  11.     @Required
  12.     @IntegerType
  13.     public String arg1;
  14.     @Required
  15.     @IntegerType
  16.     public String arg2;
  17. }

AddDtoServiceImpl.java

  1. package tutorial.service.impl;
  2. import java.util.List;
  3. import org.seasar.extension.jdbc.JdbcManager;
  4. import tutorial.dto.AddDto;
  5. import tutorial.service.AddDtoService;
  6. /**
  7.  * @author shenbin
  8.  */
  9. public class AddDtoServiceImpl implements AddDtoService {
  10.     public JdbcManager jdbcManager;
  11.     
  12.     public void persistence(AddDto addDto) {
  13.         
  14.         System.out.println("================INSERT START==================");
  15.         jdbcManager.insert(addDto).execute();
  16.         System.out.println("================INSERT   END==================");
  17.         
  18.         System.out.println("================SELECT START==================");
  19.         long count = jdbcManager.from(AddDto.class).getCount();
  20.         System.out.println(count);
  21.         List<AddDto> results = jdbcManager.from(AddDto.class).getResultList();
  22.         for (AddDto add : results) {
  23.             System.out.println(add.result);
  24.         }
  25.         System.out.println("================SELECT   END==================");
  26.     }
  27. }

AddServiceImpl.java

  1. package tutorial.service.impl;
  2. import tutorial.service.AddService;
  3. /**
  4.  * @author shenbin
  5.  */
  6. public class AddServiceImpl implements AddService {
  7.     public int add(int arg1, int arg2) {
  8.         return arg1 + arg2;
  9.     }
  10. }

AddDtoService.java

  1. package tutorial.service;
  2. import tutorial.dto.AddDto;
  3. /**
  4.  * @author shenbin
  5.  */
  6. public interface AddDtoService {
  7.     public void persistence(AddDto addDto);
  8. }

AddService.java

  1. package tutorial.service;
  2. /**
  3.  * @author shenbin
  4.  */
  5. public interface AddService {
  6.     public int add(int arg1, int arg2);
  7. }

配置文件基本和上例一致,只有部分文件需要修改。

app.dicon

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN"
  3.     "http://www.seasar.org/dtd/components24.dtd">
  4. <components>
  5.     <include path="convention.dicon"/>
  6.     <include path="aop.dicon"/>
  7.     <include path="j2ee.dicon"/>
  8.     <include path="s2jdbc.dicon"/>
  9.     
  10.     <include path="tutorial/dicon/Add.dicon"/>
  11.     
  12.     <component name="actionMessagesThrowsInterceptor" class="org.seasar.struts.interceptor.ActionMessagesThrowsInterceptor"/>
  13. </components>

jdbc.dicon

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN"
  3.     "http://www.seasar.org/dtd/components21.dtd">
  4. <components namespace="jdbc">
  5.     <include path="jta.dicon"/>
  6.     <!-- for HSQLDB -->
  7.     <component name="xaDataSource"
  8.         class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
  9.         <property name="driverClassName">
  10.             "org.hsqldb.jdbcDriver"
  11.         </property>
  12.         <property name="URL">
  13.             "jdbc:hsqldb:file:"
  14.                 + application.getRealPath("/WEB-INF/data")
  15.                 + "/test"
  16.         </property>
  17.         <property name="user">"sa"</property>
  18.         <property name="password">""</property>
  19.     </component>
  20.     <!-- for H2 -->
  21.     <!--
  22.     <component name="xaDataSource"
  23.         class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
  24.         <property name="driverClassName">
  25.             "org.h2.Driver"
  26.         </property>
  27.         <property name="URL">
  28.             "jdbc:h2:file:"
  29.                 + @org.seasar.framework.util.ResourceUtil@getBuildDir(@examples.entity.JdbcManagerTest@class).getCanonicalPath()
  30.                 + "/data/demo;DB_CLOSE_ON_EXIT=FALSE"
  31.         </property>
  32.         <property name="user">"sa"</property>
  33.         <property name="password"></property>
  34.     </component>
  35.     -->
  36.     
  37.     <!-- for Oracle -->
  38.     <!--
  39.     <component name="xaDataSource"
  40.         class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
  41.         <property name="driverClassName">
  42.             "oracle.jdbc.driver.OracleDriver"
  43.         </property>
  44.         <property name="URL">
  45.             "jdbc:oracle:thin:@xxx:1521:xxx"
  46.         </property>
  47.         <property name="user">"xxx"</property>
  48.         <property name="password">"xxx"</property>
  49.     </component>
  50.     -->
  51.     <!-- for DB2 -->
  52.     <!--
  53.     <component name="xaDataSource"
  54.         class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
  55.         <property name="driverClassName">
  56.             "com.ibm.db2.jcc.DB2Driver"
  57.         </property>
  58.         <property name="URL">
  59.             "jdbc:db2://foo.bar.com:50000/SAMPLE"
  60.         </property>
  61.         <property name="user">"db2user"</property>
  62.         <property name="password">"db2password"</property>
  63.         <initMethod name="addProperty">
  64.             <arg>"currentSchema"</arg>
  65.             <arg>"SCHEMA"</arg>
  66.         </initMethod>
  67.     </component>
  68.     -->
  69.     
  70.     <!-- for PostgreSQL -->
  71.     <!--
  72.     <component name="xaDataSource"
  73.         class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
  74.         <property name="driverClassName">
  75.             "org.postgresql.Driver"
  76.         </property>
  77.         <property name="URL">
  78.           "jdbc:postgresql://localhost/TEST"
  79.         </property>
  80.         <property name="user">"xxxx"</property>
  81.         <property name="password">"xxxx"</property>
  82.     </component>
  83.     -->
  84.     <!-- MySQL
  85.     - MySQL4.0以下でマルチバイト文字を扱う場合には,URL指定の後に,
  86.         以下の接続パラメータを追加で指定してください.
  87.         useUnicode=true
  88.         characterEncoding=[MySQLのエンコーディングに対応した
  89.         Javaのエンコーディング名]
  90.         例:"jdbc:mysql://localhost:3306/test?useUnicode=true" +
  91.         "&characterEncoding=Windows-31J"
  92.         
  93.     - MySQL5.0以降で,エンコーディングがeucjpmsの列を扱う場合には,
  94.         URL指定の後に,以下の接続パラメータを追加で指定してください.
  95.         characterEncoding=UTF-8またはWindows-31J
  96.         characterSetResults=UTF-8またはWindows-31J
  97.         例:"jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8" +
  98.         "&characterSetResults=UTF-8"
  99.         この指定をしない場合,Java側でUCS-2⇔EUC_JP_Solarisの変換が
  100.         行なわれるため,ユーザー定義外字などの一部の文字が化けます.
  101.         この指定をすることで,MySQL側でeucjpms⇔ucs2⇔utf8(cp932)の
  102.         変換が行なわれ,Java側でUCS-2⇔UTF-8(Windows-31J)の変換が
  103.         行なわれるようになります.この結果,文字化けを防げます.
  104.         なおJIS X 0212(補助漢字)を使用する場合は,Windows-31Jではなく
  105.         UTF-8を指定する必要があります.
  106.         
  107.     - 上記以外の場合は,my.cnfでdefault-character-setの設定を適切に
  108.         (cp932やutf8など.デフォルトのlatin1は不可)行なっていれば,
  109.         文字化けは防げます.
  110.     <component name="xaDataSource"
  111.         class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
  112.         <property name="driverClassName">
  113.             "com.mysql.jdbc.Driver"
  114.         </property>
  115.         <property name="URL">
  116.             "jdbc:mysql://localhost:3306/test"
  117.         </property>
  118.         <property name="user">"xxx"</property>
  119.         <property name="password">"xxx"</property>
  120.     </component>
  121.     -->
  122.     <!-- for SQLServer -->
  123.     <!--
  124.     <component name="xaDataSource"
  125.         class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
  126.         <property name="driverClassName">
  127.             "net.sourceforge.jtds.jdbc.Driver"
  128.         </property>
  129.         <property name="URL">
  130.             "jdbc:jtds:sqlserver://localhost/TEST;instance=SQLEXPRESS"
  131.         </property>
  132.         <property name="user">"xxxx"</property>
  133.         <property name="password">"xxxx"</property>
  134.     </component>
  135.     -->
  136.     <component name="connectionPool"
  137.         class="org.seasar.extension.dbcp.impl.ConnectionPoolImpl">
  138.         <property name="timeout">600</property>
  139.         <property name="maxPoolSize">10</property>
  140.         <property name="allowLocalTx">true</property>
  141.         <destroyMethod name="close"/>
  142.     </component>
  143.     <component name="DataSource"
  144.         class="org.seasar.extension.dbcp.impl.DataSourceImpl"
  145.     />
  146.     <!-- from JNDI -->
  147.     <!--
  148.     <component name="DataSource"
  149.         class="javax.sql.DataSource">
  150.         @org.seasar.extension.j2ee.JndiResourceLocator@lookup("java:comp/env/jdbc/DataSource")
  151.     </component>
  152.     -->
  153.     <!--
  154.     <component name="DataSource"
  155.         class="org.seasar.extension.datasource.impl.SelectableDataSourceProxy"/>
  156.     -->
  157. </components>

其他无需变化。

jsp文件部分

/view/add/index.jsp

  1. <%@page pageEncoding="UTF-8"%>
  2. <html>
  3. <head>
  4. <title>Tutorial: Add</title>
  5. <link rel="stylesheet" type="text/css" href="${f:url('/css/sa.css')}" />
  6. </head>
  7. <body>
  8. <h1>Tutorial: Add</h1>
  9. <html:errors/>
  10. <s:form>
  11. request:<html:text property="r_name" value="request"/>
  12. session:<html:text property="s_name" value="session"/>
  13. <br/>
  14. <br/>
  15. <html:text property="arg1"/> +
  16. <html:text property="arg2"/>
  17. = ${f:h(result)}<br />
  18. <input type="submit" name="submit" value="サブミット"/>
  19. <br>
  20. <input type="submit" name="dtosvc" value="Persistence"/>
  21. </s:form>
  22. </body>
  23. </html>

数据库初始文件:

WEB-INF/data/test.properties

  1. #HSQL Database Engine
  2. #Sat Nov 03 19:49:53 JST 2007
  3. hsqldb.script_format=0
  4. runtime.gc_interval=0
  5. sql.enforce_strict_size=false
  6. hsqldb.cache_size_scale=8
  7. readonly=false
  8. hsqldb.nio_data_file=true
  9. hsqldb.cache_scale=14
  10. version=1.8.0
  11. hsqldb.default_table_type=memory
  12. hsqldb.cache_file_scale=1
  13. hsqldb.log_size=200
  14. modified=yes
  15. hsqldb.cache_version=1.7.0
  16. hsqldb.original_version=1.8.0
  17. hsqldb.compatible_version=1.8.0

test.script

  1. CREATE SCHEMA PUBLIC AUTHORIZATION DBA
  2. CREATE USER SA PASSWORD ""
  3. GRANT DBA TO SA
  4. SET WRITE_DELAY 20
  5. SET SCHEMA PUBLIC
  6. 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层协同工作。