Java源码示例:freemarker.ext.servlet.AllHttpScopesHashModel
示例1
@Override
public Template getTemplate(String name, final Locale locale) throws IOException {
if (name.equals("templateName") || name.equals("prefix_test_suffix")) {
return new Template(name, new StringReader("test"), this) {
@Override
public void process(Object model, Writer writer) throws TemplateException, IOException {
assertEquals(Locale.US, locale);
assertTrue(model instanceof AllHttpScopesHashModel);
AllHttpScopesHashModel fmModel = (AllHttpScopesHashModel) model;
assertEquals("myvalue", fmModel.get("myattr").toString());
}
};
}
else {
throw new FileNotFoundException();
}
}
示例2
@Override
public Template getTemplate(String name, final Locale locale) throws IOException {
if (name.equals("templateName") || name.equals("prefix_test_suffix")) {
return new Template(name, new StringReader("test"), this) {
@Override
public void process(Object model, Writer writer) throws TemplateException, IOException {
assertEquals(Locale.US, locale);
assertTrue(model instanceof AllHttpScopesHashModel);
AllHttpScopesHashModel fmModel = (AllHttpScopesHashModel) model;
assertEquals("myvalue", fmModel.get("myattr").toString());
}
};
}
else {
throw new FileNotFoundException();
}
}
示例3
@Override
public Template getTemplate(String name, final Locale locale) throws IOException {
if (name.equals("templateName") || name.equals("prefix_test_suffix")) {
return new Template(name, new StringReader("test"), this) {
@Override
public void process(Object model, Writer writer) throws TemplateException, IOException {
assertEquals(Locale.US, locale);
assertTrue(model instanceof AllHttpScopesHashModel);
AllHttpScopesHashModel fmModel = (AllHttpScopesHashModel) model;
assertEquals("myvalue", fmModel.get("myattr").toString());
}
};
}
else {
throw new FileNotFoundException();
}
}
示例4
protected TemplateModel createModel(ObjectWrapper wrapper) throws Throwable {
HttpServletRequest request = super.getRequestContext().getRequest();
HttpServletResponse response = super.getRequestContext().getResponse();
ServletContext servletContext = request.getSession().getServletContext();
AllHttpScopesHashModel hashModel = new AllHttpScopesHashModel(wrapper, servletContext, request); //super.createModel(wrapper, servletContext, request, response);
ControllerServlet servlet = new ControllerServlet();
MockServletConfig config = new MockServletConfig(servletContext);
servlet.init(config);
ServletContextHashModel newServletContextModel = new ServletContextHashModel(servlet, wrapper);
ServletContextHashModel servletContextModel = new ServletContextHashModel(servlet, wrapper);
servletContext.setAttribute(ATTR_APPLICATION_MODEL, servletContextModel);
TaglibFactory taglibs = new TaglibFactory(servletContext);
servletContext.setAttribute(ATTR_JSP_TAGLIBS_MODEL, taglibs);
hashModel.putUnlistedModel(FreemarkerServlet.KEY_APPLICATION, newServletContextModel);
hashModel.putUnlistedModel(FreemarkerServlet.KEY_APPLICATION_PRIVATE, newServletContextModel);
hashModel.putUnlistedModel(FreemarkerServlet.KEY_JSP_TAGLIBS, taglibs);
HttpRequestHashModel requestModel = new HttpRequestHashModel(request, response, wrapper);
request.setAttribute(ATTR_REQUEST_MODEL, requestModel);
hashModel.putUnlistedModel(FreemarkerServlet.KEY_REQUEST_PRIVATE, requestModel);
return hashModel;
}
示例5
@Override
public int doEndTag() throws JspException {
if (this.isRemoveOnEndTag()) {
ServletRequest request = this.pageContext.getRequest();
RequestContext reqCtx = (RequestContext) request.getAttribute(RequestContext.REQCTX);
try {
ExecutorBeanContainer ebc = (ExecutorBeanContainer) reqCtx.getExtraParam(SystemConstants.EXTRAPAR_EXECUTOR_BEAN_CONTAINER);
TemplateModel templateModel = ebc.getTemplateModel();
if (templateModel instanceof AllHttpScopesHashModel) {
AllHttpScopesHashModel hashModel = (AllHttpScopesHashModel) templateModel;
hashModel.remove(this.getVar());
}
} catch (Throwable t) {
_logger.error("error in doEndTag", t);
throw new JspException("Error evaluating di end tag", t);
}
}
this.release();
return EVAL_PAGE;
}
示例6
/**
* Build a FreeMarker template model for the given model Map.
* <p>The default implementation builds a {@link AllHttpScopesHashModel}.
* @param model the model to use for rendering
* @param request current HTTP request
* @param response current servlet response
* @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof
*/
protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request,
HttpServletResponse response) {
AllHttpScopesHashModel fmModel = new AllHttpScopesHashModel(getObjectWrapper(), getServletContext(), request);
fmModel.put(FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory);
fmModel.put(FreemarkerServlet.KEY_APPLICATION, this.servletContextHashModel);
fmModel.put(FreemarkerServlet.KEY_SESSION, buildSessionModel(request, response));
fmModel.put(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request, response, getObjectWrapper()));
fmModel.put(FreemarkerServlet.KEY_REQUEST_PARAMETERS, new HttpRequestParametersHashModel(request));
fmModel.putAll(model);
return fmModel;
}
示例7
/**
* Build a FreeMarker template model for the given model Map.
* <p>The default implementation builds a {@link AllHttpScopesHashModel}.
* @param model the model to use for rendering
* @param request current HTTP request
* @param response current servlet response
* @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof
*/
protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request,
HttpServletResponse response) {
AllHttpScopesHashModel fmModel = new AllHttpScopesHashModel(getObjectWrapper(), getServletContext(), request);
fmModel.put(FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory);
fmModel.put(FreemarkerServlet.KEY_APPLICATION, this.servletContextHashModel);
fmModel.put(FreemarkerServlet.KEY_SESSION, buildSessionModel(request, response));
fmModel.put(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request, response, getObjectWrapper()));
fmModel.put(FreemarkerServlet.KEY_REQUEST_PARAMETERS, new HttpRequestParametersHashModel(request));
fmModel.putAll(model);
return fmModel;
}
示例8
/**
* Build a FreeMarker template model for the given model Map.
* <p>The default implementation builds a {@link AllHttpScopesHashModel}.
* @param model the model to use for rendering
* @param request current HTTP request
* @param response current servlet response
* @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof
*/
protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
AllHttpScopesHashModel fmModel = new AllHttpScopesHashModel(getObjectWrapper(), getServletContext(), request);
fmModel.put(FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory);
fmModel.put(FreemarkerServlet.KEY_APPLICATION, this.servletContextHashModel);
fmModel.put(FreemarkerServlet.KEY_SESSION, buildSessionModel(request, response));
fmModel.put(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request, response, getObjectWrapper()));
fmModel.put(FreemarkerServlet.KEY_REQUEST_PARAMETERS, new HttpRequestParametersHashModel(request));
fmModel.putAll(model);
return fmModel;
}
示例9
/**
* Build a FreeMarker template model for the given model Map.
* <p>The default implementation builds a {@link AllHttpScopesHashModel}.
* @param model the model to use for rendering
* @param request current HTTP request
* @param response current servlet response
* @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof
*/
protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
AllHttpScopesHashModel fmModel = new AllHttpScopesHashModel(getObjectWrapper(), getServletContext(), request);
fmModel.put(FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory);
fmModel.put(FreemarkerServlet.KEY_APPLICATION, this.servletContextHashModel);
fmModel.put(FreemarkerServlet.KEY_SESSION, buildSessionModel(request, response));
fmModel.put(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request, response, getObjectWrapper()));
fmModel.put(FreemarkerServlet.KEY_REQUEST_PARAMETERS, new HttpRequestParametersHashModel(request));
fmModel.putAll(model);
return fmModel;
}
示例10
@Override
public int doStartTag() throws JspException {
ServletRequest request = this.pageContext.getRequest();
RequestContext reqCtx = (RequestContext) request.getAttribute(RequestContext.REQCTX);
try {
ExecutorBeanContainer ebc = (ExecutorBeanContainer) reqCtx.getExtraParam(SystemConstants.EXTRAPAR_EXECUTOR_BEAN_CONTAINER);
TemplateModel templateModel = ebc.getTemplateModel();
if (!(templateModel instanceof AllHttpScopesHashModel)) {
return EVAL_BODY_INCLUDE;
}
AllHttpScopesHashModel hashModel = (AllHttpScopesHashModel) templateModel;
Object object = this.pageContext.getAttribute(this.getValueName());
if (null == object) {
Environment environment = Environment.getCurrentEnvironment();
if (null != environment) {
Object wrapper = environment.getVariable(this.getValueName());
if (null != wrapper) {
if (wrapper instanceof StringModel) {
object = ((StringModel) wrapper).getWrappedObject();
} else {
object = wrapper;
}
}
}
}
if (null != object) {
hashModel.put(this.getVar(), object);
}
} catch (Throwable t) {
_logger.error("error in doStartTag", t);
throw new JspException("Error during tag initialization", t);
}
return EVAL_BODY_INCLUDE;
}