mercredi 18 février 2015

how can i modify a freemarker variable value inside a success block in jQuery AJAX

how can i change a value of a freemarker variable inside a success block in jQuery AJAX, i have two controllers for my page the first one returns me a simple string with the name of the view with a GET method, the second one is the one that process the data using a json with a POST method


here they are



@RequestMapping(value = "myform", method = RequestMethod.GET)
public String formmethod(Model model) {



model.addAttribute("successMessage", "i'm in the firts controller");

return "forms/myform";
}


my second controller



@RequestMapping(value = "myform", method = RequestMethod.POST)
public @ResponseBody String getTags(@RequestBody final String json, Model model)
throws IOException
{
ObjectMapper mapper = new ObjectMapper();

User userMapped= mapper.readValue(json, User.class);

User person = new Usuario();
person.setName("new name");
person.setLastName("new lastname");


model.addAttribute("successMessage", person.getName());

return toJson(userMapped);
}


my to Json method



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


and my page myform.html



<script src="http://ift.tt/KA8c68"></script>

<script type="text/javascript">

function doAjaxPost()
{
// get the form values
var name= $('#name').val();
var lastName = $('#lastName ').val();

var json = {"name" : name, "lastName " : lastName };
console.log(json);
var FreeMarkervariable = "${successMessage}";

//this brings me the value that i put in the firts controller


$.ajax(
{
type: "POST",
url: "myform",
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)
{


//HERE I WANT TO CHANGE THE VALUE OF MY FREEMARKER VARIABLE SO I CAN
//PRINT A SUCCESS MESSAGE IN A DIV


<#assign successMessage = "success">



},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}

});
}
</script>
<!-- NEW WIDGET START -->
<article class="col-sm-12">
<div class="alert alert-warning fade in">
<button class="close" data-dismiss="alert">
×
</button>
<i class="fa-fw fa fa-warning"></i>
<strong>${successMessage} I WANT TO PRINT A SUCCESS MESSAGE HERE </strong>
</div>
</article>
<!-- WIDGET END -->
<fieldset>
<legend>Name in view</legend>
<form name="myform">
Name in view: <input type="text" name="name">
<br>
Last Name in view: <input type="text" id="lastName" name="lastName">
<br>
<input type="button" value="Add Users" onclick="doAjaxPost()">

</form>
</fieldset>
<br>


so far my freemarker variable gets the value that i put inside the success block but it appears "success" before i press my submit button, i believed that the succes block was executed after i hit the submit button so i dont know why it have the value even before i press the button it should have "i'm in the firts controller" before i press the submit button


Aucun commentaire:

Enregistrer un commentaire