Support > Wagby Developer Network(R7) > Customize with Java > Change contents of menu dynamically
ja | en

We will explain how to customize menus by adding parameters to pre-designed "external link menus".

External link menu

Here, "http://calendar.infocharge.net/We will prepare an external link to.

Fig. 1 Preparing external links

In the sample of this time, "month/month information (yyyy/MM /) of the following month" is added to the above URL.For example,http://calendar.infocharge.net/cal/2017/02/"Will be.The year and month parts are dynamically determined.

MyMenuController

Create a class MyMenuController to extend the menu.

If the package name is "jp.jasminesoft.wagby", prepare the following file.

customize/java/jp/jasminesoft/wagby/controller/MyMenuController.java

Override the perform_db () method of this MyMenuController and embed year/month information in menubar/menu/item/@ url.Specific code is shown.

package jp.jasminesoft.wagby.controller;

import static jp.jasminesoft.util.ExcelFunction.*;

import java.io.IOException;

import javax.servlet.ServletException;

import jp.jasminesoft.jfc.DbActionParameter;
import jp.jasminesoft.jfc.controller.BaseController;
import jp.jasminesoft.jfc.controller.MenuController;
import jp.jasminesoft.jfc.menu.IMenuChoice;
import jp.jasminesoft.jfc.menu.Item;
import jp.jasminesoft.jfc.menu.Menu;
import jp.jasminesoft.jfc.menu.Menubar;

import org.springframework.stereotype.Controller;

/**
 * MenuController の拡張クラスです。
 *
 * @author JasmineSoft
 * @version $Revision$ $Date$
 */
@Controller
public class MyMenuController extends MenuController {

    /** {@inheritDoc} **/
    @Override
    public String perform_db(DbActionParameter p)
            throws IOException, ServletException {
        String forward = super.perform_db(p);

        // メニュー情報を取得する。
        Menubar menubar = (Menubar) p.request.getAttribute(
                BaseController.MenubarRequestName);
        for (Menu menu : menubar.getMenu()) {
            // メニュー大項目毎の処理。
            for (IMenuChoice imc : menu.getContent()) {
                if (!(imc instanceof Item)) {
                    continue;
                }
                // メニュー毎の処理
                Item item = (Item) imc;
                if (!"http://calendar.infocharge.net/".equals(item.getUrl())) {
                    // 目的のメニュー以外は処理をスキップ
                    continue;
                }

                // URL に翌月の年月(yyyy/MM/)を埋め込み、
                //「http://calendar.infocharge.net/cal/2017/02/」を組み立てる。
                item.setUrl(item.getUrl()
                        + "cal/" + TEXT(MOVEMONTH(NOW(), 1), "yyyy/MM/"));
            }
        }
        return forward;
    }
}
  • This change is reflected even when submenu is displayed.
  • For the structure of menubar, please refer to the wagbyapp/webapps/wagby/WEB-INF/menu.xml file.

Build and confirm that the external menu works properly.

If it does not work, please also check that the MyMenuController class is referenced in WEB-INF/wagby-servlet.xml.When the customization file is imported, the following entry is included in wagby-servlet.xml.

<bean id="menuController"
        class="jp.jasminesoft.wagby.controller.MyMenuController">
                <property name="menuService" ref="menuService" />
</bean>