0% found this document useful (0 votes)
48 views

MVC Spring Application Setup:: Dependencies

The document provides instructions for setting up an MVC Spring application. It describes adding dependencies to the pom.xml file, configuring the web.xml file with a DispatcherServlet, and creating a SpringSample-servlet.xml configuration file. The DispatcherServlet is used to handle requests and the configuration file scans components and configures the view resolver.

Uploaded by

FuadNaserAl-deen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

MVC Spring Application Setup:: Dependencies

The document provides instructions for setting up an MVC Spring application. It describes adding dependencies to the pom.xml file, configuring the web.xml file with a DispatcherServlet, and creating a SpringSample-servlet.xml configuration file. The DispatcherServlet is used to handle requests and the configuration file scans components and configures the view resolver.

Uploaded by

FuadNaserAl-deen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Pom.

xml

Web.xml

SpringSample-servlet.xml

MVC Spring application setup:

 Create web dynamic project


 Add dependency: Configure  Convert to Maven project
o Group Id  the package , Artifact Id the name of the project
without spaces and the packaging is war.
And now we have the Pom.xml - dependency
Click on dependency on the pom.xml and add some dependency

 Search for Spring and add:


o Spring context
 Spring core
o Spring beans
o JSTL  I thing you can also copy the jstl jar to the
lib inside the WebContent
o Servlet api  we have it inside the tomcate folder so I
think it’s enough
o Jsp-api  again it’s inside the tomcate folder so I
think it’s enough
o And inside the web browser we would search for
 “spring web maven dependency”
 org.springframework  open it in another page.
 Spring web – the last version  Maven  copy the dependency
and paste it inside the pom.xml file exactly before the row
“</dependencies>” the last row..

Or just add this line

<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.2</version>
</dependency>
And we also need the spring web mvc

o And finally update the Jar file, it’s 1.7 and we want to
to 12. So in the pom.xml file we will add it before the

</properties> line.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>

 And in the web xml file exactly before the line </web-app>

We will write <servlet></servlet> and write inside it

o We well write servlet-name (auto writing serv..), and add the name of the servlet that we
want
o And the most important to add the DespatcherServlet that we will need to handle our
application.
o So in the Maven Dependencies  spring-webmvc-5.3.2.jar 
org.springframework.web.servlet  DispatcherServlet.class DispatcherServlet 
in this form right click and select Copy qualify name and paste it in <servlet-class> in
the web.xml

It’s will be like that..

<servlet>
<servlet-name>Spring Servlet</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

https://studyeasy.org/spring/spring-mvc-minimal/
If u don’t have the web.xml file then..
you can do it by Dynamic Web Project –> RightClick –> Java EE Tools –> Generate Deployment
Descriptor Stub.

The web.xml

<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
id="WebApp_ID" version="2.4">
<servlet>
<servlet-name>SpringSample</servlet-name>
<servlet-class>

org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringSample-
servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>SpringSample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
The SpringSample-servlet.xml file inside web-inf

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans    
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">
  
   <!-- Adding Support for Component Scan -->
   <context:component-scan base-package="org.studyeasy" />
  
   <!-- Configure View Resolver -->

https://studyeasy.org/spring/spring-mvc-minimal/

Create a maven project filter ison catalog internal


org.apache.maven.archetype maven-archetype-webapp

https://dzone.com/articles/spring-spring-boot-and-component-scan

Any class that is annotated with @Configuration is also a Spring


bean. That means the MainDirver is also a spring bean that will be
created during creating AnnotationConfigApplicationContext.

And after the MainDirver bean is created , Spring will then inject
other beans into its field if that field is annotated with @Autowird.
So in this case , Environment , ApplicationContext, and
ConfigurableEnvironment are all injected this MainDirver bean.

P.S. You can think that Environment , ApplicationContext, and


ConfigurableEnvironment are kind of Spring infrastructure beans that
must be created even though you do not define them using
@Configuration , @Service , @Bean and etc.
one way to manually instantiate a container:

AnnotationConfigApplicationContext context =
 new
AnnotationConfigApplicationContext(AppContext.class);
//this is reference to Bean factory,
//Bean => java object created by spring.
 //Bean factory is the place were spring keeps all
the object it creates.

@Autowired//in order to allow spring to create a object


//when ever there is a requerment for this object.
This annotation make a default constructor

@Component // as a result spring can create


//an object of this class

The @Configuration annotation indicates that the class is a source of bean definitions. Also, we can
add it to multiple configuration classes.

The @Autowired annotation is a great way of making the need to inject a dependency in
Spring explicit. And although it's useful, there are use cases for which this annotation alone
isn't enough for Spring to understand which bean to inject.

By default, Spring resolves autowired entries by type.

If more than one bean of the same type is available in the container, the framework will
throw NoUniqueBeanDefinitionException, indicating that more than one bean is available
for autowiring.

If we try to load FooService into our context, the Spring framework will throw a
NoUniqueBeanDefinitionException. This is because Spring doesn't know which bean to inject. To
avoid this problem, there are several solutions. The @Qualifier annotation is one of them.

By using the @Qualifier annotation, we can eliminate the issue of which bean needs to be injected.
How it work in few word..

1. App.java  by taking help with App.config.java  we creating


context.
2. Using this context we can generate the bean (getBean) in the code.
Bean is just a java object like person/car/..

3. AppConfig.java  @ComponentScan(“org.studyeasy”)

It’s scan the package and telling Spring where to search.

4. @Configuration  this say that this class is the Configuration class.


5. Then we have corolla class and swift class, this class is just component , and they have the
6. @ autowired  if the spring bean has only one constructor the autowired annotation can be
omitted and spring will use that constructor and inject all the necessary dependencies.
7.
8. @qualifier  if there is more than object , so this is to specific which one to work.
9. Like if there is interface Engine and V6 implements Engine and V8 implements Engine.
10. @RequestBody annotation allows us to retrieve the request body and automatically
convert it to java Object
11.@RestController is a specialized version of the controller. It includes
the @Controller and @ResponseBody annotations, and as a result,
simplifies the controller implementation:
12. @RestController
13. @RequestMapping("books-rest")
14. public class SimpleBookRestController {
15.
16. @GetMapping("/{id}", produces = "application/json")
17. public Book getBook(@PathVariable int id) {
18. return findBookById(id);
19. }
20.
21. private Book findBookById(int id) {
22. // ...
23. }

@Controller @RequestMapping("books") public class SimpleBookController { @GetMapping("/{id}",


produces = "application/json")

public @ResponseBody Book getBook(@PathVariable int id) { return findBookById(id); }

https://docs.spring.io/spring-framework/docs/5.0.0.RC2/spring-framework-
reference/web.html#spring-web
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

private long id;

this variable in the entity class @


@Id annotation specifies the primary key of the entity

@GeneratedValue annotation specifies the generation strategies for the values of primary
keys.

@Column annotation is for the name of the db table for this variable .

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

Spring Data JPA (JpaRepository<User, Long> )interenally provides


@Repository annotation so we no need to add @Repository annotation for
this class , we can remove the annotation her,

transaction  -annotation

A database transaction (DB transaction) is a unit of work that is either completed as


a unit or undone as a unit. Proper database transaction processing is critical to
maintaining the integrity of your databases.
Suppose you are entering new customer records into your database and are entering
the 99th customer record. If your machine goes down, are the first 98 records you
entered lost? No, because WebSpeed:

 Keeps the first 98 records in the database


 Discards the partial 99th record
This is just one simple scenario. Suppose the procedure was updating multiple
tables. You want to make sure that WebSpeed saves any completed changes and
discards partial changes in all tables.
5. ModelAndView
The final interface to pass values to a view is the ModelAndView.

This interface allows us to pass all the information required by Spring MVC in one return:

@getMapping == handling only the get request

Spring form elements

<%@ taglib prefix="form"


uri="http://www.springframework.org/tags/form" %>

Spring IDE  plugin of eclipse  to configure XML files under


Eclipse.
And the support of Spring IDE is ended on version mars 4.5 so we can not
added it directly. Cuz we use version 4.17.
Help eclipseMarkeetplace  springspring ide 3 .

If you don’t have web.xml

Dynamic Web Project –> RightClick –> Java EE Tools –> Generate Deployment Descriptor Stub.
Take it as rule: if the content is static does not change keep it in the view file, if it is dynamic
content  controller file and pass the information to the view file

You might also like