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

相关推荐
Re_zero2 小时前
线上日志被清空?这段仅10行的 IO 代码里竟然藏着3个毒瘤
java·后端
洋洋技术笔记2 小时前
Spring Boot条件注解详解
java·spring boot
程序员清风20 小时前
程序员兼职必看:靠谱软件外包平台挑选指南与避坑清单!
java·后端·面试
皮皮林55121 小时前
利用闲置 Mac 从零部署 OpenClaw 教程 !
java
NE_STOP1 天前
springMVC-HTTP消息转换器与文件上传、下载、异常处理
spring
华仔啊1 天前
挖到了 1 个 Java 小特性:var,用完就回不去了
java·后端
SimonKing1 天前
SpringBoot整合秘笈:让Mybatis用上Calcite,实现统一SQL查询
java·后端·程序员
日月云棠2 天前
各版本JDK对比:JDK 25 特性详解
java
用户8307196840822 天前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
JavaGuide2 天前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code