• Post Reply Bookmark Topic Watch Topic
  • New Topic

Updating a JSP multiple times with ajax calls

 
Edmund Castermund
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I think I'm doing the right thing, and I've hunted around and can't find anything that directly addresses my situation. I am writing a basic crud app, and this is an example of a basic use case:

  • The page loads and I use ${obj.method()} to populate all the relevant areas. One of the elements that is loaded is an edit form that is stored in a template. *no problem*
  • when I click the edit button, an ajax function fades out the existing html, and fades in the edit form that  is in the template. *also, no problem*
  • When I hit "save" a javascript function makes a jquery ajax call to a servlet, which updates the relevant material in the db, creates an ojb with the updated values, and builds a json obj which it sends back to the javascript function. The new data is displayed properly.  *no problem*
  • When I see I made a typo in my edit, I want to re-edit it, but when I click edit, my new changes are not there. The way I have been working thru this is to create another form edit template that is populated with the new values after the successful ajax call. *is this right??*



  • In general, is it fair to say that the normal jsp stuff will only be displayed on the initial load of the page, and from there javascript/jquery/ajax will do all the work. Is that correct?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65661
    129
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Edmund Castermund wrote:The way I have been working thru this is to create another form edit template that is populated with the new values after the successful ajax call. *is this right??*

    No, you should not need to repeat code.

    In general, is it fair to say that the normal jsp stuff will only be displayed on the initial load of the page

    Of course. If you don't refresh the age, the JSP isn't going to be rerun. If that's confusing, perhaps this article on how JSP works might be helpful.

    When the Ajax returns with the new data, the elements that were original filled with JSP expression data should be updated with the JSON data.
     
    Edmund Castermund
    Ranch Hand
    Posts: 102
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks for reply and answering my question.

    As per repeating code, I populate the edit form via an object using a *.tag file. But I can't do that for a subsequent edit that has been made via an ajax call, right?

    So on the initial load the edit form renders populated via jsp, but after an ajax call a new edit form with the updated values needs to be created, and I can't use jsp since we don't have a new http request. So I'm using a javascript template that is update via json on the successful ajax return.

    Am I understanding your comment correctly?
     
    Edmund Castermund
    Ranch Hand
    Posts: 102
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    In general, is it fair to say that the normal jsp stuff will only be displayed on the initial load of the page

    Of course. If you don't refresh the age, the JSP isn't going to be rerun. If that's confusing, perhaps this article on how JSP works might be helpful.

    When the Ajax returns with the new data, the elements that were original filled with JSP expression data should be updated with the JSON data.

    ooops...did not read this last sentence...I'll read that article you posted to. Is there a naming convention with regards to the obj->json? so if the java code is:





    then you would need to arrange the json as




    and then ${dude.getName()} will automatically be updated by the ajax call?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65661
    129
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Or, you could forgo the initial populating with JSP and just always populate it using Ajax and avoid having to come at the data two ways.

    Modern SPAs generally have no server-side templating, so this is a common approach.
     
    What are you doing? You are supposed to be reading this tiny ad!
    the new thread boost feature brings a LOT of attention to your favorite threads
    https://coderanch.com/t/674455/Thread-Boost-feature
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!