Wicket 表を作る で、呪文を唱えるたびに MP が減っていくようにしたい

Session Object

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;
  }

}

ユーザへの Session Object の割り付け

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);
  }
}

WebPage?からの使い方

RecitePage?.java

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());
          }
        }
      });
  }
}

RecitePage?.html

<?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>

スクリーンショット

  1. 一覧画面
     
    WicketSession1.png
     
  2. とりあえず、ベギラマでも唱えてみますか
     
    WicketSession2.png
     
  3. [Sure] を押すと、一覧画面に戻る。ここで [ベホマズン] の明細画面にはいると、MPが6減っている事が分かる
     
    WicketSession1.png
     
    WicketSession3.png
     
  4. どんどん呪文を唱えていき、MP が足りなくなると、[Sure] を押しても警告が表示されて一覧表に戻らない
     
    WicketSession4.png
     
  5. このとき、同じ画面を別ブラウザで見ても、Session( cookie )が違うので、別クライアントと見なされ、MPは初期値の100
     
    WicketSession5.png
     

Java#Wicket


添付ファイル: fileWicketSession2.png 2478件 [詳細] fileWicketSession3.png 2488件 [詳細] fileWicketSession5.png 2525件 [詳細] fileWicketSession1.png 2528件 [詳細] fileWicketSession4.png 2454件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2008-03-05 (水) 02:23:47 (6122d)
Short-URL: http://at-sushi.com/pukiwiki/index.php?cmd=s&k=f5516dff36
ISBN10
ISBN13
9784061426061