Java tutorial
/* * Copyright 2010- the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package qc.web.util; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.util.StringUtils; /** * WebUI * * @author dragon * @since 1.0.0 */ public class WebUtils { static Log logger = LogFactory.getLog(WebUtils.class); private WebUtils() { } /** * ?CSS? * @param cssPath ?CSS * @return CSSHTML? */ public static String printCSS(String cssPath) { return "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + cssPath + "\"/>"; } /** * ?JS? * @param jsPath ?CSS * @return JSHTML? */ public static String printJS(String jsPath) { return "<script type=\"text/javascript\" src=\"" + jsPath + "\"></script>"; } /** * ??? * * <pre> * http://www.demo.com/demo/test.htm --> /demo/test.htm</span> * </pre> * * @param request * @return ?/demo/test.htm */ public static String getResourcePath(HttpServletRequest request) { // Adapted from VelocityViewServlet.handleRequest() method: // If we get here from RequestDispatcher.include(), getServletPath() // will return the original (wrong) URI requested. The following // special attribute holds the correct path. See section 8.3 of the // Servlet 2.3 specification. String path = (String) request.getAttribute("javax.servlet.include.servlet_path"); // Also take into account the PathInfo stated on // SRV.4.4 Request Path Elements. String info = (String) request.getAttribute("javax.servlet.include.path_info"); if (path == null) { path = request.getServletPath(); info = request.getPathInfo(); } if (info != null) { path += info; } return path; } /**js????? * @param fnName ?js?? * @return */ public static String wrapJSFunction(String fnName) { return "function(){return window['" + fnName + "'].apply(this,arguments);}"; } public static String wrapJSFunctionWithVar(String fnName, String varName) { if (!StringUtils.hasLength(varName)) return "function(){return window['" + fnName + "'].apply(this,arguments);}"; else return "function(){this.pvar='" + varName + "';return window['" + fnName + "'].apply(this,arguments);}"; } /**???????? * @param request * @param srcFileName ?? * @return ???? */ public static String encodeFileName(HttpServletRequest request, String srcFileName) { String _fileName; // ??? boolean isIE = request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") != -1; try { if (isIE) {// IE? _fileName = URLEncoder.encode(srcFileName, "UTF-8"); if (_fileName.length() > 150) { // URLEncoder?17IE6 IEbug??KB816868 // ?????ie6 sp1 String guessCharset = "gb2312"; //?requestlocale???gb2312 _fileName = new String(srcFileName.getBytes(guessCharset), "ISO8859-1"); } } else {// ?IE?:?URLEncoder.encode???url? _fileName = new String(srcFileName.getBytes("UTF-8"), "ISO8859-1"); } } catch (UnsupportedEncodingException e) { logger.warn(e.getMessage()); _fileName = srcFileName; } return _fileName; } }