Home Java javaTutorial 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

Sep 20, 2023 pm 01:36 PM
web application java development spring mvc

如何使用Java开发一个基于Spring MVC的Web应用

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:

  1. Java development environment (JDK)
  2. Maven build tool
  3. An integrated development environment (IDE), such as Eclipse or IntelliJ IDEA
  4. 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>
Copy after login

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>
Copy after login

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";
    }
}
Copy after login

In this example, the @Controller annotation identifies this as a controller class, @RequestMappingThe 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>Hello Spring MVC!</h1>
</body>
</html>
Copy after login

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>
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the five options for choosing the Java career path that best suits you? What are the five options for choosing the Java career path that best suits you? Jan 30, 2024 am 10:35 AM

There are five employment directions in the Java industry, which one is suitable for you? Java, as a programming language widely used in the field of software development, has always been popular. Due to its strong cross-platform nature and rich development framework, Java developers have a wide range of employment opportunities in various industries. In the Java industry, there are five main employment directions, including JavaWeb development, mobile application development, big data development, embedded development and cloud computing development. Each direction has its characteristics and advantages. The five directions will be discussed below.

Master the key concepts of Spring MVC: Understand these important features Master the key concepts of Spring MVC: Understand these important features Dec 29, 2023 am 09:14 AM

Understand the key features of SpringMVC: To master these important concepts, specific code examples are required. SpringMVC is a Java-based web application development framework that helps developers build flexible and scalable structures through the Model-View-Controller (MVC) architectural pattern. web application. Understanding and mastering the key features of SpringMVC will enable us to develop and manage our web applications more efficiently. This article will introduce some important concepts of SpringMVC

Essential for Java development: Recommend the most efficient decompilation tool Essential for Java development: Recommend the most efficient decompilation tool Jan 09, 2024 pm 07:34 PM

Essential for Java developers: Recommend the best decompilation tool, specific code examples are required Introduction: During the Java development process, we often encounter situations where we need to decompile existing Java classes. Decompilation can help us understand and learn other people's code, or make repairs and optimizations. This article will recommend several of the best Java decompilation tools and provide some specific code examples to help readers better learn and use these tools. 1. JD-GUIJD-GUI is a very popular open source

Java development skills revealed: implementing data encryption and decryption functions Java development skills revealed: implementing data encryption and decryption functions Nov 20, 2023 pm 05:00 PM

Java development skills revealed: Implementing data encryption and decryption functions In the current information age, data security has become a very important issue. In order to protect the security of sensitive data, many applications use encryption algorithms to encrypt the data. As a very popular programming language, Java also provides a rich library of encryption technologies and tools. This article will reveal some techniques for implementing data encryption and decryption functions in Java development to help developers better protect data security. 1. Selection of data encryption algorithm Java supports many

JAX-RS vs. Spring MVC: A battle between RESTful giants JAX-RS vs. Spring MVC: A battle between RESTful giants Feb 29, 2024 pm 05:16 PM

Introduction RESTful APIs have become an integral part of modern WEB applications. They provide a standardized approach to creating and using Web services, thereby improving portability, scalability, and ease of use. In the Java ecosystem, JAX-RS and springmvc are the two most popular frameworks for building RESTful APIs. This article will take an in-depth look at both frameworks, comparing their features, advantages, and disadvantages to help you make an informed decision. JAX-RS: JAX-RSAPI JAX-RS (JavaAPI for RESTful Web Services) is a standard JAX-RSAPI developed by JavaEE for developing REST

The role of the controller package in java The role of the controller package in java May 07, 2024 am 02:45 AM

In the Spring MVC architecture, the Controller package implements business logic by processing user requests and returning responses. Its responsibilities include: receiving user requests (usually via HTTP). Validate and process request parameters. Call the appropriate business logic (usually the service layer). Render the view and return it to the user (usually HTML, JSON, or XML).

Spring Security permission control framework usage guide Spring Security permission control framework usage guide Feb 18, 2024 pm 05:00 PM

In back-end management systems, access permission control is usually required to limit different users' ability to access interfaces. If a user lacks specific permissions, he or she cannot access certain interfaces. This article will use the waynboot-mall project as an example to introduce how common back-end management systems introduce the permission control framework SpringSecurity. The outline is as follows: waynboot-mall project address: https://github.com/wayn111/waynboot-mall 1. What is SpringSecurity? SpringSecurity is an open source project based on the Spring framework, aiming to provide powerful and flexible security for Java applications.

What is the difference between the architecture of the Spring WebFlux framework and traditional Spring MVC? What is the difference between the architecture of the Spring WebFlux framework and traditional Spring MVC? Apr 17, 2024 pm 02:36 PM

The key difference between SpringWebFlux and SpringMVC is reactive programming (asynchronous processing) and blocking I/O model. This difference leads to key architectural differences: asynchronous processing and event loop models; handlers based on functional interfaces; asynchronous response streams (Publisher objects); simplified exception handling mechanisms; higher throughput and scalability.

See all articles