Java tutorial
/******************************************************************************* * Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) * Copyright (c) 2011- kotemaru@kotemaru.org ******************************************************************************/ package org.kotemaru.browser; import org.kotemaru.util.IOUtil; import java.net.URI; import java.net.URLDecoder; import org.apache.commons.beanutils.BeanUtils; /** BrowserFrame???StrutsAction ???????? <li>http://action/ ??URL???? @author kotemaru@kotemaru.org */ public class ActionBrowserFrame extends BrowserFrame { static final String ACTION_PREFIX = "http://action/"; /** Action???*/ private String basePkg; /** * * <li>Velocity???Action???? * @param title Window? * @param pkg Action??? */ public ActionBrowserFrame(String title, String pkg) { super(title); basePkg = pkg; setHome(ACTION_PREFIX + "Main"); } /** * ??URL? * <li>about: ??? * @param url ?URL * @return true=??fale=?? */ public boolean isAllowUrl(String url) { return url.startsWith("about:"); } /** * Action?URL? * <li>http://action/ ??URL?Action * @param url ?URL * @return true=Action */ public boolean isApplicationUrl(String url) { return url.startsWith(ACTION_PREFIX); } /** * Action?URL? * <li>.?????????? * <li>???Action? * @param urlStr URL */ public void doApplicationUrl(String urlStr) throws Exception { URI uri = new URI(urlStr); String name = uri.getPath(); if (name.indexOf('.') == -1) { doAction(uri); } else { String html = IOUtil.getResource(this.getClass(), name); browser.setText(html); } } /** * Action? * <li>URL?? Action ?????? * <li>Action??? * <li>Action?setter?? * <li>Action ? execute() ?? * <li>? RedirectResult ????URL?? * <li>? RedirectResult ?HTML???? * @param uri Action URI */ protected void doAction(URI uri) throws Exception { String name = uri.getPath(); String cname = basePkg + name.replace('/', '.') + "Action"; Class cls = Class.forName(cname); Action action = (Action) cls.newInstance(); action.postBrowserFrame(this); copyQuery(uri, action); Result navi = action.execute(); if (navi == null) return; if (navi instanceof RedirectResult) { browser.setUrl(navi.getResource()); } else { browser.setText(navi.getHtml()); } } /** * ? * <li>BeanUtils ?? * <li>????? * <li>? UTF8 ? * @param uri Action URI * @param action Action */ protected void copyQuery(URI uri, Action action) throws Exception { String query = uri.getQuery(); if (query == null) return; String[] params = query.split("&"); for (int i = 0; i < params.length; i++) { String[] kv = params[i].split("="); String key = kv[0]; if (kv.length == 2) { String val = URLDecoder.decode(kv[1], "UTF-8"); BeanUtils.setProperty(action, key, val); } else if (params[i].indexOf('=') >= 0) { BeanUtils.setProperty(action, key, ""); } } } }