spark.ModelAndView类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(127)

本文整理了Java中spark.ModelAndView类的一些代码示例,展示了ModelAndView类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ModelAndView类的具体详情如下:
包路径:spark.ModelAndView
类名称:ModelAndView

ModelAndView介绍

[英]Model And View class is used to set the name of the view and the model object to be rendered.
[中]Model And View类用于设置要渲染的视图和模型对象的名称。

代码示例

代码示例来源:origin: gocd/gocd

  1. @Override
  2. public String render(ModelAndView modelAndView) {
  3. Template template = velocityEngine.getTemplate(provider.layout(), "utf-8");
  4. Object model = modelAndView.getModel();
  5. if (model == null) {
  6. model = Collections.emptyMap();
  7. }
  8. if (model instanceof Map) {
  9. VelocityContext context = initialContextProvider.getVelocityContext((Map) model, controller, modelAndView.getViewName());
  10. StringWriter writer = new StringWriter();
  11. template.merge(context, writer);
  12. Object meta = context.get("meta");
  13. context.put("meta", GSON.toJson(meta));
  14. return writer.toString();
  15. } else {
  16. throw new IllegalArgumentException("modelAndView must be of type java.util.Map");
  17. }
  18. }
  19. }

代码示例来源:origin: perwendel/spark

  1. /**
  2. * Creates a new ModelAndView object with given arguments.
  3. *
  4. * @param model object.
  5. * @param viewName t be rendered.
  6. * @return object with model and view set.
  7. */
  8. public ModelAndView modelAndView(Object model, String viewName) {
  9. return new ModelAndView(model, viewName);
  10. }

代码示例来源:origin: aalmiray/javatrove

  1. @Override
  2. public final Object handle(Request request, Response response) throws Exception {
  3. ModelAndView modelAndView = doHandle(request, response);
  4. response.status(200);
  5. if (shouldReturnHtml(request)) {
  6. response.type("text/html");
  7. return mustacheTemplateEngine.render(modelAndView);
  8. } else {
  9. response.type("application/json");
  10. return objectMapper.writeValueAsString(modelAndView.getModel());
  11. }
  12. }

代码示例来源:origin: com.sparkjava/spark-template-freemarker

  1. /**
  2. * {@inheritDoc}
  3. */
  4. @Override
  5. public String render(ModelAndView modelAndView) {
  6. try {
  7. StringWriter stringWriter = new StringWriter();
  8. Template template = configuration.getTemplate(modelAndView.getViewName());
  9. template.process(modelAndView.getModel(), stringWriter);
  10. return stringWriter.toString();
  11. } catch (IOException | TemplateException e) {
  12. throw new IllegalArgumentException(e);
  13. }
  14. }

代码示例来源:origin: perwendel/spark

  1. /**
  2. * Constructs a ModelAndView with the provided model and view name
  3. *
  4. * @param model the model
  5. * @param viewName the view name
  6. * @return the model and view
  7. */
  8. public static ModelAndView modelAndView(Object model, String viewName) {
  9. return new ModelAndView(model, viewName);
  10. }

代码示例来源:origin: pyloque/captain

  1. @Override
  2. public String render(ModelAndView modelAndView) {
  3. try {
  4. StringWriter stringWriter = new StringWriter();
  5. Template template = configuration.getTemplate(modelAndView.getViewName());
  6. template.process(modelAndView.getModel(), stringWriter);
  7. return stringWriter.toString();
  8. } catch (IOException e) {
  9. throw new IllegalArgumentException(e);
  10. } catch (TemplateException e) {
  11. throw new IllegalArgumentException(e);
  12. }
  13. }

代码示例来源:origin: perwendel/spark

  1. /**
  2. * Creates a new ModelAndView object with given arguments.
  3. *
  4. * @param model object.
  5. * @param viewName to be rendered.
  6. * @return object with model and view set.
  7. */
  8. public ModelAndView modelAndView(Object model, String viewName) {
  9. return new ModelAndView(model, viewName);
  10. }

代码示例来源:origin: perwendel/spark-template-engines

  1. /**
  2. * {@inheritDoc}
  3. */
  4. @Override
  5. public String render(ModelAndView modelAndView) {
  6. try {
  7. StringWriter stringWriter = new StringWriter();
  8. Template template = configuration.getTemplate(modelAndView.getViewName());
  9. template.process(modelAndView.getModel(), stringWriter);
  10. return stringWriter.toString();
  11. } catch (IOException | TemplateException e) {
  12. throw new IllegalArgumentException(e);
  13. }
  14. }

代码示例来源:origin: gocd/gocd

  1. public ModelAndView index(Request request, Response response) {
  2. HashMap<Object, Object> object = new HashMap<Object, Object>() {{
  3. put("viewTitle", "Elastic Agent Profiles");
  4. }};
  5. return new ModelAndView(object, null);
  6. }
  7. }

