Java tutorial
/** * Project: easyframework-webapp * * File Created at 2013-12-23 * $Id$ * * Copyright 2013 leixl.com Croporation Limited. * All rights reserved. * * This software is the confidential and proprietary information of * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into */ package com.leixl.easyframework.web; import static com.leixl.easyframework.common.Constants.UTF8; import static com.leixl.easyframework.web.Constants.RES_PATH; import static com.leixl.easyframework.web.Constants.TPLDIR_STYLE_LIST; import static com.leixl.easyframework.web.Constants.TPL_STYLE_PAGE_CHANNEL; import static com.leixl.easyframework.web.Constants.TPL_SUFFIX; import static com.leixl.easyframework.web.filter.ProcessTimeFilter.START_TIME; import java.io.IOException; import java.util.Locale; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; import org.springframework.beans.propertyeditors.LocaleEditor; import org.springframework.context.MessageSource; import com.leixl.easyframework.web.URLHelper.PageInfo; import com.leixl.easyframework.web.freemarker.DirectiveUtils; import com.leixl.easyframework.web.springmvc.MessageResolver; import freemarker.core.Environment; import freemarker.template.TemplateException; import freemarker.template.TemplateModel; import freemarker.template.TemplateModelException; import freemarker.template.TemplateNumberModel; /** * * @author leixl * @date 2013-12-23 ?3:59:04 * @version v1.0 */ public class TplUtils { /** * ? */ public static final String RES_SYS = "resSys"; /** * ?? */ public static final String RES_TPL = "res"; /** * ??? */ public static final String RES_EXP = "${res}"; /** * */ public static final String BASE = "base"; /** * ?? */ public static final String LOCATION = "location"; /** * */ public static final String APP_SOLUTION = "/t/cms/www/chang"; public static final String MODULE_NAME_MOVIE = "movie"; public static final String MODULE_NAME_MEMBER = "member"; /** * ? */ public static final String PAGE_NO = "pageNo"; /** * ? */ public static final String COUNT = "count"; /** * ? */ public static final String FIRST = "first"; /** * ?? */ public static final String PARAM_STYLE_LIST = "styleList"; /** * ? */ public static final String PARAM_SYS_PAGE = "sysPage"; /** * ? */ public static final String PARAM_USER_PAGE = "userPage"; /** * ? */ public static final String RETURN_URL = "returnUrl"; /** * ???? * * @param solution * * @param dir * ??? * @param name * ????? * @return */ public static String getTplPath(String dir, String name) { return APP_SOLUTION + "/" + dir + "/" + name + TPL_SUFFIX; } /** * ????? * * @param request * @param solution * * @param dir * ??? * @param name * ???? * @return */ public static String getTplPath(HttpServletRequest request, String dir, String name) { if (StringUtils.isBlank(dir)) { return APP_SOLUTION + "/" + MessageResolver.getMessage(request, name) + TPL_SUFFIX; } else { return APP_SOLUTION + "/" + dir + "/" + MessageResolver.getMessage(request, name) + TPL_SUFFIX; } } /** * ????? * * @param messageSource * @param lang * * @param solution * * @param dir * ??? * @param name * ???? * @return */ public static String getTplPath(MessageSource messageSource, String lang, String solution, String dir, String name) { LocaleEditor localeEditor = new LocaleEditor(); localeEditor.setAsText(lang); Locale locale = (Locale) localeEditor.getValue(); if (StringUtils.isBlank(dir)) { return solution + "/" + messageSource.getMessage(name, null, locale) + TPL_SUFFIX; } else { return solution + "/" + dir + "/" + messageSource.getMessage(name, null, locale) + TPL_SUFFIX; } } /** * ? * * @param env * @return * @throws TemplateException */ public static int getPageNo(Environment env) throws TemplateException { TemplateModel pageNo = env.getGlobalVariable(PAGE_NO); if (pageNo instanceof TemplateNumberModel) { return ((TemplateNumberModel) pageNo).getAsNumber().intValue(); } else { throw new TemplateModelException("'" + PAGE_NO + "' not found in DataModel."); } } public static int getFirst(Map<String, TemplateModel> params) throws TemplateException { Integer first = DirectiveUtils.getInt(FIRST, params); if (first == null || first <= 0) { return 0; } else { return first - 1; } } /** * ?? * * @param params * @return ??050005000?? * @throws TemplateException */ public static int getCount(Map<String, TemplateModel> params) throws TemplateException { Integer count = DirectiveUtils.getInt(COUNT, params); if (count == null || count <= 0 || count >= 5000) { return 5000; } else { return count; } } public static void includePagination(Map<String, TemplateModel> params, Environment env) throws TemplateException, IOException { String sysPage = DirectiveUtils.getString(PARAM_SYS_PAGE, params); String userPage = DirectiveUtils.getString(PARAM_USER_PAGE, params); if (!StringUtils.isBlank(sysPage)) { String tpl = TPL_STYLE_PAGE_CHANNEL + sysPage + TPL_SUFFIX; env.include(tpl, UTF8, true); } else if (!StringUtils.isBlank(userPage)) { String tpl = getTplPath(TPLDIR_STYLE_LIST, userPage); env.include(tpl, UTF8, true); } else { // ? } } /** * ??? * * @param listStyle * @param site * @param env * @throws IOException * @throws TemplateException */ public static void includeTpl(String listStyle, Environment env) throws IOException, TemplateException { String tpl = getTplPath(TPLDIR_STYLE_LIST, listStyle); env.include(tpl, UTF8, true); } /** * ???? * * @param request * @param model */ public static void frontData(HttpServletRequest request, Map<String, Object> map) { String location = RequestUtils.getLocation(request); Long startTime = (Long) request.getAttribute(START_TIME); frontData(map, location, startTime); } public static void frontData(Map<String, Object> map, String location, Long startTime) { if (startTime != null) { map.put(START_TIME, startTime); } String ctx = ""; map.put(BASE, ctx); map.put(RES_SYS, ctx + RES_PATH); String res = ctx + RES_PATH + "/" + "www" + "/" + "leixl"; // res?'/' map.put(RES_TPL, res.substring(1)); map.put(LOCATION, location); } /** * ???? * * @param request * @param map */ public static void frontPageData(HttpServletRequest request, Map<String, Object> map) { int pageNo = URLHelper.getPageNo(request); PageInfo info = URLHelper.getPageInfo(request); String href = info.getHref(); String hrefFormer = info.getHrefFormer(); String hrefLatter = info.getHrefLatter(); frontPageData(pageNo, map); } /** * ???? * * @param pageNo * @param href * @param urlFormer * @param urlLatter * @param map */ public static void frontPageData(int pageNo, Map<String, Object> map) { map.put(PAGE_NO, pageNo); } }