Wicket 表を作る で、呪文を唱えるたびに MP が減っていくようにしたい
package com.snail.wicket.exam;
import org.apache.wicket.Request;
import org.apache.wicket.protocol.http.WebSession;
public final class MySession extends WebSession {
private static final long serialVersionUID = -1354773246805396494L;
private int magicPoint;
public MySession(Request request) {
super(request);
magicPoint = 100;
}
public int getMagicPoint() {
return magicPoint;
}
public void setMagicPoint(int magicPoint) {
this.magicPoint = magicPoint;
}
}
package com.snail.wicket.exam;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebApplication;
public class WicketApplication extends WebApplication {
public WicketApplication() {
super();
}
@SuppressWarnings("unchecked")
@Override
public Class getHomePage() {
return MenuPage.class;
}
@Override
public Session newSession(Request req,Response res){
return new MySession(req);
}
}
package com.snail.wicket.exam.list;
import com.snail.wicket.exam.MySession;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
public class RecitePage extends WebPage {
private static final long serialVersionUID = -2267116207092439035L;
private FeedbackPanel feedback = new FeedbackPanel("msg");
private MagicVO magic;
public RecitePage(MagicVO pMagic) {
add(feedback);
magic = pMagic;
Form form = new Form("f", new CompoundPropertyModel(magic));
add(form);
form.add(new Label(MagicVO.NAME_COL));
form.add(new Label(MagicVO.EFFECT_COL));
form.add(new Label(MagicVO.MP_COL));
final MySession session = (MySession) getSession();
form.add(new Label("rem",
Integer.toString(session.getMagicPoint())));
form.add(new Button("cancel") {
@Override
public void onSubmit() {
setResponsePage(new ListPage());
}
});
form.add(new Button("sure") {
@Override
public void onSubmit() {
int useMP = Integer.parseInt(magic.getMp());
int remMP = session.getMagicPoint();
if (remMP < useMP) {
error("MP is lacking to put " + magic.getName()
+ "in motion.");
} else {
session.setMagicPoint(remMP - useMP);
setResponsePage(new ListPage());
}
}
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:wicket="http://wicket.apache.org/">
<head>
<title>Result Page</title>
</head>
<body>
Are you sure you recite this magic? <br/>
<span wicket:id="msg"> error message will be here </span>
<form wicket:id="f">
Name: <span wicket:id="name">Name will be here</span><br/>
Effect: <span wicket:id="effect">Effect will be here</span><br/>
Use MP: <span wicket:id="mp">Magic Point will be here</span><br/>
Remains MP: <span wicket:id="rem">Remains Magic Point will be here</span><br/>
<input type="submit" wicket:id="sure" value="sure"/>
<input type="submit" wicket:id="cancel" value="cancel"/>
</form>
</body>
</html>