JavaWeb简单开发

JavaWeb 开发是指基于 Java 技术栈进行 Web 应用开发的过程,主要依赖于 Java EE 或者 Spring 框架来构建服务器端应用。JavaWeb 的技术栈比较广泛,通常包括以下几个部分:

示例:简单的 JavaWeb 应用(Spring Boot + Thymeleaf)

假设我们创建一个简单的 JavaWeb 应用,使用 Spring Boot 和 Thymeleaf 来显示一个欢迎页面。

1. 创建 Spring Boot 项目

首先创建一个 Spring Boot 项目,并添加必要的依赖:

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency> <dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

</dependencies>

2. 创建 Controller

在 Spring Boot 中,Controller 类用于接收请求并返回视图:

@Controller

public class HelloController

{

@GetMapping("/hello")

public String hello(Model model) {

model.addAttribute("message", "Hello, Welcome to JavaWeb!");

return "hello"; // 返回 hello.html 模板

}

}

3. 创建 Thymeleaf 模板

src/main/resources/templates/ 目录下创建 hello.html 文件:

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

<head>

<title>Welcome Page</title>

</head> <body>

<h1 th:text="${message}"></h1>

</body>

</html>

4. 运行应用

启动 Spring Boot 应用后,访问 http://localhost:8080/hello,即可看到页面显示 "Hello, Welcome to JavaWeb!"。

相关推荐
小雅痞几秒前
[Java][Leetcode middle] 73. 矩阵置零
java·leetcode·矩阵
funnycoffee12312 分钟前
cisco Firepower 4110 9300 FXOS set chassis hostname
java·服务器·数据库
Ting-yu13 分钟前
SpringCloud快速入门(2)---- SpringCloud简介
后端·spring·spring cloud
Gofarlic_OMS14 分钟前
CONVERGE CFD许可不够用?自动回收闲置,燃烧仿真随时跑
java·大数据·开发语言·架构·制造
重生之我是Java开发战士18 分钟前
【Java SE】多线程(二):线程安全、synchronized、volatile与wait/notify详解
java·开发语言·安全
赏金术士26 分钟前
Kotlin 习题集 · 进阶篇
java·数据库·kotlin
ch.ju30 分钟前
Java程序设计(第3版)第三章——数组的遍历
java·开发语言
何故染尘優31 分钟前
面试八股文-01
java·jvm·面试
XiYang-DING35 分钟前
【Java EE】Cookie
服务器·前端·java-ee