Web Artisan Blog - ウェブ アルチザン ブログ

strutsで配列型のフォームオブジェクトを使う方法

Java (21 items)

2004年07月08日

strutsで配列型のフォームオブジェクトを使う方法

内容
1.JSP
2.ActionForm
3.Action
4.Bean
5.実行時のhtml
<スポンサードリンク>
1.【DispSetModelBean.jsp】

<%@ page contentType="text/html; charset=Windows-31J" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<html:html locale="true">
<head>
<title>DispSetModelBean.jsp</title>
</head>
<body>
<html:form action="/DispSetModelBeanAction" method="POST">
<html:hidden property="decisionMode" />
<br>
<html:submit property="submit" value="追加" onclick="form.decisionMode.value='insert'; submit();"/>
<html:submit property="submit" value="変更" onclick="form.decisionMode.value='update'; submit();"/>
<html:submit property="submit" value="削除" onclick="form.decisionMode.value='delete'; submit();"/>
<table border=1>
<logic:notEmpty name="DispSetModelBeanForm">
<logic:iterate id="data" name="DispSetModelBeanForm" property="data" indexId="idx">
<tr>
<td>
<html:multibox property="sentaku">
<%= idx%>
</html:multibox>
</td>
<td>
<html:text name="data" property="code" indexed="true" size="10"/>
</td>
<td>
<html:text name="data" property="name" indexed="true" size="10"/>
</td>
</tr>
</logic:iterate>
</logic:notEmpty>
</table>
<br>
</html:form>
</body>
</html:html>




2.【DispSetModelBeanForm.java】

package mylist;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;

public class DispSetModelBeanForm extends ActionForm {

private String decisionMode="";
private int[] sentaku;
private List data = new ArrayList();

public void reset(ActionMapping mapping, HttpServletRequest req) {
try {
req.setCharacterEncoding("Windows-31J");
}
catch (Exception e) {
e.printStackTrace();
}

}

public MyValueBean getData(int iIndex)
{
while (this.data.size() <= iIndex)
{
this.data.add(new MyValueBean());
}
return (MyValueBean)this.data.get(iIndex);
}

public void add()
{
this.data.add(new MyValueBean());
}

public int getlistSize()
{
return this.data.size();
}

public void delete(int i)
{
this.data.remove(i);
}

public Object[] getData(){
return data.toArray();
}

public int[] getSentaku() {
return sentaku;
}

public void setSentaku(int[] is) {
sentaku = is;
}

public String getDecisionMode() {
return decisionMode;
}

public void setDecisionMode(String string) {
decisionMode = string;
}

}




3.【DispSetModelBeanAction.java】

package mylist;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 DispSetModelBeanAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res )
throws Exception {

DispSetModelBeanForm myForm = (DispSetModelBeanForm)form;

if (myForm.getDecisionMode().equals("")){
MyValueListBean myListBean = new MyValueListBean();
MyValueBean myBean = new MyValueBean();

myForm.getData(0).setCode("001");
myForm.getData(0).setName("AAA");
myForm.getData(1).setCode("002");
myForm.getData(1).setName("BBB");
myForm.getData(2).setCode("003");
myForm.getData(2).setName("CCC");

}else if(myForm.getDecisionMode().equals("update")){
}else if(myForm.getDecisionMode().equals("insert")){
String str = "00" + String.valueOf(myForm.getlistSize()+1);
myForm.getData(myForm.getlistSize()).setCode(str.substring(str.length() -3 ));
}else if(myForm.getDecisionMode().equals("delete")){
int sentaku[] = myForm.getSentaku();
if (sentaku != null){
for(int i=0; i<sentaku.length; i++){
//ArrayListの要素が削除されるのでループ毎に削除対象が-1される
myForm.delete(sentaku[i]-i);
}
myForm.setSentaku(new int[0]);
}
}
return mapping.findForward( "success" );
}
}




4.【MyValueBean.java】

package mylist;

public class MyValueBean {
String code;
String name;

public void setCode(String code ) {
this.code = code;
}

public String getCode() {
return code;
}

public void setName(String name ) {
this.name = name;
}

public String getName() {
return this.name;
}

}



5.【実行時のHTML】

<html lang="ja">
<head>
<title>DispSetModelBean.jsp</title>
</head>
<body>
<form name="DispSetModelBeanForm" method="POST" action="/struts-test01/DispSetModelBeanAction.do">
<input type="hidden" name="decisionMode" value="">
<br>
<input type="submit" name="submit" value="追加" onclick="form.decisionMode.value='insert'; submit();">
<input type="submit" name="submit" value="変更" onclick="form.decisionMode.value='update'; submit();">
<input type="submit" name="submit" value="削除" onclick="form.decisionMode.value='delete'; submit();">
<table border=1>
<tr>
<td>
<input type="checkbox" name="sentaku" value="0">
</td>
<td>
<input type="text" name="data[0].code" size="10" value="001">
</td>
<td>
<input type="text" name="data[0].name" size="10" value="AAA">
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="sentaku" value="1">
</td>
<td>
<input type="text" name="data[1].code" size="10" value="002">
</td>
<td>
<input type="text" name="data[1].name" size="10" value="BBB">
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="sentaku" value="2">
</td>
<td>
<input type="text" name="data[2].code" size="10" value="003">
</td>
<td>
<input type="text" name="data[2].name" size="10" value="CCC">
</td>
</tr>
</table>
<br>
</form>
</body>
</html>



<スポンサードリンク>
前の記事 次の記事

Comments

コメントは、まだ書かれていません

Add Comments

Trackback

トラックバックはありません

Trackback URL

http://www.res-system.com/weblog/action.php?action=plugin&name=TrackBack&tb_id=166