読者です 読者をやめる 読者になる 読者になる

作成したサンプルのソース

Google App Engine Slim3

IndexController.java


package slim3Sample1.controller.regist;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class IndexController extends Controller {

@Override
public Navigation run() {

requestScope("name", "");
requestScope("email", "");
requestScope("bookArray", new String { "JAVA_Press", "DosV_Magazine" });
requestScope("mailsend", "on");
requestScope("gender", "M");
requestScope("prefectural", "13");
requestScope("fruitArray", new String
{ "1", "3" });
requestScope("comment", "");

return forward("/regist/index.jsp");
}
}


ConfirmController.java

package slim3Sample1.controller.regist;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.controller.validator.Validators;

public class ConfirmController extends Controller {

@Override
public Navigation run() {

if (!validate()) {
return forward("/regist/index.jsp");
}
return forward("confirm.jsp");
}

protected boolean validate() {
Validators v = new Validators(request);
v.add("name", v.required());
v.add("email", v.required());
return v.validate();
}
}


InsertController .java

package slim3Sample1.controller.regist;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.util.RequestMap;

import slim3Sample1.service.MemberService;

public class InsertController extends Controller {

MemberService service = new MemberService();

@Override
public Navigation run() {
RequestMap requestMap = new RequestMap(request);
String bookArray = (String )request.getAttribute("bookArray");
StringBuffer sb = new StringBuffer();
if(bookArray != null && bookArray.length > 0) {
for(int i=0;i if(i != 0) {
sb.append(";");
}
sb.append(bookArray[i]);
}
}
requestMap.put("bookArray", sb.toString());

String fruitArray = (String )request.getAttribute("fruitArray");
sb = new StringBuffer();
if(fruitArray != null && fruitArray.length > 0) {
for(int i=0;i if(i != 0) {
sb.append(";");
}
sb.append(fruitArray[i]);
}
}
requestMap.put("fruitArray", sb.toString());

service.member(requestMap);
return forward("insert.jsp");
}
}


Member .java

package slim3Sample1.model;

import java.io.Serializable;

import com.google.appengine.api.datastore.Key;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.Model;

