private static TemplateModel createModel(PageContext pageContext, Map<String, Object> map) { ObjectWrapper wrapper = ObjectWrapper.DEFAULT_WRAPPER; HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); HttpServletResponse response = (HttpServletResponse) pageContext.getResponse(); HttpServlet servlete = new ProxyServlet(pageContext.getServletConfig(), pageContext.getServletContext()); ServletContextHashModel servletContextModel = new ServletContextHashModel(servlete, wrapper); ServletActionContext.getServletContext() .setAttribute(ATTR_APPLICATION_MODEL, servletContextModel); TaglibFactory taglibs = new TaglibFactory(ServletActionContext.getServletContext()); ServletActionContext.getServletContext().setAttribute(ATTR_JSP_TAGLIBS_MODEL, taglibs); AllHttpScopesHashModel params = new AllHttpScopesHashModel(wrapper, pageContext.getServletContext(), request); params.putUnlistedModel(KEY_APPLICATION, servletContextModel); params.putUnlistedModel(KEY_APPLICATION_PRIVATE, servletContextModel); params.putUnlistedModel( KEY_JSP_TAGLIBS, (TemplateModel) ServletActionContext.getServletContext().getAttribute(ATTR_JSP_TAGLIBS_MODEL)); // Create hash model wrapper for session HttpSessionHashModel sessionModel; HttpSession session = request.getSession(false); sessionModel = (HttpSessionHashModel) session.getAttribute(ATTR_SESSION_MODEL); if (sessionModel == null) { sessionModel = new HttpSessionHashModel(session, wrapper); session.setAttribute(ATTR_SESSION_MODEL, sessionModel); } params.putUnlistedModel(KEY_SESSION, sessionModel); // Create hash model wrapper for request HttpRequestHashModel requestModel = (HttpRequestHashModel) request.getAttribute(ATTR_REQUEST_MODEL); if (requestModel == null || requestModel.getRequest() != request) { requestModel = new HttpRequestHashModel(request, response, wrapper); request.setAttribute(ATTR_REQUEST_MODEL, requestModel); request.setAttribute( ATTR_REQUEST_PARAMETERS_MODEL, new HttpRequestParametersHashModel(request)); } params.putUnlistedModel(KEY_REQUEST, requestModel); params.putUnlistedModel(KEY_INCLUDE, new IncludePage(request, response)); params.putUnlistedModel(KEY_REQUEST_PRIVATE, requestModel); // Create hash model wrapper for request parameters HttpRequestParametersHashModel requestParametersModel = (HttpRequestParametersHashModel) request.getAttribute(ATTR_REQUEST_PARAMETERS_MODEL); params.putUnlistedModel(KEY_REQUEST_PARAMETERS, requestParametersModel); // 将用户数据放入model中 params.putAll(map); return params; }
/** * Replace, add or remove query parameters in the current request and return the new query part of * the request. You can remove a query parameter by providing an empty string or null as value. * * @param hr * @param params array of name/value like params[0]="name" and params[1] = "values". Length of the * array must be even. * @return query part only e.g. view=table&filter=auto or null if the params array is odd. */ public static String replaceCurrentQueryParams(HttpRequestHashModel hr, String... params) { UriComponentsBuilder bldr = ServletUriComponentsBuilder.fromRequest(hr.getRequest()); if (params.length % 2 != 0) { return null; } for (int i = 0; i < params.length; i += 2) { // if the value is blank, remove the parameter if (StringUtils.isBlank(params[i + 1])) { bldr.replaceQueryParam(params[i]); } else { bldr.replaceQueryParam(params[i], params[i + 1]); } } return bldr.build().getQuery(); }
/** * Replace or add a query parameter in the current request and return the new query part of the * request. * * @param hr HttpRequestHashModel from Freemarker template (variable Request) * @param name query string parameter to add or change * @param value value of the query string parameter * @return query part only e.g. view=table&filter=auto */ public static String replaceCurrentQueryParam( HttpRequestHashModel hr, String name, String value) { UriComponentsBuilder bldr = ServletUriComponentsBuilder.fromRequest(hr.getRequest()); bldr.replaceQueryParam(name, value); return bldr.build().getQuery(); }
/** * Get absolute URL from HttpRequestHashModel and remove a query string parameter. * * @param hr from Freemarker template (variable Request) * @param name query string parameter to remove * @return absolute URL as String */ public static String getURLRemoveQueryParam(HttpRequestHashModel hr, String name) { UriComponentsBuilder bldr = ServletUriComponentsBuilder.fromRequest(hr.getRequest()); bldr.replaceQueryParam(name); return bldr.build().toUriString(); }