slim3の便利メソッドasXxx()
ひがさんにはControllerクラスには便利な機能(asXxx())があるよと教えてもらったので、Controllerクラスをついでに調べてみました。
slim3のControllerクラスにはrequestスコープから取得すると同時に型変換もできる便利なメソッド群asXxx()があるよというお話。
Boolean deleteFlg = asBoolean("delete_flg");
こんな感じ。
私はデフォルト値をつけて使ってます
protected Byte asByte(String name, Byte defaultValue) { Byte v = asByte(name); return v == null ? defaultValue : v; } protected Byte asByte(String name, String pattern, Byte defaultValue) { Byte v = asByte(name, pattern); return v == null ? defaultValue : v; } protected Short asShort(String name, Short defaultValue) { Short v = asShort(name); return v == null ? defaultValue : v; } protected Short asShort(String name, String pattern, Short defaultValue) { Short v = asShort(name, pattern); return v == null ? defaultValue : v; } protected Integer asInteger(String name, Integer defaultValue) { Integer v = asInteger(name); return v == null ? defaultValue : v; } protected Integer asInteger(String name, String pattern, Integer defaultValue) { Integer v = asInteger(name, pattern); return v == null ? defaultValue : v; } protected Long asLong(String name, Long defaultValue) { Long v = asLong(name); return v == null ? defaultValue : v; } protected Long asLong(String name, String pattern, Long defaultValue) { Long v = asLong(name, pattern); return v == null ? defaultValue : v; } protected Float asFloat(String name, Float defaultValue) { Float v = asFloat(name); return v == null ? defaultValue : v; } protected Float asFloat(String name, String pattern, Float defaultValue) { Float v = asFloat(name, pattern); return v == null ? defaultValue : v; } protected Double asDouble(String name, Double defaultValue) { Double v = asDouble(name); return v == null ? defaultValue : v; } protected Double asDouble(String name, String pattern, Double defaultValue) { Double v = asDouble(name, pattern); return v == null ? defaultValue : v; } protected BigDecimal asBigDecimal(String name, BigDecimal defaultValue) { BigDecimal v = asBigDecimal(name); return v == null ? defaultValue : v; } protected BigDecimal asBigDecimal(String name, String pattern, BigDecimal defaultValue) { BigDecimal v = asBigDecimal(name, pattern); return v == null ? defaultValue : v; } protected String asString(String name, String defaultValue) { String v = asString(name); return v == null ? defaultValue : v; } protected Boolean asBoolean(String name, Boolean defaultValue) { Boolean v = asBoolean(name); return v == null ? defaultValue : v; } protected Date asDate(String name, String pattern, Date defaultValue) { Date v = asDate(name, pattern); return v == null ? defaultValue : v; }
ほかにはAdminControllerクラス作ったりCronControllerクラス作ったり。DataViewerControllerも
(略)
ほかにはHelperクラス
EL式ではなく全部helperクラスでjspは処理してます。static importがすごく便利。
import java.util.Date; import java.util.List; import java.util.Locale; import java.util.TimeZone; import java.util.Map.Entry; import java.util.logging.Logger; import javax.servlet.http.HttpServletRequest; import org.slim3.controller.ControllerConstants; import org.slim3.controller.validator.Errors; import org.slim3.jsp.Functions; import org.slim3.util.BeanMap; import org.slim3.util.RequestLocator; import com.google.appengine.api.users.User; import com.google.appengine.api.users.UserService; import com.google.appengine.api.users.UserServiceFactory; public abstract class ActionHelper { @SuppressWarnings("unused") private static final Logger logger = Logger.getLogger(ActionHelper.class.getName()); public static String h(Object input) { return Functions.h(input); } public static String url(String input) { return Functions.url(input); } public static String errorClass(String name, String styleClass) { return Functions.errorClass(name, styleClass); } public static String text(String name) { return Functions.text(name); } public static String checkbox(String name) { return Functions.checkbox(name); } public static String multibox(String name, String value) { return Functions.multibox(name, value); } public static String radio(String name, String value) { return Functions.radio(name, value); } public static String select(String name, String value) { return Functions.select(name, value); } public static String multiselect(String name, String value) { return Functions.multiselect(name, value); } public static Locale locale() { return Functions.locale(); } public static TimeZone timeZone() { return Functions.timeZone(); } private static HttpServletRequest getRequest() { return RequestLocator.get(); } public static Object get(String attributeKey) { return getRequest().getAttribute(attributeKey); } public static String getAsString(String attributeKey) { return (String) get(attributeKey); } public static Integer getAsInteger(String attributeKey) { return (Integer) get(attributeKey); } @SuppressWarnings("unchecked") public static List getAsList(String attributeKey) { return (List) get(attributeKey); } @SuppressWarnings("unchecked") public static List<BeanMap> getAsBeanMapList(String attributeKey) { return (List<BeanMap>) get(attributeKey); } public static Errors getErrors() { Errors e = (Errors) get(ControllerConstants.ERRORS_KEY); if (e != null) { return e; } return new Errors(); } public static User getUser() { UserService userService = UserServiceFactory.getUserService(); return userService.getCurrentUser(); } public static boolean isLogined() { User user = getUser(); if (user == null) { return false; } if (user.getEmail().length() == 0) { return false; } return true; } public static boolean isAdmin() { if (!isLogined()) { return false; } return UserServiceFactory.getUserService().isUserAdmin(); } public static String loginUrl(String uri) { return UserServiceFactory.getUserService().createLoginURL(uri); } public static String loginUrl() { return loginUrl(getRequest().getRequestURI()); } public static String logoutUrl(String uri) { return UserServiceFactory.getUserService().createLogoutURL(uri); } public static String logoutUrl() { return logoutUrl(getRequest().getRequestURI()); } public static String userEmail() { return getUser().getEmail(); } public static String userAuthDomain() { return getUser().getAuthDomain(); } }