在AngularJS中访问Spring MVC模型对象 [英] Accessing Spring MVC Model Object in AngularJS

查看:46
本文介绍了在AngularJS中访问Spring MVC模型对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用JAVA Spring MVC(后端)和AngularJS(前端),JSP(用于视图)和MySQL DB构建的Spring MVC Web表单.用户可以在填写表格后提交表格并搜索现有条目.提交条目后,还会向用户发送一封电子邮件.该条目也将保存在数据库中.在电子邮件正文中,我试图建立一个链接,该链接将用户重定向到填充有与给定id对应的条目的表单.与单击搜索按钮时的方式相同.该表单填充了现有表单字段.

I have a Spring MVC web form built using JAVA Spring MVC (Backend) and AngularJS (frontend), JSP(for view) and MySQL DB. A user is able to submit the form after filling it out and search an existing entry. An email is also sent out to the user once the entry has been submitted. The entry also gets saved on a database. In the email body, I am trying to have a link which redirects the user to the form populated with the entries corresponding to the given id..the same way as when the search button is clicked..the form gets populated with the existing form fields..

当前,我编写了一个控制器,该控制器使用ModelAndView返回带有数据(searchData)的JSP页面.但是,它没有使用条目的详细信息填充表单.如何实现此功能?

Currently I wrote a controller which returns the JSP page with the data (searchData)..using ModelAndView however.. it is not populating the form with the details of the entry. How can I achieve this functionality?

我为控制器编写了以下代码

I wrote the below code for the controller

@RequestMapping(value = "/searchById", method = RequestMethod.GET)
public ModelAndView populateEntrydata(@RequestParam(value = "id") String id) {
    String url= "http://localhost:8080/myform/searchById?id=" + id;
    ModelAndView model = new ModelAndView("index"); //jsp page
    ResponseEntity<Data> searchData = search(id);

    model.addObject("searchById", searchData);
    // String, String, object
    return model;
}  

如何在angularJS控制器中访问模型对象,以便可以使用从ModelAndView接收的内容填充jsp?

How can I access the model object in the angularJS controller so I can populate the jsp with the contents i receive from the ModelAndView?

推荐答案

您在这里犯的错误是混合服务器端表示(JSP)和客户端表示(Angular)层.当您使用诸如Angular(反应或Vue或任何其他JavaScript框架)之类的客户端表示技术时,服务器端只需担心数据(通常是json/xml)而不是视图(html).

The mistake that you are doing here is mixing serverside presentation (JSP) and clientside presentation (Angular) layers. When you are dealing with clientside presentation technologies like Angular (react or vue or any yet-another-javascript framework) all your server side needs to worry about is the data (usually json/xml) not the view (html).

如果您决定使用Angular或任何其他SPA,那么就不用考虑JSP了,创建一个Spring Boot Rest Web服务.

If you made up your mind about Angular or any other SPA, then forget about JSP, create a spring boot rest webservice.

这篇关于在AngularJS中访问Spring MVC模型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