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!"。

相关推荐
深邃-1 分钟前
【Web安全】-基础环境安装:虚拟机安装,JDK环境安装(1)
java·开发语言·计算机网络·安全·web安全·网络安全·安全架构
小雅痞2 分钟前
[Java][Leetcode hard] 135. 分发糖果
java·算法·leetcode
q5431470874 分钟前
基于Spring Boot 3 + Spring Security6 + JWT + Redis实现登录、token身份认证
spring boot·redis·spring
jwt7939279378 分钟前
Spring之DataSource配置
java·后端·spring
逻辑驱动的ken16 分钟前
Java高频面试场景题07
java·开发语言·面试·职场和发展·求职招聘·春招
slarymusic21 分钟前
解决报错net.sf.jsqlparser.statement.select.SelectBody
java
callJJ23 分钟前
JVM 内存区域划分详解——从生活比喻到运行时数据区全景图
java·jvm·面试·内存区域划分
小江的记录本27 分钟前
【网络安全】《网络安全与数据安全核心知识体系》(包括数据脱敏、数据加密、隐私合规、等保2.0)
java·网络·后端·python·算法·安全·web安全
北漂Zachary27 分钟前
PHP vs Python vs Java:三大编程语言终极对比
java·python·php
Paxon Zhang29 分钟前
JavaEE初阶学习web开发的第一步**计算机组成原理,操作系统,进程(基础扫盲)**
java·后端·学习·java-ee