請原諒我的問題是否已經與任何現有問題匹配。但是,我走過了很多stackoverflow相關解決方案的線程,但我無法找到我的問題的任何相關解決方案。請幫幫我。我很感謝他們。提前感謝。如何從ajax get()方法的spring控制器返回列表對象作爲json格式調用
實際上,我嘗試發送列表對象作爲json格式的ajax調用的響應。
這是我的代碼我寫的實現這一功能,
My Controller
package controllers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.daos.StudentDao;
import com.user.model.Student;
@Controller
public class Registration {
@RequestMapping(value = "/registration", method = RequestMethod.GET)
public @ResponseBody List studentReg() {
Map<Integer, String> map1 = new HashMap();
map1.put(1, "a");
map1.put(2, "b");
Map<Integer, String> map2 = new HashMap();
map2.put(3, "c");
map2.put(4, "d");
Map<Integer, String> map3 = new HashMap();
map3.put(5, "e");
map3.put(6, "f");
List<Map> list = new ArrayList();
list.add(map1);
list.add(map2);
list.add(map3);
return list;
}
}
My spring cinfiguration file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<mvc:annotation-driven />
<context:component-scan base-package="controllers" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
And my Ajax call is
$(document).ready(function($) {
$("#abc").click(function(event) {
event.preventDefault();
$.ajax({
type : "Get",
url : "registration",
success : function(response) {
alert(response);
},
error : function(e) {
alert('Error: ' + e);
}
});
});
});
最後我得到這個異常,同時呈現迴應http://d.pr/i/lnzL 請幫我在我犯錯的地方。預先感謝。並且我將jackson jar添加到我的classpath以及lib文件夾中。
也許你不得不在classpath – gipinani
@mserioli添加傑克遜罐子:我添加這些罐子,請看這裏http://d.pr/i/IM7T – vikky