Java AngularJs如何从Spring MVC控制器访问模型属性值

Java AngularJs如何从Spring MVC控制器访问模型属性值,java,angularjs,spring-mvc,Java,Angularjs,Spring Mvc,我有一个SpringMVC控制器返回一个带有如下属性的页面 @RequestMapping(method = RequestMethod.GET, value = "/create") public ModelAndView getAddAccountView() { ModelAndView model = new ModelAndView("protected/accounts/AccountAddView"); List<Client> clients=clie

我有一个SpringMVC控制器返回一个带有如下属性的页面

@RequestMapping(method = RequestMethod.GET, value = "/create")
public ModelAndView getAddAccountView() {
    ModelAndView model = new ModelAndView("protected/accounts/AccountAddView");
    List<Client> clients=clientService.findAll();
    model.addObject("listClients", clients);
    return model;
}
var listOfClients=$scope.clients;
但我仍然得到以下错误

angular.min-1.5.3.js:116 Error: [$parse:lexerr] http://errors.angularjs.org/1.5.3/$parse/lexerr?p0=Unexpected%20nextharacter%20&p1=s%2033-33%20%5B%40%5D&p2=clients%3D%5Bsoftbank.ui.model.Client%4042%2C%softbank.ui.model.Client%4041%2C%softbank.ui.model.Client%4043%5D
at Error (native)
at http://localhost:8080/softbank/resources/js/angular.min-1.5.3.js:6:416
at gc.throwError (http://localhost:8080/softbank/resources/js/angular.min-1.5.3.js:212:149)
at gc.lex (http://localhost:8080/softbank/resources/js/angular.min-1.5.3.js:211:16)
at Object.ast (http://localhost:8080/softbank/resources/js/angular.min-1.5.3.js:216:103)
at Object.compile (http://localhost:8080/softbank/resources/js/angular.min-1.5.3.js:225:232)
at hc.parse (http://localhost:8080/softbank/resources/js/angular.min-1.5.3.js:252:380)
at e (http://localhost:8080/softbank/resources/js/angular.min-1.5.3.js:123:317)
at m.$eval (http://localhost:8080/softbank/resources/js/angular.min-1.5.3.js:142:463)
at pre (http://localhost:8080/softbank/resources/js/angular.min-1.5.3.js:271:500)
请问这里怎么了。为什么ng init会生成此错误?
提前感谢您的回答。

我只是从带弹簧的Angular开始,所以我要做的是解释我是如何做到这一点的,您可以看看这是否是一个可行的选择

首先,我没有尝试通过
模型和视图查看我的数据。我让我的“常规”控制器返回视图,但通过角度控制器和服务以及弹簧侧的
@RestController
获取数据(这是两个单独的弹簧控制器)

要仅返回视图,我知道您至少有两个选项。第一,您可以只返回一个,而不返回这样的模型:

public ModelAndView yourView() {
    return new ModelAndView("yourView");
}
或者,以字符串形式返回视图的名称,如下所示:

public String yourView() {
    return "yourView";
}
在这两种情况下,您显然需要正确的
@RequestMapping

在我看来,我有一个angular controller函数,它调用我的关联angular服务,然后调用my
@RestController
等等。我启动了如下数据:

ng controller=“MyController as ctrl”ng init=“ctrl.allThings()”

代码示例:

角度控制器:

self.allThings = function () {
        ThingService.things()
            .then(
                function (data) {
                    self.things = data;
                },
                function (errResponse) {
                    console.error("Error retrieving thing list");
                }
            );
    };
@RequestMapping(value = "/things", method = RequestMethod.GET)
public List<Thing> things() {
    return thingService.findAll();
}
角度服务:

    things: function() {
        return $http.get('/things')
            .then(
                function(response) {
                    return response.data;
                },
                function (errResponse) {
                    return $q.reject(errResponse);
                }
            );
    },
弹簧座控制器:

self.allThings = function () {
        ThingService.things()
            .then(
                function (data) {
                    self.things = data;
                },
                function (errResponse) {
                    console.error("Error retrieving thing list");
                }
            );
    };
@RequestMapping(value = "/things", method = RequestMethod.GET)
public List<Thing> things() {
    return thingService.findAll();
}
@RequestMapping(value=“/things”,method=RequestMethod.GET)
公共物品清单{
return thingService.findAll();
}

我想您知道Spring端的剩余代码。Jackson将为您处理所有对象转换。

旧帖子,但我仍想回答,以便使用Angular with Spring MVC的任何人都能得到一些帮助。
使用模型属性对于角度来说是一个挑战。之前我在模型属性中设置employee数据,比如
mav.addObject(“employee”,employee)
,但是当我试图在jsp内部使用
${employee}
时,Angular无法使用
ng init
指令设置java对象,因为它需要一个java脚本对象。因此,我在模型属性
mav.addObject(“employeeJson”,empJson)
中设置json,并使用
json.Parse()
对其进行解析,将其转换为Java脚本对象,并能够在jsp上与角度表达式一起使用
以下是我遵循的步骤:
1。弹簧控制器
在控制器中,在model属性中设置json。例如,
mav.addObject(“employeeJson”,empJson)
确保在将其添加到
modeldattribute
之前,将引号替换为html实体“否则在解析json时会出现错误
empJson.replaceAll(“\”,“\&\quot;”)
2。Jsp
使用angular init指令,
data ng init=“yourController.getEmployeeData('${employeeJson}')”

3。在Angular js中,解析此json以获取json
var vm=this
vm.employee={}
vm.getEmployeeData=函数(employeeJson){,
vm.employee=JSON.parse(employeeJson);
};
4。在jsp内部
我可以通过以下方式访问员工姓名
{{yourController.employee.firstName}

谢谢您的回答。但有些事情我不明白。何时向spring controller请求视图?你能给我看看你的spring控制器那一面吗?@blaiso,不客气。请看编辑;我添加了几个控制器选项。非常感谢Chieftwo铅笔。我使用了您的策略,它对我来说很好。原因是当angular在浏览器中运行时,.jsp是一个服务器端模板。jsp代码在服务器上呈现,用实际的json字符串替换
${employeeJson}
。当angular运行init时,它现在将实际字符串值硬编码到html中。要实现将json数据直接返回到前端的目标还有很长的路要走。当我调用ajax并从服务器返回json时,接收端即js成功解析此json,但最初在我加载jsp时,my.js需要此json并提供我在模型属性中设置的json,我采取了这种方法。