0

我在使用 JSON 的 Spring MVC 中遇到问题我试图从我的控制器返回一个数组列表,但我无法返回它

function getCategory() {
    alert("Test"); 
    $.getJSON("getCategoryList.do", { message: "Test Message" }, function(value) {
        alert(value[1]);
        alert("inside Json"); 
        alert(JSON.stringify(value));;
        for(var i=0;i<value.length;i++){

        }

    });

}

我检查在我的控制器中打印消息它的工作,但是当我尝试在我的 jsp 页面中设置值时,我无法确定控制器代码是

@RequestMapping(value="admin/getCategoryList", method=RequestMethod.GET)public  @ResponseBody ArrayList<String> receiveCategoryList(@RequestParam String message) {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add(name);
        arrayList.add("success");
        System.out.println("Inside Method");
        System.out.println(name);
        return arrayList;
    }

任何人都可以帮助我

4

1 回答 1

0

To return json data from spring you need to include jackson jars in your pom and than you have to just declare

@Responsebody in your method where you returning json data That its , you can than directly use that object in your view ,

I tried it with Angular Its working .

@RequestMapping(value="/getTeamManager",method=RequestMethod.GET)

public @ResponseBody List getEmployees (Model model){

Employee employee1 =new Employee ();
employee1.setName("Himanshu");
employee1.setState("Haryana ");
employee1.setCountry("India ");

Employee employee2 =new Employee ();
employee2.setName("Vijay");
employee2.setState("Haryana ");
employee2.setCountry("India ");

Employee employee3 =new Employee ();
employee3.setName("Himanshu");
employee3.setState("Haryana ");
employee3.setCountry("India ");

List<Employee> employeeList =new ArrayList<Employee>();

employeeList.add(employee1);
employeeList.add(employee2);
employeeList.add(employee3);


return employeeList;

}

于 2015-01-16T06:29:34.967 回答