[英]i can't access freemarker variables in external javascript file
i cant get the value of a freeMarker variable when i put my code in a external javascript file 当我将代码放入外部javascript文件时,我无法获取freeMarker变量的值
here is my page when the javascript code inside, this works i can get the value of my freemarker variable in this way: 这是我的页面,其中包含javascript代码时,这可以通过以下方式获取我的freemarker变量的值:
<#import "../masterPage.html" as layout>
<@layout.masterPageLay bread1="my bread1" bread2="my bread2">
<#assign title="value 1">
<#assign subtitle="value 2">
<script>
function doAjaxPost()
{
var name= $('#name').val();
var lastName= $('#lastName').val();
var json = {"name" : name, "lastName" : lastName};
console.log(json);
var variableFreeMarker = "${freeMarkValue}";
console.log('this value is: ' + variableFreeMarker);
$.ajax(
{
type: "POST",
url: "myUrl",
data: JSON.stringify(json),
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
beforeSend: function(xhr)
{
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success: function(data)
{
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}
/* error: function (xhr, ajaxOptions, thrownError)
{
alert(xhr.status);
alert(xhr.responseText);
alert(thrownError);
}*/
});
}
</script>
<form name="myform">
Name in view: <input type="text" id="name" name="name">
<br>
Last Name in view: <input type="text" id="lastName" name="lastName">
<br>
Show modify name in view: <input type="text" id="modifyname" name="modifyname">
<br>
<input type="button" value="Add Users" onclick="doAjaxPost()">
</form>
<br>
</@layout.masterPageLay>
but if i put my javascript code in a external file in this case myPageScript.js
and then call that script in my page i cant get the value of my freeMarker variable this is how i'm calling my script 但是,如果我在这种情况下将我的javascript代码放在外部文件
myPageScript.js
,然后在我的页面中调用该脚本,则无法获取我的freeMarker变量的值,这就是我调用脚本的方式
<script src="../resources/js/scripts/myPageScript.js"></script>
and this is my page that dont work 这是我的页面不起作用
<#import "../masterPage.html" as layout>
<@layout.masterPageLay bread1="my bread1" bread2="my bread2">
<#assign titulo="value 1">
<#assign subtitulo="value 2">
<script src="../resources/js/scripts/myPageScript.js"></script>
<form name="myform">
Name in view: <input type="text" id="name" name="name">
<br>
Last Name in view: <input type="text" id="lastName" name="lastName">
<br>
Show modify name in view: <input type="text" id="modifyname" name="modifyname">
<br>
<input type="button" value="Add Users" onclick="doAjaxPost()">
</form>
<br>
</@layout.masterPageLay>
this output in my chrome console "${freeMarkValue}"
instead of the value of the variable 此输出在我的Chrome控制台
"${freeMarkValue}"
而不是变量的值
here are my controllers i'm processing the form using jquery ajax 这是我的控制器,我正在使用jQuery Ajax处理表单
@RequestMapping(value = "myForm", method = RequestMethod.GET)
public String myForm(Model model) {
model.addAttribute("freeMarkValue", "controll");
return "myForm";
}
@RequestMapping(value = "myForm", method = RequestMethod.POST)
public @ResponseBody String getTags(@RequestBody final String json, Model model)
throws IOException
{
ObjectMapper mapper = new ObjectMapper();
User objetmapped = mapper.readValue(json, User .class);
User person = new User iox();
person.setName(objetmapped .getName());
person.setLastName(objetmapped .getLastName());
);
model.addAttribute("freeMarkValue", "second controller value");
return toJson(objetmapped );
}
private String toJson(User person)
{
ObjectMapper mapper = new ObjectMapper();
try
{
String value = mapper.writeValueAsString(person);
// return "["+value+"]";
return value;
}
catch (JsonProcessingException e)
{
e.printStackTrace();
return null;
}
}
You can move your variable into a script block in the html page. 您可以将变量移到html页面的脚本块中。
<#import "../masterPage.html" as layout>
<@layout.masterPageLay bread1="my bread1" bread2="my bread2">
<#assign titulo="value 1">
<#assign subtitulo="value 2">
<script src="../resources/js/scripts/myPageScript.js"></script>
<script>
// This variable can be accessed from myPageScript.js
var variableFreeMarker = "${freeMarkValue}";
</script>
<form name="myform">
Name in view: <input type="text" id="name" name="name">
<br>
Last Name in view: <input type="text" id="lastName" name="lastName">
<br>
Show modify name in view: <input type="text" id="modifyname" name="modifyname">
<br>
<input type="button" value="Add Users" onclick="doAjaxPost()">
</form>
Or add it as a value of a hidden input etc.. 或将其添加为隐藏输入等的值。
<input type="hidden" id="myVal" value="${freeMarkValue}">
Your JS (in a seperate script) would then need to read the value for example using jQuery. 然后,您的JS(在单独的脚本中)将需要例如使用jQuery读取值。
var aValue = $("#myVal").val();
I use the first method for common stuff such as adding a date format string that is specific to the user on to every page. 我将第一种方法用于常见内容,例如在每个页面上添加特定于用户的日期格式字符串。 They will have global scope so be careful with naming.
它们将具有全球范围,因此命名时要小心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.