Spring MVC program - Smart Tech Guides

Latest

Hi this is Sravan Kumar from India. SMART TECH GUIDES is a technical blog. it helps you to learn about Java topics, frameworks and tools like Jenkins, GitHub & related explanations, data base related topics. All these Explanations are explained in simple and understandable manner.

Subscribe Us

SMART TECH GUIDES

Spring MVC program

Steps involved in Spring web MVC application development.

1) create new maven project, select maven-archetype-web. 

2) add web server(tomcat). 

3) add spring-webmvc and tomcat dependency in pom.xml. 

4) create beans configured Java/xml file. 

5) write business logic inside src/main/java.



Java Program
package com.smarttechguides;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Bank {
@RequestMapping("/deposite")
public String depositeAmount() {
// write business logic for deposite amount
return "deposite";
}
@RequestMapping("/withdrawn")
public String withdrawAmount() {
// write business logic for withdrawn amount
return "withdraw";
}
}
view raw Bank.java hosted with ❤ by GitHub
 


pom.xml
<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.smarttechguides</groupId>
<artifactId>02-sample</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>02-sample Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>9.0.58</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>02-sample</finalName>
</build>
</project>
view raw pom.xml hosted with ❤ by GitHub
 


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>
<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>
view raw gistfile1.txt hosted with ❤ by GitHub
 


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:context="http://www.springframework.org/schema/context"
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 ">
<context:component-scan
base-package="com.smarttechguides"></context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver" name="view">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
 

 

deposite.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<body>
<h2>amount deposited statement..!!</h2>
</body>
</html>
view raw deposite.jsp hosted with ❤ by GitHub
withdraw.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<body>
<h2>amount withdraw statement..!!</h2>
</body>
</html>
view raw withdraw.jsp hosted with ❤ by GitHub


project Structure

mvc folder structure

Note: spring beans we can configure in 3 ways

1) xml based configuration file

 Configure <beans> inside beans.xml file

 

2) annotation-based configuration

Configure <context:annotation-config/> inside beans.xml file and use annotations inside Java class.

 

3) Java based configuration

Configurations happen in Java file without xml file.

with the help of annotations like @configuration, @beans, @component, @service, @controller..etc.!


No comments:

Post a Comment