What's the M, V, and C in Spring MVC? - spring

I've been looking and looking for a simple answer, yet I always end up with different illustrations and/or explanations. I hope someone can help me to understand.
Are the controllers the C in MVC?
Are models/entities the M in MVC?
Is Thymeleaf/HTML our V in MVC?

Model - A model contains the data of the application. A data can be a
single object or a collection of objects.
Controller - A controller contains the business logic of an
application. Here, the #Controller annotation is used to mark the
class as the controller.
View - A view represents the provided information in a particular
format. Generally, JSP+JSTL is used to create a view page. Although
spring also supports other view technologies such as Apache Velocity,
Thymeleaf and FreeMarker.
https://www.javatpoint.com/spring-mvc-tutorial

Related

(How) can a Spring <form: tag handle multiple Model Attributes

After reading related questions, such as:
Spring MVC form handling form multiple pojos
Spring MVC: Having multiple #ModelAttribute in form handling action
I still wonder how could a Spring MVC <form: tag handle multiple modelAttributes. In the answers to the questions above, they either use JSON ( Why?) or use some tricks in the name of the raw input HTML tags.
Could somebody post a minimal, working example here ( two Pojos, a Controller and a view)?

Model, ModelMap, and ModelView in Spring MVC- which one to use

so many pages and links i found, but none of them have clear answer. Even spring docs doesn't say anything about it.
So which one to use on what context?
Model is an interface that defines methods for adding/removing attributes.
From the javadoc:
Primarily designed for adding attributes to the model. Allows for
accessing the overall model as a java.util.Map.
ModelMap is just a Map implementation used for storing values that will be used in the UI.
ModelAndView is an object that combines a Model object (for storing values that will be used in the UI) and a View (used for resolving UIs components like jsp or templates). It was designed to provide both model and view in a single return.

Accessing Spring Controller Name from View

With Spring, how can i retrieve the following Controller attributes in the view?
Controller name
Controller's #RequestMapping URI
Action method name
Action method's #RequestMapping URI
One approach which i have tried is by creating a subclass of HandlerInterceptorAdapter and overriding postHandle. I register my subclass as an mvc:interceptor for a list of given paths - which is clunky to maintain but was the only way to avoid my interceptor being called for ResourceHandler requests (which i don't want). In my postHandle i can easily add the 2 name attributes, but not the URIs...
Parsing from the HttpRequest object requires constraints on all Controller RequestMappings. I.e. i must always map /Controller/Action or equiv scheme. Quite limiting.
Creating an ApplicationContext and querying that with the requestURI is too long-winded.
I am thinking about dropping the HandlerInterceptorAdapter and instead defining a BaseController for all my controllers to extend.
I wanted to ask before i do this, is there a better approach?
You haven't stated why you need to do this (it sometimes helps to include your motivation, as others can suggest alternative approaches).
But I'm guessing that the Spring 3.1 features loosely termed "end point documentation" may do what you are asking... See RequestMappingHandlerMapping in the Spring documentation which doesn't provide a lot of detail, so this example project is the best place to see it in action:
Spring MVC 3.1 Demo App
example controller
example JSP page

struts2 spring jpa layers best practice

I have an app written with Struts2 Spring3 JPA2 and hibernate. In this app i have the following levels :
- struts2 actions
- spring services
- spring DAO
So, one struts action call for a service which can contain calls to one or many dao objects.
In order to display the info on the screen i have created some "mirror" objects for entities; ie: EmailMessage entity has a EmailMessageForm bean that is used to display/gather data from webforms (i don`t know if this is the best practice), and hence my problem.
In EmailMessageServiceImpl i have a method called:
public List < EmailMessage > getEmailMessages(){
//code here
}
and, if i call this from struts action i cannot get dependencies because session has expired (i have TRANSACTION entity manager). So, one solution would be to create another method
List<EmailMessageForm> getEmailMessagesForDisplay()
{
//....
}
and here to call for getEmailMessages() and convert this to form objects.
What do you recommend me ?
What is the best practice for this kind of problem ?
If by "dependencies" you mean "lazy-loaded objects", IMO it's best to get all the required data before hitting the view layer. In your current architecture it looks like that would mean a service method that retrieves the DTOs ("form beans"; I hesitate to use the term since it's easy to confuse with Struts 1).
Some say using an Open Session in View filter/interceptor is better. It's easier, but can lead to unintended consequences if the view developer isn't paying attention, including multiple N+1 queries etc.

ViewModel and Model in ASP.NET MVC 3 in regards to DDD - Advice

Going through the tutorial here: http://www.asp.net/mvc/tutorials/iteration-4-make-the-application-loosely-coupled-cs
I've noticed that they are passing the EF generated entity from the controller to the service layer. Should they be passing the viewmodel instead and then do the mapping in the service layer or is what they are doing correct?
I'm trying to understand the translation of the view model into the actual domain model passed from the service layer to the persistence layer.
Thanks
The general rule is that lower layers should have no knowledge about the upper levels.
This means that the service layer should have no knowledge of the view models (since they are an implementation detail in the user interface layer)

Resources