@Model
public class Member implements Serializable {

private static final long serialVersionUID = 1L;

@Attribute(primaryKey = true)
private Key key;

@Attribute(version = true)
private Long version;

private Integer schemaVersion = 1;

private String name;
private String email;
private String bookArray;
private String mailsend;
private String gender;
private String prefectural;
private String fruitArray;
private String comment;

public Key getKey() {
return key;
}
public void setKey(Key key) {
this.key = key;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public Integer getSchemaVersion() {
return schemaVersion;
}
public void setSchemaVersion(Integer schemaVersion) {
this.schemaVersion = schemaVersion;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getBookArray() {
return bookArray;
}
public void setBookArray(String bookArray) {
this.bookArray = bookArray;
}
public String getMailsend() {
return mailsend;
}
public void setMailsend(String mailsend) {
this.mailsend = mailsend;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPrefectural() {
return prefectural;
}
public void setPrefectural(String prefectural) {
this.prefectural = prefectural;
}
public String getFruitArray() {
return fruitArray;
}
public void setFruitArray(String fruitArray) {
this.fruitArray = fruitArray;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}


MemberService .java

package slim3Sample1.service;

import java.util.Map;
import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;
import com.google.appengine.api.datastore.Transaction;
import slim3Sample1.model.Member;

public class MemberService {
public Member member(Map input) {
Member member = new Member();
BeanUtil.copy(input, member);
Transaction tx = Datastore.beginTransaction();
Datastore.put(member);
Datastore.commit(tx);
return member;
}
}


index.jsp

<%@page pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@taglib prefix="f" uri="http://www.slim3.org/functions"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>input Index</title>
<link rel="stylesheet" type="text/css" href="/css/global.css" />
</head>
<body>
<form action="${f:url('confirm')}" method="post">
<table>
<tr>
<td>Name</td>
<td><input type="text" ${f:text("name")} class="${f:errorClass('name', 'error')}"/>${f:h(errors.name)}</td>
</tr>
<tr>
<td>E-Mail</td>
<td><input type="text" ${f:text("email")} class="${f:errorClass('email', 'error')}"/>${f:h(errors.email)}</td>
</tr>
<tr>
<td>購読雑誌</td>
<td>
<input type="checkbox" ${f:multibox("bookArray", "WEB-DB_Press")}/>WEB-DB Press<br />
<input type="checkbox" ${f:multibox("bookArray", "JAVA_Press")}/>JAVA Press<br />
<input type="checkbox" ${f:multibox("bookArray", "Java_World")}/>Java World<br />
<input type="checkbox" ${f:multibox("bookArray", "DosV_Magazine")}/>DosV Magazine<br />
</td>
</tr>
<tr>
<td>メルマガ送信有無</td>
<td><input type="checkbox" ${f:checkbox("mailsend")}/></td>
</tr>
<tr>
<td>性別</td>
<td>
<input type="radio" ${f:radio("gender", "M")}/>男
<input type="radio" ${f:radio("gender", "W")}/>女<br />
</td>
</tr>
<tr>
<td>都道府県</td>
<td>
<select name="prefectural">
<option ${f:select("prefectural", "1")}>北海道</option>
<option ${f:select("prefectural", "2")}>青森</option>
<option ${f:select("prefectural", "13")}>東京</option>
</select>
</td>
</tr>
<tr>
<td>好きな果物</td>
<td>
<select name="fruitArray" multiple="multiple" size="3">
  <option ${f:multiselect("fruitArray", "1")}>りんご</option>
  <option ${f:multiselect("fruitArray", "2")}>柿</option>
  <option ${f:multiselect("fruitArray", "3")}>みかん</option>
</select>
</td>
</tr>
<tr>
<td>コメント</td>
<td><textarea name="comment" class="${f:errorClass('comment', 'error')}">${f:h(comment)}</textarea>${f:h(errors.comment)}</td>
</tr>
</table>
<br />
<input type="submit" value="入力完了"/>
</form>
</body>
</html>


confirm.jsp

<%@page pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@taglib prefix="f" uri="http://www.slim3.org/functions"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>regist Confirm</title>
<link rel="stylesheet" type="text/css" href="/css/global.css" />
</head>
<body>
<form action="${f:url('insert')}" method="post">

<input type="hidden" ${f:hidden("name")}/>
<input type="hidden" ${f:hidden("email")}/>
<input type="hidden" ${f:hidden("mailsend")}/>
<input type="hidden" ${f:hidden("gender")}/>
<input type="hidden" ${f:hidden("prefectural")}/>
<input type="hidden" ${f:hidden("comment")}/>
<c:forEach items="${bookArray}" var="book">
<input type="hidden" name="bookArray" value="${book}">
</c:forEach>
<c:forEach items="${fruitArray}" var="fruit">
<input type="hidden" name="fruitArray" value="${fruit}">
</c:forEach>

<table border=1 >
<tr>
<td>Name</td>
<td><c:out value="${param.name}"/></td>
</tr>
<tr>
<td>E-Mail</td>
<td><c:out value="${param.email}"/></td>
</tr>
<tr>
<td>購読雑誌</td>
<td>
<c:forEach items="${bookArray}" var="book">
<c:out value="${book}"/><br>
</c:forEach>
</td>
</tr>
<tr>
<td>メルマガ送信有無</td>
<td>
<c:choose>
<c:when test="${param.mailsend == 'on'}">メルマガ送信有</c:when>
<c:when test="${param.mailsend != 'on'}">メルマガ送信無</c:when>
</c:choose>
</td>
</tr>
<tr>
<td>性別</td>
<td>
<c:choose>
<c:when test="${param.gender == 'M'}">男</c:when>
<c:when test="${param.gender == 'W'}">女</c:when>
</c:choose>
</td>
</tr>
<tr>
<td>都道府県</td>
<td>
<c:choose>
<c:when test="${param.prefectural == '1'}">北海道</c:when>
<c:when test="${param.prefectural == '2'}">青森</c:when>
<c:when test="${param.prefectural == '13'}">東京</c:when>
</c:choose>
</tr>
<tr>
<td>好きな果物</td>
<td>
<c:forEach items="${fruitArray}" var="fruit">
<c:choose>
<c:when test="${fruit == '1'}">りんご</c:when>
<c:when test="${fruit == '2'}">柿</c:when>
<c:when test="${fruit == '3'}">みかん</c:when>
</c:choose>
</c:forEach>
</td>
<tr>
<td>コメント</td>
<td><${f:br(param.comment)}/></td>
</tr>
</table>
<br />
<input type="submit" value="確認"/>
</form>
</body>
</html>


insert.jsp

<%@page pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@taglib prefix="f" uri="http://www.slim3.org/functions"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>regist Insert</title>
<link rel="stylesheet" type="text/css" href="/css/global.css" />
</head>
<body>
<p>登録が完了しました。</p>
</body>
</html>


Mode生成の時に自動生成されるMemberMeta.java

package slim3Sample1.meta;

//@javax.annotation.Generated(value = { "slim3-gen", "@VERSION@" }, date = "2009-12-01 01:15:26")
/** */
public final class MemberMeta extends org.slim3.datastore.ModelMeta {

/** */
public MemberMeta() {
super("Member", slim3Sample1.model.Member.class);
}

/** */
public org.slim3.datastore.StringAttributeMeta bookArray = new org.slim3.datastore.StringAttributeMeta(this, "bookArray");

/** */
public org.slim3.datastore.StringAttributeMeta comment = new org.slim3.datastore.StringAttributeMeta(this, "comment");

/** */
public org.slim3.datastore.StringAttributeMeta email = new org.slim3.datastore.StringAttributeMeta(this, "email");

/** */
public org.slim3.datastore.StringAttributeMeta fruitArray = new org.slim3.datastore.StringAttributeMeta(this, "fruitArray");

/** */
public org.slim3.datastore.StringAttributeMeta gender = new org.slim3.datastore.StringAttributeMeta(this, "gender");

/** */
public org.slim3.datastore.CoreAttributeMeta key = new org.slim3.datastore.CoreAttributeMeta(this, "__key__", com.google.appengine.api.datastore.Key.class);

/** */
public org.slim3.datastore.StringAttributeMeta mailsend = new org.slim3.datastore.StringAttributeMeta(this, "mailsend");

/** */
public org.slim3.datastore.StringAttributeMeta name = new org.slim3.datastore.StringAttributeMeta(this, "name");

/** */
public org.slim3.datastore.StringAttributeMeta prefectural = new org.slim3.datastore.StringAttributeMeta(this, "prefectural");

/** */
public org.slim3.datastore.CoreAttributeMeta schemaVersion = new org.slim3.datastore.CoreAttributeMeta(this, "schemaVersion", java.lang.Integer.class);

/** */
public org.slim3.datastore.CoreAttributeMeta version = new org.slim3.datastore.CoreAttributeMeta(this, "version", java.lang.Long.class);

@Override
protected void setKey(Object model, com.google.appengine.api.datastore.Key key) {
slim3Sample1.model.Member m = (slim3Sample1.model.Member) model;
m.setKey(key);
}

@Override
protected long getVersion(Object model) {
slim3Sample1.model.Member m = (slim3Sample1.model.Member) model;
return m.getVersion() != null ? m.getVersion().longValue() : 0L;
}

@Override
protected void incrementVersion(Object model) {
slim3Sample1.model.Member m = (slim3Sample1.model.Member) model;
long version = m.getVersion() != null ? m.getVersion().longValue() : 0L;
m.setVersion(Long.valueOf(version + 1L));
}

@Override
public slim3Sample1.model.Member entityToModel(com.google.appengine.api.datastore.Entity entity) {
slim3Sample1.model.Member model = new slim3Sample1.model.Member();
model.setBookArray*1;
model.setComment*2;
model.setEmail*3;
model.setFruitArray*4;
model.setGender*5;
model.setKey(entity.getKey());
model.setMailsend*6;
model.setName*7;
model.setPrefectural*8;
model.setSchemaVersion(longToInteger model.setVersion((java.lang.Long) entity.getProperty("version"">*9;
return model;
}

@Override
public com.google.appengine.api.datastore.Entity modelToEntity(java.lang.Object model) {
slim3Sample1.model.Member m = (slim3Sample1.model.Member) model;
com.google.appengine.api.datastore.Entity entity = null;
if (m.getKey() != null) {
entity = new com.google.appengine.api.datastore.Entity(m.getKey());
} else {
entity = new com.google.appengine.api.datastore.Entity(kind);
}
entity.setProperty("bookArray", m.getBookArray());
entity.setProperty("comment", m.getComment());
entity.setProperty("email", m.getEmail());
entity.setProperty("fruitArray", m.getFruitArray());
entity.setProperty("gender", m.getGender());
entity.setProperty("mailsend", m.getMailsend());
entity.setProperty("name", m.getName());
entity.setProperty("prefectural", m.getPrefectural());
entity.setProperty("schemaVersion", m.getSchemaVersion());
entity.setProperty("version", m.getVersion());
return entity;
}
}

*1:java.lang.String) entity.getProperty("bookArray"

*2:java.lang.String) entity.getProperty("comment"

*3:java.lang.String) entity.getProperty("email"

*4:java.lang.String) entity.getProperty("fruitArray"

*5:java.lang.String) entity.getProperty("gender"

*6:java.lang.String) entity.getProperty("mailsend"

*7:java.lang.String) entity.getProperty("name"

*8:java.lang.String) entity.getProperty("prefectural"

*9:java.lang.Long) entity.getProperty("schemaVersion"))); model.setVersion((java.lang.Long) entity.getProperty("version"