I am working on a tutorial problem using Spring MVC, with another teammate for an internship. I'm not familiar with Spring (that's why I'm learning it)
We first wrote the code without any framework support, plain old hand-written MVC
We are using Spring 2.5, and integrated Hibernate, used the Autowire, Controller, Repository annotations etc.. I'm not interested in the view part yet. So we linked to the database, but the models remained the old models something like :
public class Client {
private String name;
private String bankAccount;
public String getName() {
return name;
}
public String getBankAccount() {
return bankAccount;
}
public Client(String name, String bankAccount) {
this.name = name;
this.bankAccount = bankAccount;
}
}
How is this model properly done as the 'M' part in the Spring 2.5 MVC? I'm thinking something among the lines of autowiring, and constructor injection and remaining immutable.
I just can't wrap my mind how this is properly done in the framework. Any help would be greatly appreciated, thanks.
CodePudding user response:
Basically, your model is a mechanism for the controller to send information to the view, thus it's just great the way it is; no changes are required. You'll perform something like this to use the framework with your model:
@RequestMapping(value="/client/view")
public String viewClientPage(Model m) {
Client client = new Client( "sanyam", "123456" );
m.addAttribute( "client", client );
return "[name of a client using view goes here]";
}
in the view to get:
<h1>Name : ${client.name}</h1>
<h2>Account Number : ${client.account}</h2>
CodePudding user response:
In Spring MVC - as you mentioned, we can focus just on M - Model and C - Controller part. If I understood your question correct, you asking "How to use Client model correctly with Spring framework? " So if you not focus on view part, model is place where you can get data from database and fill the details and return it to the caller of that controller. Good way to handle M-C flow ===>
- Caller call the controller / API / URL.
- Controller (@Controller) receive request from Dispatcher Servlet.
- Controller pass the Request to Service (@Service) class method.
- Service class pass the request to Repository (@Repository) class method.
- Repository method use Hibernate session, run query and fetch /update data.
- Populate Entity beans with data.
- Repository return that bean/data to Service to Controller.
- Now if you have view you can create Model object and set data and view name OR just return ResponseEntity<> with status code and data. So, Model in MVC is data Spring work on through out this process. Good way to handle this, is keep Data you returning from Controller separate Java Class / bean and Entity Beans/ classes different (use to populate from database tables). Transformation of these two beans can be done at Service class level. You can use Annotations at fields level or constructor level. And it is prototype scope for all these spring beans.
Hope I was able to help. If there is more question or I gave little different answer let me know.