다음 예제는 제품에 대한 정보를 리스트로 보여주고, 리스트 화면상에서 수정 작업을 한 후 [save] 버튼을 클릭하면 체크박스를 클릭한 항목만 수정하여 다시 리스트로 출력하는 예입니다.
이 예제를 위해서 2개의 액션을 만들것입니다.
listProduct.action : 3개의 임의의 제품 정보를 담은 리스트를 화면에 출력하는 액션 editProduct.action : 수정된 제품 정보만을 다시 리스트로 출력하는 액션
다음은 이 예제의 화면을 캡쳐한 것입니다.
[listProduct.action을 호출했을 때 화면]

[값을 변경한 후 체크박스를 클릭한고, save 버튼을 클릭했을 때 화면]

다음은 샘플 소스의 내용입니다. 소스 프로젝트는 struts2editlist.zip 파일로 첨부되어있습니다. 참고하세요.
[struts.xml]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts> <package name="default" extends="struts-default" namespace=""> <action name="listProduct" class="example.product.action.ProductAction" method="list"> <result>/product/list.jsp</result> </action>
<action name="editProduct" class="example.product.action.ProductAction" method="edit"> <result>/product/list.jsp</result> </action> </package> </struts>
[Model Class: src/example/product/model/Product.java]
package example.product.model;
public class Product { private String modelNo; private String name; private String remark; public Product() {} public Product(String modelNo, String name, String remark) { this.modelNo = modelNo; this.name = name; this.remark = remark; } public String getModelNo() { return modelNo; } public void setModelNo(String modelNo) { this.modelNo = modelNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } }
[Action Class: src/example/product/action/ProductAction.java]
package example.product.action;
import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; import example.product.model.Product;
public class ProductAction extends ActionSupport { private List<Product> listProduct; private String[] modelNo; private String[] name; private String[] remark; private boolean[] checked; public String list() { listProduct = new ArrayList<Product>(); listProduct.add(new Product("MS001", "Laser Mouse", "1000DPI")); listProduct.add(new Product("MN001", "LCD Monitor 24Inch", "1680 x 1050")); listProduct.add(new Product("HD001", "HDD 500GB", "7200RPM")); return SUCCESS; } public String edit() { listProduct = new ArrayList<Product>(); for (int i=0; i<checked.length; i++) { if (checked[i]) { listProduct.add(new Product(modelNo[i], name[i], remark[i])); } } return SUCCESS; }
public boolean[] getChecked() { return checked; } public void setChecked(boolean[] checked) { this.checked = checked; } public List<Product> getListProduct() { return listProduct; } public void setListProduct(List<Product> listProduct) { this.listProduct = listProduct; } public String[] getModelNo() { return modelNo; } public void setModelNo(String[] modelNo) { this.modelNo = modelNo; } public String[] getName() { return name; } public void setName(String[] name) { this.name = name; } public String[] getRemark() { return remark; } public void setRemark(String[] remark) { this.remark = remark; } }
[/product/list.jsp]
<%@ page contentType="text/html; charset=utf-8" %> <%@ taglib prefix="s" uri="/struts-tags" %>
<html> <head> <title>Struts2</title> </head>
<body> <s:form action="editProduct" theme="simple">
List<br/><br/>
<table border="1"> <tr><td></td><td>model no</td><td>name</td><td>remark</td></tr> <s:iterator value="listProduct"> <tr> <td> <s:checkbox name="checked" /> </td> <td> <s:textfield name="modelNo"/> </td> <td> <s:textfield name="name"/> </td> <td> <s:textfield name="remark"/> </td> </tr> </s:iterator> </table> <br/>
<s:submit value="save"/>
</s:form> </body> </html>
|