示例#1
0
  private boolean renderTemplate(
      Object target, GroovyObject controller, GrailsWebRequest webRequest, Map argMap, Writer out) {
    boolean renderView;
    String templateName = argMap.get(ARGUMENT_TEMPLATE).toString();
    String contextPath = getContextPath(webRequest, argMap);

    String var = (String) argMap.get(ARGUMENT_VAR);
    // get the template uri
    String templateUri = GroovyPageUtils.getTemplateURI(controller, templateName);

    // retrieve gsp engine
    GroovyPagesTemplateEngine engine =
        (GroovyPagesTemplateEngine)
            webRequest.getApplicationContext().getBean(GroovyPagesTemplateEngine.BEAN_ID);
    try {
      Resource r = engine.getResourceForUri(contextPath + templateUri);
      if (!r.exists()) {
        r = engine.getResourceForUri(contextPath + "/grails-app/views/" + templateUri);
      }

      Template t = engine.createTemplate(r); // templateUri);

      if (t == null) {
        throw new ControllerExecutionException(
            "Unable to load template for uri [" + templateUri + "]. Template not found.");
      }
      Map binding = new HashMap();

      if (argMap.containsKey(ARGUMENT_BEAN)) {
        Object bean = argMap.get(ARGUMENT_BEAN);
        if (argMap.containsKey(ARGUMENT_MODEL)) {
          Object modelObject = argMap.get(ARGUMENT_MODEL);
          if (modelObject instanceof Map) binding.putAll((Map) modelObject);
        }
        renderTemplateForBean(t, binding, bean, var, out);
      } else if (argMap.containsKey(ARGUMENT_COLLECTION)) {
        Object colObject = argMap.get(ARGUMENT_COLLECTION);
        if (argMap.containsKey(ARGUMENT_MODEL)) {
          Object modelObject = argMap.get(ARGUMENT_MODEL);
          if (modelObject instanceof Map) binding.putAll((Map) modelObject);
        }
        renderTemplateForCollection(t, binding, colObject, var, out);
      } else if (argMap.containsKey(ARGUMENT_MODEL)) {
        Object modelObject = argMap.get(ARGUMENT_MODEL);
        renderTemplateForModel(t, modelObject, target, out);
      } else {
        Writable w = t.make(new BeanMap(target));
        w.writeTo(out);
      }
      renderView = false;
    } catch (GroovyRuntimeException gre) {
      throw new ControllerExecutionException(
          "Error rendering template [" + templateName + "]: " + gre.getMessage(), gre);
    } catch (IOException ioex) {
      throw new ControllerExecutionException(
          "I/O error executing render method for arguments [" + argMap + "]: " + ioex.getMessage(),
          ioex);
    }
    return renderView;
  }
示例#2
0
  private void renderView(Map argMap, Object target, GroovyObject controller) {
    String viewName = argMap.get(ARGUMENT_VIEW).toString();
    Object modelObject = argMap.get(ARGUMENT_MODEL);

    String viewUri = GroovyPageUtils.getNoSuffixViewURI((GroovyObject) target, viewName);
    Map model;
    if (modelObject instanceof Map) {
      model = (Map) modelObject;
    } else if (target instanceof GroovyObject) {
      model = new BeanMap(target);
    } else {
      model = new HashMap();
    }

    controller.setProperty(
        ControllerDynamicMethods.MODEL_AND_VIEW_PROPERTY, new ModelAndView(viewUri, model));
  }