mardi 24 février 2015

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


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;
}
}

Aucun commentaire:

Enregistrer un commentaire