tags:

views:

308

answers:

2

I defined a modelview object named "buttonpressed" in spring controller file and I need to access that modelview object in ftl(freemarker) file which is being returned as a view from a controller like abcd.java

The abcd.java controller code is as below

if (questionAnswer.getAnswerId() == 1045) 
  {
    modelAndView.addObject("buttonPressed","You have been added in mailing list");
    modelAndView.setViewName("enterCode_nextSteps");
  } 
else 
  {
    modelAndView.addObject("buttonPressed","Not added in the mailing list");
    modelAndView.setViewName("enterCode_nextSteps");
  }

the below ajax function is working fine currently but I am not sure how to access this object called "buttonpressed" in this ajax function. I have written like the way mentioned below but when i am clicking the submit link its not calling "partner.do" and also throwing error saying #buttonPressed is undefind (but in the script below its working fine and calling "partner.do" and even posting data)

So is that problem coming from javsscript code i mean due to incorrect use of "buttonPressed" or might be problem from spring controller abcd.java file.

<div class="partnerOptInBox">
  <div id="optInContent">
    <form name="partnerOptIn">
      <h4>Want the Latest</h4>
      <p class="pad10Top">${partnerOpt.translation}</p>
      <div class="pad10Top">
        <input type="radio" name="questionAnswer['${partnerOpt.questionId}']" value="${partnerOpt.getAnswers()[0].answerId}" class="radioButton" /> <label for="questionAnswer['${partnerOpt.questionId}']" class="formLabel pad20Right">Yes</label> <input type="radio" name="questionAnswer['${partnerOpt.questionId}']" class="radioButton" value="${partnerOpt.getAnswers()[1].answerId}" /> <label for="questionAnswer['${partnerOpt.questionId}']" class="formLabel">No</label>
      </div>
      <div id="optInError" class="formError" style="display:none;">Oops... your request did not go through, please try again.</div>
      <div class="pad15Top">
        <a href="javascript:submitOptIn();"><img src="images/theme/btn_opt_in_submit.gif"/></a>
      </div>                        
    </form>
    <script type="text/javascript">
  function submitOptIn() {
    $('optInError').hide();
    dataString = $('#partnerOptIn').serialize();
    $.ajax({
     data: dataString,
     url: "partnerOpt.do",
     timeout: 30000,
     type: "POST",
       success: function(html){
        var newHtml = "<h4>Thank you</h4><p>We appreciate your time to respond to our request.</p>";
        $('#optInContent').html(newHtml);
},

*/trying this code for sucess is throwing me an error /*
<!-- buttonpressed function-->
    success: function(html){
      $('#optInContent').html(${buttonPressed});
    },

<!-- buttonpressed function-->

error: function(){
      $('#optInError').show();
      }
  });
}
</script>
A: 

Assuming the HTML code shown is part of the view in which model from abcd.java is accessible, you need to enclose ${buttonPressed} in quotes when you're invoking $('#optInContent').html() on it because it's a string.

ChssPly76
A: 

Or if it doesn't work then you can output as javascript variable on the top of the page and read later using DOM,

var buttonpressed = ${buttonPressed}, but i think above solution will work. But generally this approach works best if you have lot of javascript work in a page and needs backend data and you don't want to do AJAX calls.

narup