JavaのWebアプリケーション開発フレームワークによる、Webサイト開発の顛末記です。

EclipseのMavenを使った、Spring-MVC、Thymeleaf、MyBatis 等のプログラミングテクニックを、
備忘録的に記録しています。実際に動くソースコードを多用して説明していますので、
これからEclipseや、Spring-MVCを始めたいと思っている人にとって、少しでも参考になれば幸いです。
Spring-MVCの散歩道 > 応用の森(総合テクニック編) > ツリー階層構造表現

package jp.dip.arimodoki.blogic;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jp.dip.arimodoki.common.CConst;
import jp.dip.arimodoki.mapper.TblClassMap;
import jp.dip.arimodoki.model.FormCreatureClassIf;
import jp.dip.arimodoki.model.dto.DtoTblClass;
import jp.dip.arimodoki.model.dto.DtoTblClassIf;

/**
* 生物分類ツリーを検索するビジネスロジッククラスです。
*/
@Service
public class BlCreatureClass implements CConst, BlCreatureClassIf {

  @Autowired
  private TblClassMap   tblClassMap;    //生物分類Mapper

  //生物分類情報フォームビーン
  private FormCreatureClassIf       formCreatureClass;

  /**
   * フォームBeanの継承
* アクションコントローラで取得したFormBeanを継承する * @param form 生物分類情報フォームビーン */
public void setForm(FormCreatureClassIf form) { this.formCreatureClass = form; } /** * 生物分類ストをDBから取得し、フォームBeanにセットする * @throws エラーが発生した場合はExceptionをスローします */ @Transactional(readOnly=true) //DBトランザクション状態設定(read only) public void getClassList() throws Exception { //リクエストパラメータを取得 String parent = this.formCreatureClass.getParent(); //親code String rute = this.formCreatureClass.getRute(); //経路 logger.log_info(this, "parent["+parent+"]"); //検索条件 DtoTblClassIf cond = new DtoTblClass(); cond.setParent(parent); cond.setRute(rute); List classList = null; //生物分類分類マスタDB検索 //parentで指定されたコードを親にもつレコードを検索する if(rute.equals("root")) { //大分類検索(経路固定) classList = tblClassMap.getKingdomList(parent); } else { //クラス分類検索(経路指定)⇒ code(学名)が重複しているものがあるので、ruteでユニーク化 classList = tblClassMap.getClassList(cond); } if(classList==null) return; int lsize = classList.size(); int i = 0; for(DtoTblClassIf entity : classList) { boolean leaf = false; //+-のアイコンスタイルシートを設定 if(entity.getLeaf().equals("0") ) { //子ノードあり leaf = true; entity.setPointer("iconpointer"); } else { //子ノードなし entity.setPointer("nopointer"); } i++; if(i==lsize) { //ノード末尾 //stylesheet 書き換え entity.setDivclass("treelistn"); //アイコン判定 if(leaf) { //子ノードあり entity.setIcon("expand_l_b.gif"); entity.setTooltip("Expand"); } else { //子ノードなし entity.setIcon("l_dot_b.gif"); entity.setTooltip(""); } } else { //末尾以外 //アイコン判定 if(leaf) { //子ノードあり entity.setIcon("expand.gif"); entity.setTooltip("Expand"); } else { //子ノードなし entity.setIcon("horizont.gif"); entity.setTooltip(""); } } } //検索結果をフォームBeanに保持 this.formCreatureClass.setResult(classList); } }