将Spring模型发送到Freemarker的头标签,无特定路由。

huangapple 未分类评论72阅读模式
英文:

Send Spring model to Freemarker head tag with no specific route

问题

我在使用Spring向Freemarker发送数据时遇到了问题。我正在使用一个服务,需要从调用方法中获取脚本,然后将脚本放入头标签中。我在所有页面中都使用相同的header.ftl。换句话说,我的head.ftl大致如下:

<html>
<head>
   // 头部内容
</head>
<body> // 但是没有body的闭合标签。

在其他ftl文件中,我会这样做:

<#include "header.ftl">
// 然后继续正文内容

以下是我正在做的事情:

在处理多个RequestMappings的UI控制器中,我添加了:

@ModelAttribute("client")
public Client getTheClient() {
    return new Client();
}

该UI控制器中的其他方法大致如下:

@RequestMapping(value="/apple")
public void apple(HttpServletRequest req, HttpServletResponse res) {
  // 将页面与"apple.ftl"模板绑定
}

在头标签中,我这样做:

<script type="text/javascript">${client.getScript()}</script>

但它找不到"client"。(显示未定义或其他内容)
然后我添加了:

<#import "/spring.ftl as spring">
<@spring.bind "client">

在脚本之前,现在它显示"读取导入的spring.ftl时出错"。(我也不知道spring.ftl在哪里。我只是按照互联网上的其他帖子进行操作。)

其他帖子都在讨论为RequestMapping提供特定路由,并且大多数情况下在前端使用jsp。但在我的情况下有些不同。

有谁可以帮忙吗?我非常感激!

英文:

I'm having a problem with sending from Spring to Freemarker. I'm using a service where I need to get script from calling a method and put the script in head tag. I'm using the same header.ftl for all the pages. That is, my head.ftl is something like:

&lt;html&gt;
&lt;head&gt;
   // head content
&lt;/head&gt;
&lt;body&gt; // But no close tag for body.

In other ftl, I will do:

&lt;#include &quot;header.ftl&quot;&gt;
// then continue with body content

This is what I'm doing:

In the UI controller that handles multiple RequestMappings, I add:

@ModelAttribute(&quot;client&quot;)
public Client getTheClient() {
    return new Client();
}

Other methods in this UI controller are mostly:

@RequestMapping(value=&quot;/apple&quot;)
public void apple(HttpServletRequest req, HttpServletResponse res) {
  // bind the page with a &quot;apple.ftl&quot; templage
}

In the head tag, I do:

&lt;script type=&quot;text/javascript&quot;&gt;${client.getScript()}&lt;/script&gt;

But it cannot find "client". (Says undefined or something)
Then I add

&lt;#import &quot;/spring.ftl as spring&quot;&gt;
&lt;@spring.bind &quot;client&quot;&gt;

before the script, now it says "Error reading imported spring.ftl". (I don't know where is spring.ftl either. I just follow other posts on the internet.)

Other posts are talking about giving a specific route to RequestMapping, and mostly uses jsp for frontend. But in my case it's different.

Can anyone help? I really appreciate it!!!

答案1

得分: 0

我不知道您的“Client”对象是什么样子的,将您的模型发布出来可能会有帮助。

不过,这里有几点我注意到的地方:

首先,每个控制器都需要URL映射。在您的getTheClient()方法中添加一个:

@RequestMapping("/client/get", method = RequestMethod.GET)
public ModelAndView getTheClient() {
    ModelAndView mav = new ModelAndView("index");
    mav.addObject("client", new Client());

    return mav;
}

其次,您引用模型和访问模型的方式是不正确的。正确的方式是:

<script type="text/javascript">${client.name}</script>

假设模型名称是Client,字段名是name(不需要使用set或get前缀)。

英文:

I don't know your Client object looks likes, could be helpful to post your model as well.

However, here are couple things I spotted:

First, every controller needs url mapping. Add one to your getTheClient() one:

@RequestMapping(&quot;/client/get&quot;, method = RequestMethod.GET)
public ModelAndView getTheClient() {
    ModelAndView mav = new ModelAndView(&quot;index&quot;);
    mav.addObject(&quot;client&quot;, new Client());

    return mav;
}

Second, your way of referencing the model an accessing it is incorrect. The correct way

&lt;script type=&quot;text/javascript&quot;&gt;${client.name}&lt;/script&gt;

Assuming model name is Client, and the field is name (no need to use set or get prefexes).

huangapple
  • 本文由 发表于 2020年8月15日 00:50:58
  • 转载请务必保留本文链接:https://java.coder-hub.com/63416957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定