I am very confused about spring controllers,
pls help me.
i want to know that how spring controller initate and how its constructor get into action,
as when i call a controller from one page, and then ask the same controller from another page it dosn't seems to create a new object of that controller rather than provide same reference of the controller object
Controllers are "application-scoped"; they are instantiated just once and then reused for all requests you associate them with. As such, it is important that they do not carry session-specific state.
You should refer to the documentation for more details on how controllers work and how to instantiate them.
Related
I'm currently trying to make a website, where I have Item models, and User models.
I want to use the item controller to display information about a user and item, (which is stored in a mysql databse) on the same html page.
How would i go about this, without having to also inject the userService class into the itemService class?
There is nothing stopping you from creating a service method that passes back a DTO containing both a User and an Item. You can inject multiple repositories into your service and retrieve both entities within the same transaction.
You could also provide separate methods in your service, one to get the user and the other to get the item, but in that case you would have two separate transactions.
Thanks for the replies guys, it helped me out a lot.
I was mainly just confused about the role of service classes I guess.
Now I'm using multiple repositories for my service class, which then sends a DTO back to the controller, with all the data it needs.
I have been working both on spring and grails (grails is built on spring).
We can pass our data from controller to view (jsp/gsp) via request object or via model.
Passing data via model is preferred because its easy.
Comparison of code :
Spring :
controller request.setAttribute("name","india");
view ${pageContext.request.getAttribute('name')}
controller ModelAndView("viewname",modelObject)
Grails
controller request.setAttribute("name","india")
view ${request.getAttribute("name")}
controller render(view:"viewName",model:[name:"india"])
I think request object does many functionalities other than this.
So my question is : If passing data from controller to view can be done with request object itself why java technology required to make a concept like model ?
necessity is mother of invention
I mean to say that there must be a task that can be implemented only via a model.
A few things come to mind, but they are speculation as to why.
Mainly, separation of concerns. Modifying the request, which is input, to hold output seems to be abusing this. The model is clearly ouput, and only output.
By clearly defining what constitutes output, using a model, frameworks such as Spring and Grails can then apply other processes to only the model. Grails filters come to mind, as an example.
That's my quick opinion.
That data which a controller gathers up and needs to make available to the view should be put in the model. That is kind of the definition of what the model is in this context. Never use request attributes for this.
Is there a way for me to do this and is it appropriate? I need the data that comes back from an endpoint in a different controller. Would I create a HTTP request on right in the java code?
Please advise if this is an appropriate question for here...
You might want to consider separating the retrieval of the data and the presentation of the data from your current endpoint in two different classes. After you've done that you can easily have two Spring controllers share the same source of data. One of them is your current Spring controller, the other one is the new one.
Performing an HTTP request from one Spring controller to another one in the same application would introduce a lot of overhead and complexity. I would try to avoid that.
The correct thing to do is to call the service that is generating the data for the controller in the controller that needs the data.
I hope that you've structured your project as an MVC. If not, you should be addressing that before you do anything else.
In a controller I can get model by declaring Model/ModelMap typed parameter. In handler interceptors and in view rendering I get the same. But how do I get the model from the arbitrary class, which can get hold of ApplicationContext or web request? I just don't want to use model.addAttribute() all the time and would like my service automatically put objects into model.
The idea is that you shouldn't. Your controllers are the entry point of your application, so you get them there, and pass them as arguments to the layers below.
Say a logged in user hits the url:
www.example.com/forum/234
Before the spring mvc action fires, I want to load the User Object, the user's permission, the Forum object.
Now I want to share these objects accross this request. So other classes can look to see, in the current request, for a User, Permission and Forum object.
Potentially it would be cool if a custom freemarker module could also reference these objects if they are available.
is this possible?
First, consider using spring-security, whose filters do everything you need.
If you want to do all by hand, then you have at least two options:
- use servlet filters
- use spring handler interceptor (http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-handlermapping-interceptor).
In both cases store this data in request attributes.
Another option is to create bean with request scope which will store your data.
As for Freemarker, you must provide own subclass of FreemarkerViewResolver, which will return subclass of FreeMarkerView in requiredViewClass() method. Add your objects in exposeHelpers() method in this FreeMarkerView subclass.