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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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";
}
}
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"; | |
} | |
} |
pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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>
<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> |
web.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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>
<!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> | |
spring-servlet.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>
<?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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ 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>
withdraw.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ 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>
<%@ 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> | |
<%@ 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> | |
project 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