Skip to main content

Configuring spring mvc in 5 steps

Add dependency after creating a project - pom.xml and click to maven-update project

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

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

    <version>5.2.4.RELEASE</version>

</dependency>



full code ----

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.spring.mvc</groupId>

  <artifactId>springmvc</artifactId>

  <packaging>war</packaging>

  <version>0.0.1-SNAPSHOT</version>

  <name>springmvc Maven Webapp</name>

  <url>http://maven.apache.org</url>

  <dependencies>

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

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

    <version>5.2.4.RELEASE</version>

</dependency>

  

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

  </dependencies>

  <build>

    <finalName>springmvc</finalName>

  </build>

</project>



Step 1: Configure Dispatcher servlet in web.xml

<!DOCTYPE web-app PUBLIC

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

 "http://java.sun.com/dtd/web-app_2_3.dtd" >


<web-app>

  <display-name>Archetype Created Web Application</display-name>

 <!--  Configure  disptacher servlet -->

 

 <servlet>

  <servlet-name>spring</servlet-name>

  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

 </servlet>

 

 <servlet-mapping>

 <servlet-name>spring</servlet-name>

 <url-pattern>/</url-pattern>

 </servlet-mapping>

 

</web-app>


2. Create Spring Configuration file spring-servlet.xml

<?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:p="http://www.springframework.org/schema/p"

    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 

http://www.springframework.org/schema/context 

http://www.springframework.org/schema/context/spring-context-3.0.xsd">


 <context:component-scan base-package="springmvc.controller"></context:component-scan>

 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" name="viewResolver">

<property name="prefix" value="/WEB-INF/views/"/>

<property name="suffix" value=".jsp"/>


</bean>

</beans>


3. Configure view resolver

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" name="viewResolver">

<property name="prefix" value="/WEB-INF/views/"/>

<property name="suffix" value=".jsp"/>


</bean>


4.Create Controller


package springmvc.controller;


import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;


@Controller

public class HomeController {


@RequestMapping("/home")

public String home()

{

System.out.print("hei");

return "index";

}

@RequestMapping("/about")

public String about()

{

return "about";

}

}


5. Create index.jsp inside webapp/WEB-INF/views


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Home</title>

</head>

<body>

<h1>

Configured

</h1>

</body>

</html>





Comments

Popular posts from this blog

How to get the CPU temperature using ADB?

import os # Function to get the CPU temperature using ADB def get_cpu_temperature():     try:         # Run the ADB command and redirect the output to a temporary file         os.system("adb shell cat /sys/class/thermal/thermal_zone0/temp > temp.txt")         # Read the temperature from the temporary file and convert to Celsius         with open("temp.txt", "r") as file:             temperature = int(file.read().strip()) / 1000.0             return temperature     except Exception as e:         print("Error:", e)         return None     finally:         # Remove the temporary file         os.remove("temp.txt") # Function to check temperature and trigger alarm if necessary def check_temperature_and_notify(threshold):     temperat...

How to call API in angular & with an example of PHP API

How to call API in angular & with an example of PHP API . Software required:- Node ->  https://nodejs.org/en/download/ VS Code ->  https://code.visualstudio.com/download Angular CLI -> npm install - g @angular / cli To understand how to call API in angular follow the below steps:- Step:1 Project Setup 1. Create a project in angular ->  ng new project name 2. Install npm -> npm i 3. Import HttpClientModule in app.module.ts -> import { HttpClientModule } from '@angular/common/http' ; 4. Add provider providers : [ WebserviceService ], example -> ng new crudInAngularPHP Step:2 API Service 1. Create a service with name webservice -> ng g s services/webservice 2. import modules import { HttpClient, HttpHeaders, HttpRequest, HttpEvent } from '@angular/common/http'; 3 .    C all constructor  constructor( private http: HttpClient) 4. Add a common function to get a response of API with name postRequest:-      ...

How to import and read XML bulk data into angular versions 2, 5, 6, 8, 10

xml import in angular,how to read xml in angular,how to import xml in angular,angular xml example,angular with mukund,mukund programming tutorials,upload excel file in angular,angular 6/7/8 tutorial in hindi,xml import into angular,xml file upload,file upload,how to,xml file upload error,how to read xml in angular 8,xml import in angularjs,xml file upload exploit,file upload in html and javascript,angular tutorial in hindi