6月12日

リストで複数項目を選択する

テキストp.226からのサンプルを実装する。
従来の単独で選択できる select1 は残して、複数選択のための select2 を SelectFormModel に追加する。

SelectFormModel.java

package jp.abc;

public class SelectFormModel {
	private String select1;
	private String[] select2; // 複数選択用

	public String getSelect1() {
		return select1;
	}

	public void setSelect1(String select1) {
		this.select1 = select1;
	}
}

spring4

selectsample.jsp

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<html>
	<head>
		<meta charset="utf-8">
		<title>Welcome</title>
	</head>

<body>
	<h1>${title}</h1>
	<p>${message}</p>
	<table>
	<form:form modelAttribute="formModel">
		<tr><td>
		<tr><td><form:select path="select1" name="select1"
			items="${selectList}" multiple="false" /></td></tr>
		<tr><td>
			<form:select path="select2" multiple="true">
				<form:options items="${optionList}" />
				<form:option value="HAL9000" />
			</form:select>
		</td></tr>
		<tr><td><input type="submit" /></td></tr>
	</form:form>
	</table>
</body>

</html>

MySelectController.java

package jp.abc;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MySelectController {

	private List<String> getList() {
		List<String> list = new ArrayList<String>();
		list.add("Mac OS X");
		list.add("Windows");
		list.add("Linux");
		return list;
	}

	@RequestMapping(value = "/select", method = RequestMethod.GET)
	public String select(Model model) {
		model.addAttribute("title", "Sample");
		model.addAttribute("message", "selectのサンプルです。");
		SelectFormModel fm = new SelectFormModel();
		fm.setSelect1("Linux");
		fm.setSelect2(new String[]{"Linux"});
		model.addAttribute("formModel", fm);
		model.addAttribute("selectList", getList());
		model.addAttribute("optionList", getList());
		return "selectsample";
	}

	@RequestMapping(value = "/select", method = RequestMethod.POST)
	public String form(@ModelAttribute SelectFormModel fm, Model model) {
		model.addAttribute("title", "Sample");
		String res = "selected: " + fm.getSelect1() + "<br/>"
				   + "options<br/><ol>";
		String[] sels = fm.getSelect2();
		for (String item : sels) {
			res += "<li>" + item + "</li>";
		}
		res += "</ol>";
		model.addAttribute("message", res);
		model.addAttribute("formModel", fm);
		model.addAttribute("selectList", getList());
		model.addAttribute("optionList", getList());
		return "selectsample";
	}

}

バリデーションの利用:買い物メモを作る

Bean: MemoFormModel.java
view: memo.jsp
Controller: MyMemoController.java
url : /memo

MemoFormModel.java

package jp.abc;

import java.util.Date;

public class MemoFormModel {
	private String item;
	private Integer price;
	private Date buydate;
	private String memo;
	public String getItem() {
		return item;
	}
	public void setItem(String item) {
		this.item = item;
	}
	public Integer getPrice() {
		return price;
	}
	public void setPrice(Integer price) {
		this.price = price;
	}
	public Date getBuydate() {
		return buydate;
	}
	public void setBuydate(Date buydate) {
		this.buydate = buydate;
	}
	public String getMemo() {
		return memo;
	}
	public void setMemo(String memo) {
		this.memo = memo;
	}
}

memo.jsp

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html>
	<head>
		<meta charset="utf-8">
		<title>${title}</title>
	</head>

<body>
	<h1>${title}</h1>
	<p>${message}</p>
	<table>
	<form:form modelAttribute="formModel">
		<tr>
			<td><form:label path="item">商品名</form:label></td>
			<td><form:input path="item" size="20"/></td>
		</tr>
		<tr>
			<td><form:label path="price">金額</form:label></td>
			<td><form:input path="price" size="20" /></td>
		</tr>
		<tr>
			<td><form:label path="memo">メモ</form:label></td>
			<td><form:textarea path="memo" cols="20" rows="5" /></td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit" /></td>
		</tr>
	</form:form>
	</table>

	<hr/>

	<c:if test="${datalist != null}">
	<table border="1">
	<tr><th>商品名</th><th>価格</th></tr>
	<c:forEach var="obj" items="${datalist}" varStatus="status">
		<tr>
		<td><c:out value="${obj.item}" /></td>
		<td><c:out value="${obj.price}" /></td>
		</tr>
	</c:forEach>
	</table>
	</c:if>
</body>
</html>

MyMemoController.java

package jp.abc;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MyMemoController {
	private List<MemoFormModel> buylist = new ArrayList<MemoFormModel>();

	@RequestMapping(value = "/memo", method = RequestMethod.GET)
	public String memo(Model model) {
		model.addAttribute("title", "Sample");
		model.addAttribute("message", "買い物メモです");
		MemoFormModel fm = new MemoFormModel();
		model.addAttribute("formModel", fm);
		model.addAttribute("detalist", buylist);
		return "memo";
	}

	@RequestMapping(value = "/memo", method = RequestMethod.POST)
	public String form(@ModelAttribute MemoFormModel fm,
					   BindingResult result, Model model) {
		buylist.add(fm);
		model.addAttribute("title", "Sample");
		String res = "<ol>";
		res += "<li>" + fm.getItem() + "</li>";
		res += "<li>" + fm.getPrice() + "</li>";
		res += "<li>" + fm.getMemo() + "</li>";
		res += "<li>" + Calendar.getInstance().getTime() + "</li>";
		res += "</ol>";
		res += "<p>" + buylist.size() + "</p>";
		model.addAttribute("message", res);
		model.addAttribute("datalist", buylist);
		model.addAttribute("formModel", new MemoFormModel());
		return "memo";
	}
}

spring5

福岡Java8勉強会

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です