How to use Java to develop a Web application based on Spring MVC
How to use Java to develop a Web application based on Spring MVC
Overview
Spring MVC is a mature Java Web application framework based on MVC (model- View-Controller) pattern can simplify the web application development process. This article will introduce how to develop a simple web application using Java and Spring MVC, with specific code examples.
Step 1: Environment setup
First, we need to ensure that the following environment has been installed:
- Java development environment (JDK)
- Maven build tool
- An integrated development environment (IDE), such as Eclipse or IntelliJ IDEA
- Tomcat server (you can choose other servers, such as Jetty)
Step 2: Create a Maven project
Create a Maven project in the IDE, select the appropriate Java version and the skeleton of the Web project. This will automatically generate some necessary dependencies and basic configuration for you.
Step 3: Add Spring MVC dependencies
Edit the project's pom.xml
file and add Spring MVC dependencies. As shown below:
<dependencies> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9</version> </dependency> </dependencies>
Step 4: Configure Spring MVC
Create a web.xml
file in the root directory of the project and configure Spring MVC's DispatcherServlet. As shown below:
<web-app> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
This will hand over all incoming requests to DispatcherServlet
for processing.
Step 5: Create Controller
Create a Controller class in the project to process requests and return responses. For example, create a simple HelloController
class as follows:
@Controller public class HelloController { @RequestMapping("/") public String hello() { return "hello"; } }
In this example, the @Controller
annotation identifies this as a controller class, @RequestMapping
The annotation defines the URL path corresponding to this method.
Step 6: Create View
Create a views
directory under the WEB-INF
directory of the project, and create a hello.jsp in it
document. This will be the view used to display the user's return. For example, hello.jsp
may look like this:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html> <html> <head> <title>Hello World!</title> </head> <body> <h1 id="Hello-Spring-MVC">Hello Spring MVC!</h1> </body> </html>
Step 7: Configure the view resolver
Edit the project's applicationContext.xml
file and configure Spring View resolver for MVC. As shown below:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean>
This will tell Spring MVC to look for the view file in the /WEB-INF/views/
directory and add the .jsp
suffix.
Step 8: Deploy and run the application
Use Maven to package the project as a WAR file and deploy it to the Tomcat server. After starting Tomcat, visit http://localhost:8080/
to see the "Hello Spring MVC!" page.
Conclusion
By following the above steps, you can develop a simple web application using Java and Spring MVC. Of course, the above examples only introduce basic settings and usage. Spring MVC has many other features and advanced usage, which can be learned in depth through official documentation and other resources. I wish you success in Java web development!
The above is the detailed content of How to use Java to develop a Web application based on Spring MVC. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software