Servlets in spring mvc - spring

I have fundamental idea about servlet and spring mvc. But I don't know that there is a usage of servlets in spring mvc or not. In spring mvc we have controller classes. My thinking is servlet is used in spring mvc as a controller. If I'm incorrect please correct me.

Yes,you are perfectly right. Servlets are used in Spring-MVC. In Spring-MVC when you write annotation like #Controller, indirectly you are using a Servlet called Dispatcher Servlet. Dispatcher Servlet is defined in web.xml file with properties and class name which is mapped to .jsp pages and Controller part.

Related / Duplicated to When to use Servlet or #Controller. The question is not the same but quite the explanation on that question you will be able to understand:
If you're a student interested in learning the language then I would stick with servlets for now. It's possible to write a web app using just servlets but in practice you'll probably want to look at JSP's too.
A JSP is a convenient way to write a servlet that allows you to mix html with scripting elements (although it's recommended to avoid Java code in your jsp in favour of tags and el expressions). Under the covers it will be compiled as a servlet but it avoids you having to use lots of messy print statements.
It's important to have at least a basic understanding of servlets and JSP's. Spring MVC is one of many frameworks built on top of servlets to try make the task of writing a web application a bit easier. Basically all requests are mapped to the DispatcherServlet which acts as a front controller.
The DispatcherServlet will then call the controller whose annotations match the incoming request. This is neater than having to write these mappings yourself in the web.xml (although with servlet 3.0 you can annotate servlets now). But you also get many other benefits that can be used like mapping form fields to an object, validating that object with jsr303 annotations, map inputs and outputs to xml or json etc etc. Plus it's tightly integrated with core spring so you can easily wire in your services for the controller to call.
It's worth noting that there are a plethora of competing frameworks built on top of servlets. Spring MVC is one of the most popular so it's not a bad choice to look into.

Controllers are not Servlets! Controllers are normal Spring MVC beans that do not extend HttpServlet. Instead what Spring has is a custom extension of HttpServlet called DispacherServlet. Looking at the DispacherServlet's source code you can see that the class hierarchy goes: DispatcherServlet extends FrameworkServlet → FrameworkServlet extends HttpServletBean → HttpServletBean extends HttpServlet.
The DispatcherServlet, like any other Servlet, is declared in the web.xml. It handles all incoming HTTP requests. It is called a front controller which provides a single point of entry in your application. It is responsible for request handling by delegating requests to additional components of Spring MVC controllers which do not extend the HTTP Servlet API.
Look at the following diagram
In this picture DispacherServlet is the only HttpServlet. The Controllers, HandlerMapping and ViewResolver are all Spring MVC beans.

Related

Will multiple dispatcherservlet will share controller and other components in spring MVC?

Will multiple dispatcherservlet will share controller and other components in spring MVC
They are working by servlet specification which means that they won't share. It will just for different URI mappings dispatch by different servlets as it suppose to be for servlet. Those controllers should present in web application context provided for dispatcherServlet instance. You can reuse controllers from different contexts.
Dispatcher servlet should be just one as it implements front controller pattern. I don't understand why you need multiple instances in the first place.

what is the difference between xml files Spring and Spring MVC framework

I start to learn spring recently.
My goal is to use spring MVC to do restful api
I know spring MVC is web framework in spring
I know that in spring,there is beans.xml
And in spring MVC , there is servletname-servlet.xml
I want to know where is difference??
Is it means if I use spring MVC,I don't need to use beans.xml??
Please give me some way or give me example project link with spring and spring MVC together
The servletname-servlet.xml defines the beans for one servlet's app context. There can be number of servlets in a webapp and for every servlet we have servletname-servlet.xml (e.g. spring1-servlet.xml for servlet1, spring2-servlet.xml for servlet2).
Beans defined in servletname-servlet.xml can reference beans in beans.xml, but not vice versa.
All Spring MVC controllers must go in the servletname-servlet.xml context.
Beans.xml contain beans that are shared between all servlets in a webapp.Usually the beans.xml context is not necessary if you have only one servlet in your webapp.
You could define all your beans in servletname-servlet.xml but it's not a good practice.
Usually if you create a web application in 'pure' spring (ie. without spring MVC) then you will add ContextLoaderListener as a filter to your web.xml. Then spring will look for applicationContext.xml when you will usually import beans.xml.
In servletname-servlet.xml you define servlets. Servlets can refer other beans. So it's good practice to separate front (servlets) from backend (beans.xml).
Also remember that beans declared in servletname-servlet.xml are overriding the definitions of any beans defined with the same name in the global scope.
See also better answer at: ContextLoaderListener or not?

Integrating Spring request scope with JSF controllers

I am working on a project that uses a hybrid of JSF and Spring-MVC. User interface endpoints are accessed through a JSF front controller (javax.faces.webapp.FacesServlet), whereas REST service calls are accessed through a Spring-MVC front controller (org.springframework.web.servlet.DispatcherServlet). Deeper layers are Spring-managed (more-or-less). I don't like this arrangement, but I'm not in a position to change it.
The problem is that Spring's request-scoped beans aren't initialized when processing requests that come in through the JSF front controller. Is there an off-the-shelf solution for integrating Spring's WebApplicationContext with the JSF machinery so that the Spring request-scoped beans are initialized for each request regardless of whether that request comes through the JSF or Spring servlet?
There are two ways to integrate JSF with Spring, depending on which framework you want to give most control:
JSF front controller:
One way is to route all requests via the JSF faces servlet and let JSF route them to controllers, let JSF manage navigation state via faces-config. Then inject spring beans into JSF managed beans and access spring beans from the facelets view using value expressions via SpringBeanFacesELResolver.
See this post for a working example.
Spring front controller: put spring as a front controller with a dispatcher servlet and put in place spring webflow. This is the preferred and most powerful way to integrate the two frameworks, see this section of the documentation.
Spring webflow will manage the navigation state and the faces config file is mostly empty. There is no need for the JSF managed beans layer, request get directly handled by webflow.
Actions in JSF buttons trigger webflow transitions directly, and spring beans can also be used in value expressions to build the view. With this solution the integration with Spring is more seamless as webflow offers more possibilities then the JSF navigation mechanism: trigger bean methods between transitions, post redirect get pattern for avoiding double submissions.
Namelly the problem with the initialization of spring request scoped beans is solved with this direction, altough an alternative for this is to add a RequestContextListener OR a RequestContextFilter to web.xml (see section 3.4.4.1 of the docs).

Spring Controllers and Dispatcher Servlet behind the scenes

How does Dispatcher Servlet works internally?
I have seen that Controllers and it methods(defined by us) can be annotated with #RequestMapping and many other attributes. These controller has methods that have **varying signatures (Thay can take any parameter we like).
How does the Dispatcher servlet know how to pass these parameters in the methods defined by us? I mean how does Dispatcher Servletprepare these objects which we require in the controller methods?
Also what actually happens in WebApplicationContext behind the scenes?
would suggest to read section 15.2 this describes internal about spring mvc framework.

Displaying spring #controller provided rest services in a jsp file

I currently have a group of rest services provided by Spring 3.1.0 #Controller and wondered if there was any way that I could easily provide a list of the services (links to the restful services) in my index.jsp file. I know reflection is an option but thought spring may have a way to see the services provided by #Controller and allow me to display them.
Depending on the specific implementation of the HandlerMapping interface that your DispatcherServlet is using, you may be able to figure it out from there.
For example, if you are using the SimpleUrlHandlerMapping, there is a method present called getUrlMap(), which returns a map of the url path mappings.
What nicholas is pointing out is that there are different ways to maps requests. You've to check which HandlerMapping you're using exactly and then see how to get the mapping information it stores via class getters.
Here is an example for Spring MVC 3.1 #Controllers:
https://github.com/rstoyanchev/spring-mvc-31-demo/tree/master/src/main/java/org/springframework/samples/mvc31/endpointdoc

Resources