Issue
I have recently been studying Angular. All the examples I am seeing regarding the backend return the data in json format.
Since I have the backend written in Spring mvc with return of views, is there a way to work on the frontend with Angular?
Unfortunately I have not found such examples.
Can anyone kindly show me how to frontend in Angular like this?
I put a piece of code about creating a Customer in SpringMVC.
Controller:
@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" />
Solution
I'm afraid that what are you trying to do is not possible as Angular projects works as single-page applications while Spring MVC does not. To work with Angular and Spring you should use Spring as backend developing an API to provide service to the Angular API.
There's an example that you may find usefull: https://www.baeldung.com/spring-boot-angular-web
Answered By - jcobo1