英文:
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:
<html>
<head>
// head content
</head>
<body> // But no close tag for body.
In other ftl, I will do:
<#include "header.ftl">
// then continue with body content
This is what I'm doing:
In the UI controller that handles multiple RequestMappings, I add:
@ModelAttribute("client")
public Client getTheClient() {
return new Client();
}
Other methods in this UI controller are mostly:
@RequestMapping(value="/apple")
public void apple(HttpServletRequest req, HttpServletResponse res) {
// bind the page with a "apple.ftl" templage
}
In the head tag, I do:
<script type="text/javascript">${client.getScript()}</script>
But it cannot find "client". (Says undefined or something)
Then I add
<#import "/spring.ftl as spring">
<@spring.bind "client">
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("/client/get", method = RequestMethod.GET)
public ModelAndView getTheClient() {
ModelAndView mav = new ModelAndView("index");
mav.addObject("client", new Client());
return mav;
}
Second, your way of referencing the model an accessing it is incorrect. The correct way
<script type="text/javascript">${client.name}</script>
Assuming model name is Client
, and the field is name
(no need to use set or get prefexes).
专注分享java语言的经验与见解,让所有开发者获益!
评论