StrustのArrayList入出力(JDK1.4
数年前にやったことがあったストラッツフレームワークの仕事で、配列を一覧に表示し、さらに一覧に入力した値を取得する機能を作成することになったのだが、以前この方法はなんなく実装できたはずなのにまったくうまくいかない。あれこれと調べてようやく完成した。
まず、データビーンを作成
package oresama.framework;
public class RecordBean {
private String m_text;
private boolean m_recordCheck;
private String recordRadio;
public final String getRecordRadio() {
return recordRadio;
}
public final void setRecordRadio(String recordRadio) {
this.recordRadio = recordRadio;
}
public String getText() {
return m_text;
}
public void setText(String text) {
m_text = text;
}
public final boolean getRecordCheck() {
return m_recordCheck;
}
public final void setRecordCheck(boolean m_recordCheck) {
this.m_recordCheck = m_recordCheck;
}
}
次にアクションフォーム(ここが味噌)
package oresama.framework;
public class Page1ActionForm extends ActionForm {
private static final long serialVersionUID = 0L;
private int m_recordNo;
private ArrayList m_recordList;
public int getRecordNo() {
return m_recordNo;
}
public void setRecordNo(int recordNo) {
m_recordNo = recordNo;
}
public Collection getRecordList() {
return m_recordList;
}
public ArrayList getRecordArrayList() {
return m_recordList;
}
public void setRecordList(RecordBean rb) {
if (m_recordList == null || m_recordList.isEmpty()) {
m_recordList = new ArrayList();
}
m_recordList.add(rb);
}
public RecordBean getRecord(int index) {
if (m_recordList == null || m_recordList.isEmpty()) {
return null;
}
while (m_recordList.size() <= index) {
m_recordList.add(new RecordBean());
}
return (RecordBean)m_recordList.get(index);
}
public void addRecord(RecordBean rb) {
if (m_recordList == null || m_recordList.isEmpty()) {
m_recordList = new ArrayList();
}
m_recordList.add(rb);
}
…
次にJSP
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8"/>
<title></title>
</head>
<body>
<html:form action="/action1">
<html:submit property="add.record">add record</html:submit>
<html:submit value="OK" />
<html:cancel>cancel</html:cancel>
<html:text property="text1" readonly="true" size="40" />
<br/>
<html:text property="field1" />
<br/>
<br>
<logic:notEmpty name="page1form" property="recordList">
<logic:iterate name="page1form" property="recordList" id="record" indexId="idx">
<html:radio name="page1form" property="recordNo" value="<%= idx.toString() %>" />
<html:checkbox name="record" property="recordCheck" indexed="true"/>
<html:radio name="record" property="recordRadio" value="A" indexed="true" />
<html:radio name="record" property="recordRadio" value="B" indexed="true" />
<html:text name="record" property="text" indexed="true"/>
<br>
</logic:iterate>
</logic:notEmpty>
</html:form>
</body>
</html:html>
最後にAction(関係ない処理も入っているので、コピペでは動かないので)
/**
* @see org.apache.struts.action.Action
*/
package oresama.framework;
import java.util.ArrayList;
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;
import org.apache.struts.util.LabelValueBean;
import antlr.collections.List;
/**
* @author laccie
*
*/
public class Page1Action extends Action {
/**
* @param mapping ActionMapping
* @param form ActionForm
* @param request HttpServletRequest
* @param response HttpServletResponse
* @return mapping.getInputForward()
* @throws Exception Exceptionにスロー
*/
public final ActionForward execute(final ActionMapping mapping,
final ActionForm form,
final HttpServletRequest request,
final HttpServletResponse response)
throws Exception {
Page1ActionForm page1form = (Page1ActionForm) form;
// 値の取り出しテスト
if (page1form.getRecordList() != null) {
ArrayList list = page1form.getRecordArrayList();
for (int i = 0; i < list.size(); i++) {
RecordBean rb = (RecordBean)list.get(i);
System.out.println("RecordCheck:" + rb.getRecordCheck()
+",Text:" +rb.getText()
+",Radio:" +rb.getRecordRadio());
}
}
String outmessage = "";
String input = page1form.getField1();
//キャンセル処理
if (this.isCancelled(request)) {
outmessage = "キャンセルしました";
} else {
//オーケー処理
if (input == null || input.isEmpty()) {
outmessage = "名前を入れてね";
} else {
outmessage = "こんばんワンツー、" + input + "さん!";
}
}
page1form.setText1(outmessage);
// 配列に追加する
if (request.getParameter("add.record") != null) {
int i = 0;
RecordBean rb = new RecordBean();
if (page1form.getRecordNo() < 1) {
i = 1;
} else {
i = page1form.getRecordNo() + 1;
}
rb.setText("text" + i);
rb.setRecordCheck(false);
rb.setRecordRadio("A");
page1form.setRecordList(rb);
page1form.setRecordNo(i);
}
return mapping.findForward("success");
}
}
まず、データビーンを作成
package oresama.framework;
public class RecordBean {
private String m_text;
private boolean m_recordCheck;
private String recordRadio;
public final String getRecordRadio() {
return recordRadio;
}
public final void setRecordRadio(String recordRadio) {
this.recordRadio = recordRadio;
}
public String getText() {
return m_text;
}
public void setText(String text) {
m_text = text;
}
public final boolean getRecordCheck() {
return m_recordCheck;
}
public final void setRecordCheck(boolean m_recordCheck) {
this.m_recordCheck = m_recordCheck;
}
}
次にアクションフォーム(ここが味噌)
package oresama.framework;
public class Page1ActionForm extends ActionForm {
private static final long serialVersionUID = 0L;
private int m_recordNo;
private ArrayList m_recordList;
public int getRecordNo() {
return m_recordNo;
}
public void setRecordNo(int recordNo) {
m_recordNo = recordNo;
}
public Collection getRecordList() {
return m_recordList;
}
public ArrayList getRecordArrayList() {
return m_recordList;
}
public void setRecordList(RecordBean rb) {
if (m_recordList == null || m_recordList.isEmpty()) {
m_recordList = new ArrayList();
}
m_recordList.add(rb);
}
public RecordBean getRecord(int index) {
if (m_recordList == null || m_recordList.isEmpty()) {
return null;
}
while (m_recordList.size() <= index) {
m_recordList.add(new RecordBean());
}
return (RecordBean)m_recordList.get(index);
}
public void addRecord(RecordBean rb) {
if (m_recordList == null || m_recordList.isEmpty()) {
m_recordList = new ArrayList();
}
m_recordList.add(rb);
}
…
次にJSP
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8"/>
<title></title>
</head>
<body>
<html:form action="/action1">
<html:submit property="add.record">add record</html:submit>
<html:submit value="OK" />
<html:cancel>cancel</html:cancel>
<html:text property="text1" readonly="true" size="40" />
<br/>
<html:text property="field1" />
<br/>
<br>
<logic:notEmpty name="page1form" property="recordList">
<logic:iterate name="page1form" property="recordList" id="record" indexId="idx">
<html:radio name="page1form" property="recordNo" value="<%= idx.toString() %>" />
<html:checkbox name="record" property="recordCheck" indexed="true"/>
<html:radio name="record" property="recordRadio" value="A" indexed="true" />
<html:radio name="record" property="recordRadio" value="B" indexed="true" />
<html:text name="record" property="text" indexed="true"/>
<br>
</logic:iterate>
</logic:notEmpty>
</html:form>
</body>
</html:html>
最後にAction(関係ない処理も入っているので、コピペでは動かないので)
/**
* @see org.apache.struts.action.Action
*/
package oresama.framework;
import java.util.ArrayList;
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;
import org.apache.struts.util.LabelValueBean;
import antlr.collections.List;
/**
* @author laccie
*
*/
public class Page1Action extends Action {
/**
* @param mapping ActionMapping
* @param form ActionForm
* @param request HttpServletRequest
* @param response HttpServletResponse
* @return mapping.getInputForward()
* @throws Exception Exceptionにスロー
*/
public final ActionForward execute(final ActionMapping mapping,
final ActionForm form,
final HttpServletRequest request,
final HttpServletResponse response)
throws Exception {
Page1ActionForm page1form = (Page1ActionForm) form;
// 値の取り出しテスト
if (page1form.getRecordList() != null) {
ArrayList list = page1form.getRecordArrayList();
for (int i = 0; i < list.size(); i++) {
RecordBean rb = (RecordBean)list.get(i);
System.out.println("RecordCheck:" + rb.getRecordCheck()
+",Text:" +rb.getText()
+",Radio:" +rb.getRecordRadio());
}
}
String outmessage = "";
String input = page1form.getField1();
//キャンセル処理
if (this.isCancelled(request)) {
outmessage = "キャンセルしました";
} else {
//オーケー処理
if (input == null || input.isEmpty()) {
outmessage = "名前を入れてね";
} else {
outmessage = "こんばんワンツー、" + input + "さん!";
}
}
page1form.setText1(outmessage);
// 配列に追加する
if (request.getParameter("add.record") != null) {
int i = 0;
RecordBean rb = new RecordBean();
if (page1form.getRecordNo() < 1) {
i = 1;
} else {
i = page1form.getRecordNo() + 1;
}
rb.setText("text" + i);
rb.setRecordCheck(false);
rb.setRecordRadio("A");
page1form.setRecordList(rb);
page1form.setRecordNo(i);
}
return mapping.findForward("success");
}
}
コメント