サンプル

ディレクトリ構成

wepアプリディレクトリ
    + html、jspなどのファイル
 
    + WEB-INFディレクトリ
        + web.xml
 
        + Struts関連xml
 
        + Struts関連tld
 
        + classesディレクトリ
            + クラスファイル
 
        +libディレクトリ
            + ライブラリファイル
 
        +resourcesディレクトリ
            + リソースファイル
 

表示用JSP

  1. <%@ page contentType="text/html; charset=Shift_JIS" %>
  2. <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
  3.  
  4. <html:html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html"; charset="Shift_Jis" />
  7. <title></title>
  8. </head>
  9. <body>
  10. <html:form method="post" action="action1.do">
  11. <html:text property="text1" readonly="true" size="40" />
  12. <br />
  13. <html:text property="field1" />
  14. <br />
  15. <html:submit value="OK" />
  16. </html:form>
  17. </body>
  18. </html:html>
  19.  

アクションフォームクラス(Page1ActionForm.java)

  1. package jp.sample.framework;
  2.  
  3. import java.io.*;
  4. import javax.servlet.http.*;
  5. import org.apache.struts.action.*;
  6.  
  7. public class Page1ActionForm extends ActionForm{
  8. private static final long serialVersionUID = 1L;
  9. private String text1;
  10. private String field1;
  11.  
  12. public String getText1(){
  13. return this.text1;
  14. }
  15. public void setText1(String text1){
  16. this.text1 = text1;
  17. }
  18.  
  19. public String getField1(){
  20. return this.field1;
  21. }
  22.  
  23. public void setField1(String field1){
  24. this.field1 = field1;
  25. }
  26.  
  27. public void reset(ActionMapping mapping,
  28. HttpServletRequest request){
  29.  
  30. super.reset(mapping, request);
  31. try{
  32. request.setCharacterEncoding("Shift_JIS");
  33. this.setText1("お名前は?");
  34.  
  35. }catch(Exception ex){
  36. ex.printStackTrace();
  37.  
  38. }
  39.  
  40. }
  41. }
  42.  

アクションクラス(Page1Action.java)

  1. package jp.sample.framework;
  2.  
  3. import javax.servlet.http.*;
  4. import org.apache.struts.action.*;
  5.  
  6. public class Page1Action extends Action{
  7. public ActionForward execute(ActionMapping mapping,
  8. ActionForm form,
  9. HttpServletRequest request,
  10. HttpServletResponse response){
  11.  
  12. Page1ActionForm page1Form = (Page1ActionForm)form;
  13. String input = page1Form.getField1();
  14. page1Form.setText1("こんにちは" + input + "さん!");
  15.  
  16. return mapping.getInputForward();
  17.  
  18.  
  19. }
  20. }
  21.  

設定ファイル(web.xml)

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9.  
  10. http://www.apache.org/licenses/LICENSE-2.0
  11.  
  12. Unless required by applicable law or agreed to in writing, software
  13. distributed under the License is distributed on an "AS IS" BASIS,
  14. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. See the License for the specific language governing permissions and
  16. limitations under the License.
  17. -->
  18.  
  19. <!DOCTYPE web-app PUBLIC
  20. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  21. "http://java.sun.com/dtd/web-app_2_3.dtd">
  22.  
  23. <web-app>
  24. <display-name>Struts Blank Application</display-name>
  25.  
  26. <!-- Standard Action Servlet Configuration -->
  27. <servlet>
  28. <servlet-name>action</servlet-name>
  29. <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  30. <init-param>
  31. <param-name>config</param-name>
  32. <param-value>/WEB-INF/struts-config.xml</param-value>
  33. </init-param>
  34. <load-on-startup>2</load-on-startup>
  35. </servlet>
  36.  
  37.  
  38. <!-- Standard Action Servlet Mapping -->
  39. <servlet-mapping>
  40. <servlet-name>action</servlet-name>
  41. <url-pattern>*.do</url-pattern>
  42. </servlet-mapping>
  43.  
  44.  
  45. <!-- The Usual Welcome File List -->
  46. <welcome-file-list>
  47. <welcome-file>index.jsp</welcome-file>
  48. </welcome-file-list>
  49.  
  50. </web-app>
  51.  

