The relationship between spring-web and spring-webmvc - spring

I found #Controller and #GetMapping in spring-web, but only use these two annotations, not using DispatcherServlet how to receive requests, why these two annotations are not placed in spring-webmvc

Many use Spring Boot for building REST-APIs, and then consume those APIs from other backend services or JavaScript based web GUIs. In that case you want the Spring Web package to be able to create your controller's but don't need the MVC parts. That's why they have put the MVC parts in its own package.

Related

Spring MVC modules,subsystems and components

Let's say we have a Spring MVC project which is a banking web application. I am trying to identify the sub-systems ,modules and components of this project.
1.Subsystems - from what I have red the subsystems in MVC architecture are only 3- the Model,View and Controller is that correct?
2.Modules - are these a group of classes that do something particular as a group.For example LoginController.java,RegisterControler.java form a module let's call it Authentication
3.Components - for components I am not sure which they are in a Spring MVC project.
If someone can explain with examples in terms of a banking web application or other Spring MVC app it would be great!
Spring MVC follows the Model-View-Controller design pattern.
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.
Spring Boot Framework has mainly four major Components.
Spring Boot Starters.
Spring Boot AutoConfigurator.
Spring Boot CLI.
Spring Boot Actuator.
Please check out : https://www.journaldev.com/7989/key-components-and-internals-of-spring-boot-framework
In project-specific terms,
Module: It can be something that can be built separately in an application. ex: Login Module, Sign-up module, Transactions module, etc., You can say a module is a group of components.
Sub-systems: As far as I know, subsystems are the service package related stuff.
Components: Annotating a class with #Component tells Spring that it is available for fulfilling injections.
Spring Component annotation is used to denote a class as a Component. It means that the Spring framework will autodetect these classes for dependency injection when annotation-based configuration and classpath scanning is used

Spring vs JAX-RS

Here, several questions have been asked by many developers about difference between Spring-Rest and JAX-RS.
And, I have also learned that Spring is not following any specification and Spring framework has their own implementation then
Why Spring allows all that Annotations which are supported/used by JAX-RS by default?
Spring does not support JAX-RS annotations. If there is a situation where you think they do, then you are mistaken or it's just a coincidence. Period. If you will add any JAX-RS annotations in my Spring MVC program, nothing will happen. Annotations are just metadata. They are not programs. If Spring does not recognize the metadata, it will ignore it. But if you use a JAX-RS annotation in place of a Spring annotation that is used for the same purpose, respective of their framework, then you will not get the expected Spring behavior. So basically, if you are using Spring MVC, remove any JAX-RS dependencies so you don't mistakenly use them.

thymeleaf vs thymeleaf-spring4 dependency

Question is simple, what is the difference between the above dependencies? Does the first one enough for a springboot app or the second contains something special?
Artifact thymeleaf is the Core library.
Artifact thymeleaf-spring4 allows to integrate Thymeleaf with the Spring Framework, especially (but not only) Spring MVC. Btw there are several Thymeleaf integration packages for different Spring versions are available at the moment:
thymeleaf-spring3, thymeleaf-spring4, thymeleaf-spring5.
Information from thymeleaf-spring official documentation:
Thymeleaf offers a set of Spring integrations that allow you to use it as a fully-featured substitute for JSP in Spring MVC applications.
These integrations will allow you to:
Make the mapped methods in your Spring MVC #Controller objects forward to templates managed by Thymeleaf, exactly like you do with JSPs.
Use Spring Expression Language (Spring EL) instead of OGNL in your templates.
Create forms in your templates that are completely integrated with your form-backing beans and result bindings, including the use of property editors, conversion services and validation error handling.
Display internationalization messages from message files managed by Spring (through the usual MessageSource objects).
Resolve your templates using Spring’s own resource resolution mechanisms.
If you use Spring Boot, you can just use the spring-boot-starter-thymeleaf dependency. It already contains the above two dependencies as well as some others.

Spring Data Rest vs Spring Data Rest WebMvc

What is the difference between:
spring-data-rest
and
spring-data-rest-webmvc
To obviously they are two different things, but I am slightly confused here.
The spring-data-rest-webmvc is the project describing the main concepts of spring-data-rest which is the one of the main spring modules.
In most cases one will use spring-data-rest dependency for his/her project.
Use spring-data-rest when your app is based on Spring Boot
spring-data-rest-webmvc when your app is not a Spring Boot app
Ref: https://docs.spring.io/spring-data/rest/docs/current/reference/html/#install-chapter

Spring Boot with Jsf views [duplicate]

