Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
67
rated 0 times [  71] [ 4]  / answers: 1 / hits: 7740  / 9 Years ago, thu, february 19, 2015, 12:00:00

i cant get the value of a freeMarker variable when i put my code in a external javascript file



here is my page when the javascript code inside, this works i can get the value of my freemarker variable in this way:



<#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



<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



here are my controllers i'm processing the form using 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;
}
}

More From » java

 Answers
5

You can move your variable into a script block in the html page.



<#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.



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.


[#39129] Wednesday, February 18, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
masonm

Total Points: 167
Total Questions: 87
Total Answers: 103

Location: Rwanda
Member since Wed, Jun 8, 2022
2 Years ago
masonm questions
;