spring mvc - AJAX mechanics when updating view parts -
i learning ajax atm , far best application of form validation.
my conceptual question best way deal design issues when example need update page fragment.
basic scenario:
we have page 1 , page 2 both pages have same menu , footer. page 1 contains neatly formatted table content of statistics , page 2 contains messages. want implement ajax menu stays there on top when move page 1 page 2 , vice-versa.
for instance have request handler returns json server. page 1 ajax request, our server returns table data, page 2 messages.
the confusion how , store design information format page fragment of returning data correctly? common strategy - manually apply css on data on "succeed" event within ajax method? not introduce disjointness between client , server server applies formatting standard requests? , if content data returning ajax request demands dramatically different design in example gave you, means externalizing majority of html , css application javascript believe problem if are:
- updating big fragments of page - code becoming unmaintainable on client side, or need template engine on client side too.
- using template engines on server-side , plain .jsp in case of java. - example syntax , structure of view chunks of "apache tiles" , have write plain html css client side ajax response handling section.
of course can return xml seems development trajectory goes in favor of json, in case of spring framework example converter job automatically..
i believe little concern vaadin or gwt users, taking side traditional mvc , not using code generators.
to narrow down context particularly interested how people deal in spring mvc jsp , spring mvc + apache tiles.
also can possibly send html design metadata data in json client, or horrible thing do? again disparity view rendering technologies on server side.
i believe missing fundamental , important in here.
thank you,
if re-using menu , footer across many pages in system, indeed recommend using template engine thymeleaf, jsp tags, or apache tiles.
but think curious how manage transitions of partial page content through ajax, , (rightly) not want make controller layer responsible generating page fragments.
here, generate single page contains both page 1 table, , page 2 messages:
<div id="page1" style="display: none"> <%--table structure, without data--%> </div> <div id="page2" style="display: none"> <%--message display, without message content--%> </div>
now, create 2 spring mvc controller methods return raw data in json format, 1 returns table data, , returns messages.
finally, write javascript handlers invoke controllers, populate data, , manage page transitions. use jquery syntax brevity:
$.getjson( 'mycontroller/page1', params, function( jsondata ) { // populate table data $( '#page1' ).show(); $( '#page2' ).hide(); } );
to show table data, and:
$.getjson( 'mycontroller/page2', params, function( jsondata ) { // populate message text $( '#page1' ).hide(); $( '#page2' ).show(); } );
to show messages.
of course, using javascript, can replace show() fancy animated transitions slide or fadein, if wish.
Comments
Post a Comment