I am currently implementing a small Spring MVC PoC, and I would like to use JSF as the view technology since most people in my company are used to a J2EE with Primefaces environment.
Does Spring MVC 3 support JSF, or simply JSP? I have read multiple articles mixing the two.
My need is to create an appealing UI. Is there a simple way to do this using Spring MVC with the JSP as the view technology?
Our application uses schedules/calendars in multiples pages. It's basically a time management APP
You're making a conceptual mistake. JSF is not a view technology. JSF is a MVC framework. Exactly like as Spring MVC, albeit they have both a different ideology; JSF is component based MVC and Spring MVC is request based MVC. Thus they are full competitors. You cannot mix them. You should choose the one or the other. Instead, JSP and Facelets are true view technologies. Since Java EE 6 (December 2009), JSP is deprecated and replaced by Facelets (XHTML) as default view technology for JSF.
You can use Spring MVC with JSP view technology. You can also use Spring MVC with Facelets view technology (and many others). But you can not use Spring MVC with JSF components let alone with JSF component libraries like PrimeFaces. JSF output components may work, but JSF input components won't work at all. Spring MVC has already its own <form:xxx> tags for input. Even if you mix them, you will end up with half of the functionality from both frameworks in a mingled and confusing code base. This is not making any sense. If all you want is to use the same UI as PrimeFaces, just grab jQuery UI. It's also exactly what PrimeFaces is using under the covers. PrimeFaces is a jQuery-based JSF component library.
From the other side on, it can also be very good that you confused Spring IoC/DI with Spring MVC. Spring IoC/DI is in turn usable together with JSF. You can replace the JSF managed bean facility (#ManagedBean and friends) by Spring managed bean facility (#Component and friends), usually with the sole purpose in order to use #Autowired in a JSF backing bean. But that's it. The JSF MVC framework lifecycle, the JSF components and the view technology remain unchanged. The standard Java EE equivalent of that would be using CDI (and EJB).
The same story applies to Spring Security. You can use it together with JSF, you should however not follow Spring Security + Spring MVC targeted documentation/examples in order to configure it, but only Spring Security + JSF ones. Do note that Spring Security constraints on business actions only works when you replace the JSF managed bean facility by Spring managed bean facility. So that would still require a "Integrate Spring in JSF" as described in previous paragraph. The standard Java EE equivalent of this all would be using container managed security (JAAS/JASPIC) via <security-constraint> entries in web.xml.
The same story also applies to Spring WebFlow. You only also need to make sure that you're using most recent version of Spring WebFlow as older versions cause conflicts when used together with multiple JSF component libraries. Moreover, since JSF 2.2, new Faces Flows feature was introduced as part of standard Java EE API, hereby basically making Spring WebFlow superfluous.
Then there is Spring Boot. This does not have a direct equivalent in Java EE. Spring Boot basically enables you to execute a Java EE application using a plain Java application class with a main() method "in an easy and abstract way". Without Spring Boot it's surely possible (otherwise Spring Boot would never have existed), it's only a bit more work as to configuration as you have to take into account server-specific details based on its documentation. For example: Undertow and Jetty.
Coming back to JSF and Spring MVC, if really necessary, you can safely run Spring MVC and JSF next to each other in same web application, but they won't interoperate in server side. They will run completely independently. They will at most touch each other in the client side, if some JavaScript in a JSF-generated HTML page happens to invoke a Spring based REST web service call in the same web application. But that Spring web service would then not need/have to know anything about JSF in order to respond accordingly. The standard Java EE equivalent of that Spring REST webservice is JAX-RS.
The upcoming Java EE 8 will come with a new request based MVC framework, named just "MVC", based on lessons of both JSF and Spring MVC, hereby supplanting Spring MVC and providing a standard alternative to JSF.
See also:
What exactly is Java EE?
Difference between Request MVC and Component MVC
What are the main disadvantages of Java Server Faces 2.0?
What is the need of JSF, when UI can be achieved from CSS, HTML, JavaScript, jQuery?
When is it necessary or convenient to use Spring or EJB3 or all of them together?
Spring JSF integration: how to inject a Spring component/service in JSF managed bean?
Why Facelets is preferred over JSP as the view definition language from JSF2.0 onwards?
Spring MVC and JSF don't really mix. You can use JSF for the view related stuff and have Spring manage and wire the backed (services, daos etc.). But trying to match #Controllers with JSF pages isn't something that really works nicely (next to that both are different stacks request based against component-based).
To integrate Spring with JSF you will need to add the SpringBeanFacesELResolver to your faces-config.xml. That will lookup beans from Springs application context. For this to work you have to use plain JSF annotations and not the CDI based annotations.
Spring Webflow can be a help here. Check out this sample project.
https://github.com/spring-projects/spring-webflow-samples/tree/master/primefaces-showcase

Resources