設定ファイル(struts-config.xml)

  1. <?xml version="1.0" encoding="ISO-8859-1" ?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9.  
  10. http://www.apache.org/licenses/LICENSE-2.0
  11.  
  12. Unless required by applicable law or agreed to in writing, software
  13. distributed under the License is distributed on an "AS IS" BASIS,
  14. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. See the License for the specific language governing permissions and
  16. limitations under the License.
  17. -->
  18.  
  19. <!DOCTYPE struts-config PUBLIC
  20. "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
  21. "http://struts.apache.org/dtds/struts-config_1_3.dtd">
  22.  
  23. <!--
  24. This is a blank Struts configuration file with an example
  25. welcome action/page and other commented sample elements.
  26.  
  27. Struts Validator is configured using the factory defaults
  28. and is ready-to-use.
  29.  
  30. NOTE: If you have a generator tool to create the corresponding Java classes
  31. for you, you could include the details in the "form-bean" declarations.
  32. Otherwise, you would only define the "form-bean" element itself, with the
  33. corresponding "name" and "type" attributes, as shown here.
  34. -->
  35.  
  36.  
  37. <struts-config>
  38.  
  39.  
  40. <!-- ================================================ Form Bean Definitions -->
  41.  
  42. <form-beans>
  43. <!-- sample form bean descriptor for an ActionForm
  44. <form-bean
  45. name="inputForm"
  46. type="app.InputForm"/>
  47. end sample -->
  48.  
  49. <!-- sample form bean descriptor for a DynaActionForm
  50. <form-bean
  51. name="logonForm"
  52. type="org.apache.struts.action.DynaActionForm">
  53. <form-property
  54. name="username"
  55. type="java.lang.String"/>
  56. <form-property
  57. name="password"
  58. type="java.lang.String"/>
  59. </form-bean>
  60. end sample -->
  61. <!-- アクションフォーム定義 -->
  62. <form-bean
  63. name="page1form"
  64. type="jp.sample.framework.Page1ActionForm" />
  65. </form-beans>
  66.  
  67.  
  68. <!-- ========================================= Global Exception Definitions -->
  69.  
  70. <global-exceptions>
  71. <!-- sample exception handler
  72. <exception
  73. key="expired.password"
  74. type="app.ExpiredPasswordException"
  75. path="/changePassword.jsp"/>
  76. end sample -->
  77. </global-exceptions>
  78.  
  79.  
  80. <!-- =========================================== Global Forward Definitions -->
  81.  
  82. <global-forwards>
  83. <!-- Default forward to "Welcome" action -->
  84. <!-- Demonstrates using index.jsp to forward -->
  85. <forward
  86. name="welcome"
  87. path="/Welcome.do"/>
  88. </global-forwards>
  89.  
  90.  
  91. <!-- =========================================== Action Mapping Definitions -->
  92.  
  93. <action-mappings>
  94. <!-- Default "Welcome" action -->
  95. <!-- Forwards to Welcome.jsp -->
  96. <action
  97. path="/Welcome"
  98. forward="/pages/Welcome.jsp"/>
  99.  
  100. <!-- sample input and input submit actions
  101.  
  102. <action
  103. path="/Input"
  104. type="org.apache.struts.actions.ForwardAction"
  105. parameter="/pages/Input.jsp"/>
  106.  
  107. <action
  108. path="/InputSubmit"
  109. type="app.InputAction"
  110. name="inputForm"
  111. scope="request"
  112. validate="true"
  113. input="/pages/Input.jsp"/>
  114.  
  115. <action
  116. path="/edit*"
  117. type="app.Edit{1}Action"
  118. name="inputForm"
  119. scope="request"
  120. validate="true"
  121. input="/pages/Edit{1}.jsp"/>
  122.  
  123. end samples -->
  124. <!-- マッピング -->
  125. <action
  126. path="/action1"
  127. name="page1form"
  128. type="jp.sample.framework.Page1Action"
  129. input="/page1.jsp" />
  130. </action-mappings>
  131.  
  132.  
  133. <!-- ======================================== Message Resources Definitions -->
  134.  
  135. <message-resources parameter="MessageResources" />
  136.  
  137.  
  138. <!-- =============================================== Plug Ins Configuration -->
  139.  
  140. <!-- ======================================================= Tiles plugin -->
  141. <!--
  142. This plugin initialize Tiles definition factory. This later can takes some
  143. parameters explained here after. The plugin first read parameters from
  144. web.xml, thenoverload them with parameters defined here. All parameters
  145. are optional.
  146. The plugin should be declared in each struts-config file.
  147. - definitions-config: (optional)
  148. Specify configuration file names. There can be several comma
  149. separated file names (default: ?? )
  150. - moduleAware: (optional - struts1.1)
  151. Specify if the Tiles definition factory is module aware. If true
  152. (default), there will be one factory for each Struts module.
  153. If false, there will be one common factory for all module. In this
  154. later case, it is still needed to declare one plugin per module.
  155. The factory will be initialized with parameters found in the first
  156. initialized plugin (generally the one associated with the default
  157. module).
  158. true : One factory per module. (default)
  159. false : one single shared factory for all modules
  160. - definitions-parser-validate: (optional)
  161. Specify if xml parser should validate the Tiles configuration file.
  162. true : validate. DTD should be specified in file header (default)
  163. false : no validation
  164.  
  165. Paths found in Tiles definitions are relative to the main context.
  166.  
  167. To use this plugin, download and add the Tiles jar to your WEB-INF/lib
  168. directory then uncomment the plugin definition below.
  169.  
  170. <plug-in className="org.apache.struts.tiles.TilesPlugin" >
  171.  
  172. <set-property property="definitions-config"
  173. value="/WEB-INF/tiles-defs.xml" />
  174. <set-property property="moduleAware" value="true" />
  175. </plug-in>
  176. -->
  177.  
  178.  
  179. <!-- =================================================== Validator plugin -->
  180.  
  181. <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
  182. <set-property
  183. property="pathnames"
  184. value="/org/apache/struts/validator/validator-rules.xml,
  185. /WEB-INF/validation.xml"/>
  186. </plug-in>
  187.  
  188. </struts-config>
  189.  
  190.  
  191.  

確認

表示

submitによる更新

サンプル

基本
バリデータチェック1(validate)
バリデータチェック2(validation.xml)
バリデータチェック3(validate+validation.xml)
ファイルアップロード
ファイルダウンロード
tilesによるテンプレート1
tilesによるテンプレート2(tiles-defs.xml)





最終更新:2010年08月31日 09:49