asamiのメモ帳

Struts/とりあえず

とりあえず作ってみた


【作りたいもの】
入力画面で名前、年齢、血液型を入力。
出力画面では入力画面で入力したものをそのまま表示。

【作るもの】
  • input.jsp
  • output.jsp
  • ShowActionForm.java
  • ShowAction.java
  • web.xml
  • struts-config.xml
  • ApplicationResources.properties

【eclipse JSPファイルの作成】
プロジェクトの下にとりあえず作ればいいけど、フォルダを作って
そのなかに入れたほうがいいかも。

●フォルダ作成
プロジェクト(TestStruts01)を右クリック→新規→フォルダ→
フォルダ名入力→終了
TestStruts01/jspができた。

●JSPファイルの作成
jspを右クリック→新規→ファイル→ファイル名入力(input.jsp)
TestStruts01/jsp/input.jspができた。
output.jspも同じように作る。

input.jsp
<%@ page contentType="text/html; charset=Shift-JIS"%>
<%@ taglib uri="/struts-html.tld" prefix="html" %>

<html>
<head><title>入力画面</title></head>
<body>

名前・年齢をを入力し、血液型を選択してください。
<br>

<html:form action="/sendData" focus="name">
  名前:
  <html:text property="name" size="16"/>
  <br>
  年齢:
  <html:text property="age" size="8" />
  <br>
  血液型:
  <html:select property="bloodtype">
    <html:option value="A">A</html:option>
    <html:option value="B">B</html:option>
    <html:option value="O">O</html:option>
    <html:option value="AB">AB</html:option>
  </html:select>
  <br><br>
  <html:submit property="submit" value="送信"/>
  <br><br>
</html:form>

</body>
</html>

output.jsp
<%@ page contentType="text/html; charset=Shift-JIS"%>
<%@ taglib uri="/struts-bean.tld" prefix="bean" %>

<html>
<head><title>出力画面</title></head>
<body>

名前:
<bean:write name="form" property="name" scope="session"/>
<br>
年齢:
<bean:write name="form" property="age" scope="session"/> 歳
<br>
血液型:
<bean:write name="form" property="bloodtype" scope="session"/> 型

</body>
</html>

【ActionFormの作成】
WEB-INF/src/にTestActionForm.javaを作成する。
まずは変数の宣言をする。
そのあと、作成したソースのどっかにフォーカスをおいて、
メニューバーの ソース→GetterおよびSetterの生成→
すべて選択→OK
各変数のGetterとSetterができる。

TestActionForm.java
import org.apache.struts.action.ActionForm;


public class TestActionForm extends ActionForm {
  
  private String name;
  private int age;
  private String bloodtype;
  
  //ここから上は手入力。下は自動生成。
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getBloodtype() {
    return bloodtype;
  }
  public void setBloodtype(String bloodtype) {
    this.bloodtype = bloodtype;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  
}

【Actionの作成】
WEB-INF/src/にTestAction.javaを作成。
TestAction.java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


public class TestAction extends Action {
  
  public ActionForward execute(ActionMapping mapping,
                ActionForm form,
                HttpServletRequest req,
                HttpServletResponse resp) {
    
    HttpSession session = req.getSession();
    TestActionForm testForm = (TestActionForm)form;
    session.setAttribute("form", testForm);
    
    return(mapping.findForward("success"));
    
  }

}

【struts-config.xmlの作成】
WEB-INFを右クリック→新規→ファイル→「struts-config.xml」を入力して作成。
struts-config.xml
<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE struts-config PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
 "http://struts.apache.org/dtds/struts-config_1_1.dtd">

<struts-config>
  <form-beans>
    <form-bean name="form" type="TestActionForm" />
  </form-beans>
  <action-mappings>
    <action path="/sendData" type="TestAction" name="form">
      <forward name="success" path="/jsp/output.jsp" />
    </action>
  </action-mappings>
  
  <message-resources parameter="ApplicationResources"/>

</struts-config>

【web.xmlの作成】
struts-config.xmlと同じ場所に、同じ方法で作成。
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <taglib>
    <taglib-uri>/struts-bean.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>
  
  <taglib>
    <taglib-uri>/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>

</web-app>

【ApplicationResources.propertiesの作成】
WEB-INF/srcで右クリック→新規→ファイル→
ApplicationResources.propertiesを入力
とりあえず空ファイルでよし!!
eclipseのフォルダ\workspace\TestStruts01\WEB-INF\classesにファイルがあるか一応確認。

【動かしてみる】
Tomcatを起動

http://localhost:8080/TestStruts01/jsp/input.jspにつないでみると・・・


送信を押すと

【今の問題点】
1.日本語を入力すると文字化けする
2.入力値チェックをしたい

なので次はこれ。
文字化け解消
その次にこれ。
入力値チェック



合計: -
最終更新:2008年07月10日 13:31
添付ファイル