AJAX fetching data from Spring-MVC Controller -


enter image description here controller:

@requestmapping(value = "jobs") public void removejobs(@requestparam("username") string username, model model) {    string []jobs = jobsfetcher(username);    model.addattribute("list", jobs);  } 

my jsp:

<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head>    <meta http-equiv="content-type" content="text/html; charset=utf-8">    <title>insert title here</title>    <link type="text/css" rel="stylesheet" href="<c:url value="public/files/styles/jobmanager.css" />" />  <script type="text/javascript"> function ajaxfunction(username) {     var xmlhttpreq = crossbrowserajaxobject();     if (xmlhttpreq) {         xmlhttpreq.open("get", "jobs", true);         xmlhttpreq.onreadystatechange = handleserverresponse(xmlhttpreq);         xmlhttpreq.setrequestheader('content-type',                 'application/x-www-form-urlencoded');         xmlhttpreq.send("username=" + username);     } }  function crossbrowserajaxobject() {     var xmlhttp;     if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari         xmlhttp = new xmlhttprequest();     } else {// code ie6, ie5         xmlhttp = new activexobject("microsoft.xmlhttp");     }     return xmlhttp; }  function handleserverresponse(xmlhttpreq) {     //var xmlhttp = new xmlhttprequest();     return function() {         alert(xmlhttpreq.readystate);         if (xmlhttpreq.readystate == 4 && xmlhttpreq.status == 200) {             alert(xmlhttpreq.responsetext); // ok             // here how model object response comming servlet &             // use in jsp file ?         }     }; } </script> </head> <body> <div>     <div class="leftdiv">         <a href="#" onclick="ajaxfunction('james')">james</a>          <a href="#" onclick="ajaxfunction('david')">david</a>          <a href="#" onclick="ajaxfunction('catrina')">catrina</a>          <a href="#" onclick="ajaxfunction('cathy')">cathy</a>          <a href="#" onclick="ajaxfunction('paul')">paul</a>     </div>      <div class="rightdiv">         <!-- how job list & use here using ajax ? -->         <c:foreach items="???" var="task">             <p>${task}</p>         </c:foreach>     </div> </div> </body> </html> 

now have mentioned inside picture, want right div element change when click employees without refreshing whole page.

the thing don't know when xmlhttpreq.responsetext returns me, how i'm gonna fetch modelelement carry array of jobs using ajax & use render page...

in words how able fetch parameters coming controller after ajax call & use ajax make page ?

do idea or suggestion or can me go through ?

( way these codes not yet tested! )

take @ jquery makes ajax requests rather simpler.

you need change controller method include annotation @responsebody on return type. don't add stuff model ajax requests, won't work unless return new modelandview .

here intro


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -