You are here: Home / Topics / Setup Spring MVC (Dynamic web project) in Eclipse using xml file from scratch

Setup Spring MVC (Dynamic web project) in Eclipse using xml file from scratch

Filed under: Spring on 2023-08-27 17:22:40

Hello guys!!!!!! Welcome to MCQ Buddy Topics.

In this topic, I am going to setup spring mvc (Dynamic web Project) in my eclipse. You should have all spring dependencies with you. I will provide a link here to find all the dependencies of spring along with the spring java classes. I struggled a lot to find these files.

 

File No 1 : Spring all jar files

File No 2: Spring framework all classes folder wise


How did i started...

Step 1: Download spring jars in your laptop. 
2- download spring framework main (which contains all the classes)

step - 3 : Open eclipse and create new web dynamic project.


in SRC/WEB-INF/LIB folder copy paste all jars of spring.

Now in web.xml file >>>>>>>>>>>>>>>>>

Create on dispatcher servlet (Main controller) which will take responsibility of doing all the routing...
suppose User is trying to find register page then the request will first come to the Main controller (Dispatcher controller) he has all the components and their requestMapping. He knows where he has to send this request.

 

 

How to setup (Dispatcher Controller)


in web.xml

<servlet>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<servlet-name>mainController</servlet-name>
<load-on-startup>1</load-on-startup>

</servlet>

 

Tips: To find the class in eclipse press Ctrl+Shift+T to open Search Type (Type DispatcherServlet in the input box) If you have paste all the jar files in the lib folder you will see DispatcherServlet class. Click on this class, You may see a warning to attach source file. 

Now you have to click on external > external folder and locate the folder where you have downloaded your all spring frames classes. You will see the code of this class after this. Right click on the DispatcherServlet and copy qualifying name (org.springframework.web.servlet.DispatcherServlet)

That's all how to find the qualifying name from our jar files classes.

When you will write this code in your web.xml file and run the project on tomcat server.

The server will load this servlet on startup (As we have mentioned in our configuration) If we do not write <load-on-startup>1</load-on-startup> the server will configure our mainController when the first request is made on our server.


Now our server will automatically find one file in our SRC/WEB-INF/mainController-servlet.xml (In this file all the configuration of our project will be written, as bean, viewResolver properties etc.)

So we have to create a xml file in our WEB-INF folder....... the name of this xml file should be the same as <servlet-name> with -servlet.xml appended.

This file contains basic beans.xml configuration as we have seen earlier when we were setting up our first project in IntelliJ Idea .

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-4.1.xsd">
   <!-- bean definitions here -->

 

</beans>

So in this file we will write

<context:component-scan base-package="first.controllers"/>


After writing this, our mainController will have all the beans which are annotated with @Controller or @Component annotations.


In this project we have three java classes. Vegetables, Shop, Football. These classes have @Controller annotation. each class has a method which is mapped by url and each method is return some string...

Have a look on the code now.

Vegetable.java

package first.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class Vegetables {


@ResponseBody
@RequestMapping("/veg")
public String getVeg() {
 return "Take your vegetables";
}
}

 


Shop.java


package first.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class Shop {

@ResponseBody
@RequestMapping("/shop")
public String visitShop() {
 return "Thank you for visiting our shop";
}
}


Football.java


package first.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class Football {

@ResponseBody
@RequestMapping("/football")
public String getFootball() {
 return "Take this football";
}
}


These all classes have a method and that method is mapped by some url. Our mainController knows everything about these classes because we have written <context:component-scan base-package="first.controllers">


These all classes are inside our first.controllers package.

Now run this project on tomcat server.

you will see a 404 error 
Url will http://localhost:8080/first/

This first is the name of our project but our mainControllers name is also first so we have write the name of our mainController after this.

http://localhost:8080/first/first/shop


When you write shop after our mainController name, you will see the output in the browser as THANK YOU FOR VISITING SHOP.

Congratulations, you have setup your first spring mvc (dynamic web project) with the help of xml based configuration..

 

A lot more things are also coming in this project............
in next topic, we will learn how to map views with the controller method.

 

Having doubts feel free to ask me in comment section.
 

About Author:
M
Mr. Dubey     View Profile
Founder and CEO of MCQ Buddy. I just like to help others.