如何在 Angular 中显示来自 Spring MVC 的 return 视图
How to show the return views from Spring MVC in Angular
最近在学习Angular。我看到的关于后端 return 数据的所有示例都是 json 格式。
因为我的后端是用 Spring mvc 编写的,有 return 个视图,有没有办法在前端使用 Angular?
很遗憾我没有找到这样的例子。
任何人都可以告诉我如何像这样在 Angular 中使用前端吗?
我在 SpringMVC 中放了一段关于创建客户的代码。
控制器:
@RequestMapping(value = "addcustomer", method = RequestMethod.POST)
public ModelAndView createCust(@RequestParam("codeperson") String codeperson, @RequestParam("surname") String surname,
@RequestParam("name") String name, ModelAndView mv) {
Customer customer = new Customer();
customer.setCodeperson(codeperson);
customer.setSurname(surname);
customer.setName(name);
int counter = customerDao.createCustomer(customer);
if (counter > 0) {
mv.addObject("msg", "OK.");
} else {
mv.addObject("msg", "Error!");
}
mv.setViewName("createcustomer");
return mv;
}
DAO:
public int createCustomer(Customer customer) {
String sql = "insert into cliente(codeperson,surnameme,name) values(?,?,?)";
try {
int counter = jdbcTemplate.update(sql,
new Object[] { customer.getCodeperson(), customer.getSurname(), customer.getName()});
return counter;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
createcustomer.jsp
<form action="addcustomer" method="post">
<pre>
Code person: <input type="text" name="codeperson" />
Surname: <input type="text" name="surname" />
Name: <input type="text" name="name" />
<input type="submit" value="Add customer" />
恐怕您尝试做的事情是不可能的,因为 Angular 项目作为单页应用程序工作,而 Spring MVC 则不能。要使用 Angular 和 Spring,您应该使用 Spring 作为后端开发 API 来为 Angular 前端提供服务。
下面是一个您可能会觉得有用的示例:
https://www.baeldung.com/spring-boot-angular-web
最近在学习Angular。我看到的关于后端 return 数据的所有示例都是 json 格式。
因为我的后端是用 Spring mvc 编写的,有 return 个视图,有没有办法在前端使用 Angular?
很遗憾我没有找到这样的例子。
任何人都可以告诉我如何像这样在 Angular 中使用前端吗?
我在 SpringMVC 中放了一段关于创建客户的代码。
控制器:
@RequestMapping(value = "addcustomer", method = RequestMethod.POST)
public ModelAndView createCust(@RequestParam("codeperson") String codeperson, @RequestParam("surname") String surname,
@RequestParam("name") String name, ModelAndView mv) {
Customer customer = new Customer();
customer.setCodeperson(codeperson);
customer.setSurname(surname);
customer.setName(name);
int counter = customerDao.createCustomer(customer);
if (counter > 0) {
mv.addObject("msg", "OK.");
} else {
mv.addObject("msg", "Error!");
}
mv.setViewName("createcustomer");
return mv;
}
DAO:
public int createCustomer(Customer customer) {
String sql = "insert into cliente(codeperson,surnameme,name) values(?,?,?)";
try {
int counter = jdbcTemplate.update(sql,
new Object[] { customer.getCodeperson(), customer.getSurname(), customer.getName()});
return counter;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
createcustomer.jsp
<form action="addcustomer" method="post">
<pre>
Code person: <input type="text" name="codeperson" />
Surname: <input type="text" name="surname" />
Name: <input type="text" name="name" />
<input type="submit" value="Add customer" />
恐怕您尝试做的事情是不可能的,因为 Angular 项目作为单页应用程序工作,而 Spring MVC 则不能。要使用 Angular 和 Spring,您应该使用 Spring 作为后端开发 API 来为 Angular 前端提供服务。
下面是一个您可能会觉得有用的示例: https://www.baeldung.com/spring-boot-angular-web