「Struts/とりあえず」の編集履歴(バックアップ)一覧はこちら
「Struts/とりあえず」(2008/07/10 (木) 13:31:13) の最新版変更点
追加された行は緑色になります。
削除された行は赤色になります。
**とりあえず作ってみた
【作りたいもの】
入力画面で名前、年齢、血液型を入力。
出力画面では入力画面で入力したものをそのまま表示。
【作るもの】
-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
#divstyle(background-color:#FFFFCC;width:400px;height:200px;overflow:auto;){
<%@ 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">
&space(2)名前:
&space(2)<html:text property="name" size="16"/>
&space(2)<br>
&space(2)年齢:
&space(2)<html:text property="age" size="8" />
&space(2)<br>
&space(2)血液型:
&space(2)<html:select property="bloodtype">
&space(4)<html:option value="A">A</html:option>
&space(4)<html:option value="B">B</html:option>
&space(4)<html:option value="O">O</html:option>
&space(4)<html:option value="AB">AB</html:option>
&space(2)</html:select>
&space(2)<br><br>
&space(2)<html:submit property="submit" value="送信"/>
&space(2)<br><br>
</html:form>
</body>
</html>
}
output.jsp
#divstyle(background-color:#FFFFCC;width:400px;height:200px;overflow:auto;){
<%@ 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
#divstyle(background-color:#FFFFCC;width:400px;height:200px;overflow:auto;){{
import org.apache.struts.action.ActionForm;
public class TestActionForm extends ActionForm {
&space(2)
&space(2)private String name;
&space(2)private int age;
&space(2)private String bloodtype;
&space(2)
&space(2)//ここから上は手入力。下は自動生成。
&space(2)public int getAge() {
&space(4)return age;
&space(2)}
&space(2)public void setAge(int age) {
&space(4)this.age = age;
&space(2)}
&space(2)public String getBloodtype() {
&space(4)return bloodtype;
&space(2)}
&space(2)public void setBloodtype(String bloodtype) {
&space(4)this.bloodtype = bloodtype;
&space(2)}
&space(2)public String getName() {
&space(4)return name;
&space(2)}
&space(2)public void setName(String name) {
&space(4)this.name = name;
&space(2)}
&space(2)
}
}}
【Actionの作成】
WEB-INF/src/にTestAction.javaを作成。
TestAction.java
#divstyle(background-color:#FFFFCC;width:400px;height:200px;overflow:auto;){{
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 {
&space(2)
&space(2)public ActionForward execute(ActionMapping mapping,
&space(16)ActionForm form,
&space(16)HttpServletRequest req,
&space(16)HttpServletResponse resp) {
&space(4)
&space(4)HttpSession session = req.getSession();
&space(4)TestActionForm testForm = (TestActionForm)form;
&space(4)session.setAttribute("form", testForm);
&space(4)
&space(4)return(mapping.findForward("success"));
&space(4)
&space(2)}
}
}}
【struts-config.xmlの作成】
WEB-INFを右クリック→新規→ファイル→「struts-config.xml」を入力して作成。
struts-config.xml
#divstyle(background-color:#FFFFCC;width:400px;height:200px;overflow:auto;){{
<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE struts-config PUBLIC
&space(1)"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
&space(1)"&html(http://struts.apache.org/dtds/struts-config_1_1.dtd)">
<struts-config>
&space(2)<form-beans>
&space(4)<form-bean name="form" type="TestActionForm" />
&space(2)</form-beans>
&space(2)<action-mappings>
&space(4)<action path="/sendData" type="TestAction" name="form">
&space(6)<forward name="success" path="/jsp/output.jsp" />
&space(4)</action>
&space(2)</action-mappings>
&space(2)
&space(2)<message-resources parameter="ApplicationResources"/>
</struts-config>
}}
【web.xmlの作成】
struts-config.xmlと同じ場所に、同じ方法で作成。
web.xml
#divstyle(background-color:#FFFFCC;width:400px;height:200px;overflow:auto;){{
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
&space(2)PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
&space(2)"&html(http://java.sun.com/dtd/web-app_2_3.dtd)">
<web-app>
&space(2)<servlet>
&space(4)<servlet-name>action</servlet-name>
&space(4)<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
&space(4)<load-on-startup>1</load-on-startup>
&space(2)</servlet>
&space(2)<servlet-mapping>
&space(4)<servlet-name>action</servlet-name>
&space(4)<url-pattern>*.do</url-pattern>
&space(2)</servlet-mapping>
&space(2)<taglib>
&space(4)<taglib-uri>/struts-bean.tld</taglib-uri>
&space(4)<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
&space(2)</taglib>
&space(2)
&space(2)<taglib>
&space(4)<taglib-uri>/struts-html.tld</taglib-uri>
&space(4)<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
&space(2)</taglib>
</web-app>
}}
【ApplicationResources.propertiesの作成】
WEB-INF/srcで右クリック→新規→ファイル→
ApplicationResources.propertiesを入力
とりあえず空ファイルでよし!!
eclipseのフォルダ\workspace\TestStruts01\WEB-INF\classesにファイルがあるか一応確認。
【動かしてみる】
Tomcatを起動
http://localhost:8080/TestStruts01/jsp/input.jspにつないでみると・・・
&ref(input.JPG)
送信を押すと
&ref(output.JPG)
【今の問題点】
1.日本語を入力すると文字化けする
2.入力値チェックをしたい
なので次はこれ。
[[文字化け解消>/Struts/文字化け解消]]
その次にこれ。
[[入力値チェック>/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
#divstyle(background-color:#FFFFCC;width:400px;height:200px;overflow:auto;){
<%@ 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">
&space(2)名前:
&space(2)<html:text property="name" size="16"/>
&space(2)<br>
&space(2)年齢:
&space(2)<html:text property="age" size="8" />
&space(2)<br>
&space(2)血液型:
&space(2)<html:select property="bloodtype">
&space(4)<html:option value="A">A</html:option>
&space(4)<html:option value="B">B</html:option>
&space(4)<html:option value="O">O</html:option>
&space(4)<html:option value="AB">AB</html:option>
&space(2)</html:select>
&space(2)<br><br>
&space(2)<html:submit property="submit" value="送信"/>
&space(2)<br><br>
</html:form>
</body>
</html>
}
output.jsp
#divstyle(background-color:#FFFFCC;width:400px;height:200px;overflow:auto;){
<%@ 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
#divstyle(background-color:#FFFFCC;width:400px;height:200px;overflow:auto;){{
import org.apache.struts.action.ActionForm;
public class TestActionForm extends ActionForm {
&space(2)
&space(2)private String name;
&space(2)private int age;
&space(2)private String bloodtype;
&space(2)
&space(2)//ここから上は手入力。下は自動生成。
&space(2)public int getAge() {
&space(4)return age;
&space(2)}
&space(2)public void setAge(int age) {
&space(4)this.age = age;
&space(2)}
&space(2)public String getBloodtype() {
&space(4)return bloodtype;
&space(2)}
&space(2)public void setBloodtype(String bloodtype) {
&space(4)this.bloodtype = bloodtype;
&space(2)}
&space(2)public String getName() {
&space(4)return name;
&space(2)}
&space(2)public void setName(String name) {
&space(4)this.name = name;
&space(2)}
&space(2)
}
}}
【Actionの作成】
WEB-INF/src/にTestAction.javaを作成。
TestAction.java
#divstyle(background-color:#FFFFCC;width:400px;height:200px;overflow:auto;){{
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 {
&space(2)
&space(2)public ActionForward execute(ActionMapping mapping,
&space(16)ActionForm form,
&space(16)HttpServletRequest req,
&space(16)HttpServletResponse resp) {
&space(4)
&space(4)HttpSession session = req.getSession();
&space(4)TestActionForm testForm = (TestActionForm)form;
&space(4)session.setAttribute("form", testForm);
&space(4)
&space(4)return(mapping.findForward("success"));
&space(4)
&space(2)}
}
}}
【struts-config.xmlの作成】
WEB-INFを右クリック→新規→ファイル→「struts-config.xml」を入力して作成。
struts-config.xml
#divstyle(background-color:#FFFFCC;width:400px;height:200px;overflow:auto;){{
<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE struts-config PUBLIC
&space(1)"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
&space(1)"&html(http://struts.apache.org/dtds/struts-config_1_1.dtd)">
<struts-config>
&space(2)<form-beans>
&space(4)<form-bean name="form" type="TestActionForm" />
&space(2)</form-beans>
&space(2)<action-mappings>
&space(4)<action path="/sendData" type="TestAction" name="form">
&space(6)<forward name="success" path="/jsp/output.jsp" />
&space(4)</action>
&space(2)</action-mappings>
&space(2)
&space(2)<message-resources parameter="ApplicationResources"/>
</struts-config>
}}
【web.xmlの作成】
struts-config.xmlと同じ場所に、同じ方法で作成。
web.xml
#divstyle(background-color:#FFFFCC;width:400px;height:200px;overflow:auto;){{
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
&space(2)PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
&space(2)"&html(http://java.sun.com/dtd/web-app_2_3.dtd)">
<web-app>
&space(2)<servlet>
&space(4)<servlet-name>action</servlet-name>
&space(4)<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
&space(4)<load-on-startup>1</load-on-startup>
&space(2)</servlet>
&space(2)<servlet-mapping>
&space(4)<servlet-name>action</servlet-name>
&space(4)<url-pattern>*.do</url-pattern>
&space(2)</servlet-mapping>
&space(2)<taglib>
&space(4)<taglib-uri>/struts-bean.tld</taglib-uri>
&space(4)<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
&space(2)</taglib>
&space(2)
&space(2)<taglib>
&space(4)<taglib-uri>/struts-html.tld</taglib-uri>
&space(4)<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
&space(2)</taglib>
</web-app>
}}
【ApplicationResources.propertiesの作成】
WEB-INF/srcで右クリック→新規→ファイル→
ApplicationResources.propertiesを入力
とりあえず空ファイルでよし!!
eclipseのフォルダ\workspace\TestStruts01\WEB-INF\classesにファイルがあるか一応確認。
【動かしてみる】
Tomcatを起動
http://localhost:8080/TestStruts01/jsp/input.jspにつないでみると・・・
&ref(input.JPG)
送信を押すと
&ref(output.JPG)
【今の問題点】
1.日本語を入力すると文字化けする
2.入力値チェックをしたい
なので次はこれ。
[[文字化け解消>/Struts/文字化け解消]]
その次にこれ。
[[入力値チェック>/Struts/入力値チェック]]
合計:&counter()