出力されるチェックボックスのchecked属性とvalue属性、あとAction側での受け取り方について。
コード
Todo.java
@Entity
public class Todo {
@Id
private long id;
private boolean checked;
private String content;
private Date created;
// getter/setter略
}
TodoAction.java
public class TodoAction extends ActionSupport {
private List<Todo> todoList;
private Collection<String> checkList;
public List<Todo> getTodoList() { return todoList; }
public void setTodoList(List<Todo> todoList) { this.todoList = todoList; }
public Collection<String> getCheckList() { return checkList; }
public void setCheckList(Collection<String> checkList) { this.checkList = checkList; }
@Action("display")
public String display() {
User user = (User) session.get("user");
userName = user.getName();
TodoEjb ejb = new TodoEjb();
todoList = ejb.findByUser(user);
return SUCCESS;
}
@Action("update")
public String update() {
for (String check : checkList) {
long id = Long.parseLong(check);
for (Todo todo : todoList) {
if (todo.getId() == id) {
todo.setChecked(true);
break;
}
}
}
TodoEjb ejb = new TodoEjb();
ejb.update(todoList);
return "update";
}
}
todo.jsp
<s:iterator value="todoList">
<tr class="row">
<td class="col-sm-1 text-center">
<s:checkbox name="checkList" value="checked" fieldValue="%{id}" />
</td>
<td class="col-sm-9">
<s:property value="content" />
</td>
<td class="col-sm-2 text-center">
<s:date name="created" format="yyyy/MM/dd HH:mm" />
</td>
</tr>
</s:iterator>
実行結果
DBの値はこんな感じだとする。
id | user_id | checked | content | created |
---|---|---|---|---|
0 | testuser1 | 1 | あれをやる | 2015-04-23 15:58:20 |
1 | testuser1 | 0 | あれとあれをやる | 2015-04-23 13:49:14 |
result.html
<tr class="row">
<td class="col-sm-1 text-center">
<input type="checkbox" name="checkList" value="0" checked="checked" id="update_checkList"/>
<input type="hidden" id="__checkbox_update_checkList" name="__checkbox_checkList" value="0" />
</td>
<td class="col-sm-9">あれをやる</td>
<td class="col-sm-2 text-center">2015/04/23 15:58</td>
</tr>
<tr class="row">
<td class="col-sm-1 text-center">
<input type="checkbox" name="checkList" value="1" id="update_checkList"/>
<input type="hidden" id="__checkbox_update_checkList" name="__checkbox_checkList" value="1" />
</td>
<td class="col-sm-9">あれとあれをやる</td>
<td class="col-sm-2 text-center">2015/04/23 13:49</td>
</tr>
<s:checkbox>タグの属性
value
出力されるチェックボックスのchecked属性に反映される。
booleanで自動型変換された結果true
となるときに、checked="checked"
となる。
fieldValue
出力されるチェックボックスのvalue属性に反映される。
name
選択されたチェックボックスのvalue属性(<s:checkbox>タグの属性で言うとfieldValue属性)を入れるBeanの指定。
複数選択される可能性があるのでAction側はCollection<String>
で受け取る。
Stringでも受け取れるが、その場合カンマ+半角スペース区切りで入ってくる。
ただし、何もチェックしていない場合はnull
。
まとめ
だいたいこんな感じ
<s:checkbox value="true" fieldValue="1" name="checkList" />
<s:checkbox value="false" fieldValue="2" name="checkList" />
<s:checkbox value="true" fieldValue="3" name="checkList" />
↓ (Response)
<input type="checkbox" name="checkList" value="1" checked="checked" />
<input type="checkbox" name="checkList" value="2" />
<input type="checkbox" name="checkList" value="3" checked="checked" />
↓ (Submit)
checkList = ["1", "3"]