代码示例来源:origin: hujiaweibujidao/Ganks-for-gank.io

  1. /**
  2. * {@inheritDoc}
  3. */
  4. @Override
  5. public String render(ModelAndView modelAndView) {
  6. Template template = velocityEngine.getTemplate(modelAndView.getViewName());
  7. Object model = modelAndView.getModel();
  8. if (model instanceof Map) {
  9. Map<?, ?> modelMap = (Map<?, ?>) model;
  10. VelocityContext context = new VelocityContext(modelMap);
  11. StringWriter writer = new StringWriter();
  12. template.merge(context, writer);
  13. return writer.toString();
  14. } else {
  15. throw new IllegalArgumentException("modelAndView must be of type java.util.Map");
  16. }
  17. }

代码示例来源:origin: gocd/gocd

  1. public ModelAndView index(Request request, Response response) {
  2. HashMap<Object, Object> object = new HashMap<Object, Object>() {{
  3. put("viewTitle", "Config Repos");
  4. }};
  5. return new ModelAndView(object, null);
  6. }
  7. }

代码示例来源:origin: perwendel/spark-template-engines

  1. @Override
  2. public String render(ModelAndView modelAndView) {
  3. Object model = modelAndView.getModel();
  4. if (model instanceof Map) {
  5. String template = null;
  6. try {
  7. template = Resources.toString(Resources.getResource(modelAndView.getViewName()), Charsets.UTF_8);
  8. } catch (IOException ignored) {
  9. }
  10. return jinjava.render(template, (Map<String, Object>) model);
  11. } else {
  12. throw new IllegalArgumentException("modelAndView.getModel() must return a java.util.Map");
  13. }
  14. }
  15. }

代码示例来源:origin: gocd/gocd

  1. public ModelAndView index(Request request, Response response) {
  2. HashMap<Object, Object> object = new HashMap<Object, Object>() {{
  3. put("viewTitle", "Plugins");
  4. }};
  5. return new ModelAndView(object, null);
  6. }
  7. }

代码示例来源:origin: perwendel/spark-template-engines

  1. @Override
  2. public String render(final ModelAndView modelAndView) {
  3. final CompiledTemplate compiledTemplate = loadTemplate(modelAndView.getViewName());
  4. return (String) TemplateRuntime.execute(compiledTemplate, modelAndView.getModel());
  5. }

代码示例来源:origin: gocd/gocd

  1. public ModelAndView index(Request request, Response response) {
  2. Map<Object, Object> object = new HashMap<Object, Object>() {{
  3. put("viewTitle", "Server Roles");
  4. }};
  5. return new ModelAndView(object, "roles/index.vm");
  6. }
  7. }

代码示例来源:origin: perwendel/spark-template-engines

  1. @Override
  2. @SuppressWarnings("unchecked")
  3. public String render(final ModelAndView modelAndView) {
  4. final String templateName = modelAndView.getViewName();
  5. if (modelAndView.getModel() instanceof Map) {
  6. final Map<String, ?> soyMapData = (Map<String, ?>) modelAndView.getModel();
  7. return soyTofu.newRenderer(templateName).setData(soyMapData).render();
  8. }
  9. throw new IllegalArgumentException("Expecting the model to be an instance of Map<String, ?>");
  10. }
  11. }

代码示例来源:origin: gocd/gocd

  1. private ModelAndView index(Request request, Response response) {
  2. HashMap<Object, Object> object = new HashMap<Object, Object>() {{
  3. put("viewTitle", "Kitchen Sink");
  4. }};
  5. return new ModelAndView(object, null);
  6. }
  7. }

代码示例来源:origin: com.sparkjava/spark-template-velocity

  1. /**
  2. * {@inheritDoc}
  3. */
  4. @Override
  5. public String render(ModelAndView modelAndView) {
  6. String templateEncoding = Optional.ofNullable(this.encoding).orElse(StandardCharsets.UTF_8.name());
  7. Template template = velocityEngine.getTemplate(modelAndView.getViewName(), templateEncoding);
  8. Object model = modelAndView.getModel();
  9. if (model instanceof Map) {
  10. Map<?, ?> modelMap = (Map<?, ?>) model;
  11. VelocityContext context = new VelocityContext(modelMap);
  12. StringWriter writer = new StringWriter();
  13. template.merge(context, writer);
  14. return writer.toString();
  15. } else {
  16. throw new IllegalArgumentException("modelAndView must be of type java.util.Map");
  17. }
  18. }

代码示例来源:origin: gocd/gocd

  1. public ModelAndView index(Request request, Response response) {
  2. Map<Object, Object> object = new HashMap<Object, Object>() {{
  3. put("viewTitle", "Artifact Stores");
  4. }};
  5. return new ModelAndView(object, "artifact_stores/index.vm");
  6. }
  7. }

代码示例来源:origin: lamarios/Homedash2

  1. public String render(ModelAndView modelAndView) {
  2. try {
  3. JadeTemplate template = this.configuration.getTemplate(modelAndView.getViewName());
  4. return this.configuration.renderTemplate(template, (Map) modelAndView.getModel());
  5. } catch (IOException var3) {
  6. throw new IllegalArgumentException(var3);
  7. }
  8. }
  9. }

相关文章