2013-08-20 39 views
0

我是Spring框架的新手,我有一个问题。 我有一个页面A.jsp,该页面中我有一个链接到页面B.jspSpring dojo请求问题

<c:url value="${pageContext.request.contextPath}" var="contextPath" /> 
Click <a href="${contextPath}/pageB">here</a> 

并在控制器

@RequestMapping("pageB") 
public String pageBlink(SitePreference sitePreference, Device device, Model model) { 
return "pageB";  
} 

现在页面上B.jsp我要调用一个Ajax呼叫。

I have a link <a href="javascript:myFunction();">Send request</a> 

function myFunction(){ 
dojo.xhrGet({ 
// The URL of the request 
url: "requestPage", 
method: "POST", 
handleAs: "json", 
// The success callback with result from server 
load: function(jsonData) { 
var content = ""; 
           dojo.forEach(jsonData.newsItems,function(locationPoint) { 
// Build data from the JSON 
content += "<p>" + locationPoint.name + "</p>"; 
content += "<p>" + locationPoint.latitude + "</p>"; 
content += "<p>" + locationPoint.longitude + "</p>"; 
content += "<p>" + locationPoint.number + "</p>"; 
}); 
}, 
// The error handler 
error: function() { 
// Do nothing -- keep old content there 
}, 
// generate an extra GET variable to prevent browsers from caching 
preventCache: true 
}); 
} 

并添加到控制器

@RequestMapping(value="requestPage", method = RequestMethod.GET) 
public MyObj returnEVSELocations(){ 
    logger.log(Level.INFO, "return evse locations --------------"); 
    MyObj myObj = new MyObj(); 
    // add some stuff into the obj 

    return myObj; 
} 

但这一要求一个requestPage.jps ......我只想在我的网页(B.jsp)工作。 任何帮助都是值得欢迎的。 谢谢!

+0

我不明白你的最后一句话,其中包含的问题和它应该是什么,所以很难给出答案。你能提供更多的信息吗? – g00glen00b

+0

我的意思是在控制台中的错误消息说,没有找到requestPage.jsp页面。我想在同一页面中检索响应。 – tinti

回答

0

我发现了这个问题。 事实上有2个问题 1.在Ajax调用我必须 dojo.forEach(jsonData, ...),而不是dojo.forEach(jsonData.newsItems, ...) 2.在控制器上我的方法,我必须添加注释

public @ResponseBody MyObj 

我希望这种帮助别人前面同样的问题。

+0

你可以在这里找到答案我猜:http://stackoverflow.com/questions/4203333/spring-mvc-json-response – Philippe