Skip to content

Commit 08a498a

Browse files
author
kmsoln
committed
[Master] HUGE UPDATE
1 parent 04cdaf0 commit 08a498a

20 files changed

Lines changed: 646 additions & 15 deletions

‎README-CN.md‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[English](README-EN.md) | 中文 | [Русский](README.md)
2+
13
## 使用Java进行Web开发的教程
24

35
欢迎使用Spring框架进行Java Web开发的教程!这些结构化的教程旨在提供全面的指导,涵盖Java Web开发的各个方面,侧重于Spring框架。每个教程都精心设计,重点介绍特定的概念和方法,确保对Web开发原理的牢固理解。
@@ -24,6 +26,3 @@
2426

2527
JMeter系列教程提供了使用JMeter进行负载测试和性能分析的完整指南。参与者将学习如何设置JMeter、记录HTTP请求、创建和执行测试场景,以及分析测试结果。
2628

27-
---
28-
29-
translate to Chinese language

‎README-EN.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
English | [中文](README-CN.md) | [Русский](README.md)
2+
13
## Java Web Development Tutorials
24

35
Welcome to the tutorials on Java web development utilizing the Spring framework! These structured tutorials aim to provide comprehensive guidance on various aspects of Java web development through the lens of the Spring framework. Each tutorial is meticulously designed to focus on specific concepts and methodologies, facilitating a robust understanding of web development principles.

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[English](README-EN.md) | [中文](README-CN.md) | Русский
2+
13
## Учебные пособия по веб-разработке на Java
24

35
Добро пожаловать на учебные пособия по веб-разработке на Java с использованием фреймворка Spring! Эти структурированные учебные пособия направлены на предоставление всестороннего руководства по различным аспектам веб-разработки на Java через призму фреймворка Spring. Каждое учебное пособие тщательно разработано для акцентирования на конкретных концепциях и методологиях, обеспечивая крепкое понимание принципов веб-разработки.

‎docs/cn/jpa/practice/dependencies/jpa.md‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# JPA:设置依赖项
22

3-
## 简介
3+
## 介绍
44

5-
本指南专注于为具有JPA(Java Persistence API)的Spring Boot项目设置依赖项。它涵盖了包括Spring Boot Starter JDBC和Spring Boot Starter Data JPA依赖项的必要步骤。
5+
本指南重点介绍了如何为具有JPA(Java持久性API)的Spring Boot项目设置依赖项。它涵盖了包含Spring Boot Starter JDBC和Spring Boot Starter Data JPA依赖项所需的必要步骤。
66

77
## 目标
88

9-
本指南的目标是提供清晰的说明,以便设置Spring Boot和JPA的必要依赖项,特别关注Spring Boot Starter JDBC和Spring Boot Starter Data JPA依赖项。
9+
本指南的目标是为Spring Boot和JPA设置必要依赖项,具体重点放在Spring Boot Starter JDBC和Spring Boot Starter Data JPA依赖项上,并提供清晰的说明。
1010

1111
## 设置依赖项
1212

@@ -29,11 +29,11 @@ implementation "org.springframework.boot:spring-boot-starter-jdbc"
2929
</dependency>
3030
```
3131

32-
此依赖项提供了基于JDBC的数据访问的自动配置。
32+
此依赖项为基于JDBC的数据访问提供了自动配置。
3333

3434
### Spring Boot Starter Data JPA
3535

36-
要在Spring Boot应用程序中包含对JPA(Java Persistence API)的支持:
36+
要在Spring Boot应用程序中包含对JPA(Java持久性API)的支持:
3737

3838
#### Gradle
3939

@@ -50,8 +50,20 @@ implementation "org.springframework.boot:spring-boot-starter-data-jpa"
5050
</dependency>
5151
```
5252

53-
此依赖项简化了JPA存储库的设置,并为Hibernate提供了默认配置。
53+
此依赖项简化了设置基于JPA的存储库,并为Hibernate提供了默认配置。
54+
55+
### 为Spring WebMVC进行附加配置
56+
57+
如果您使用Spring WebMVC,则需要添加额外的配置来设置与持久性相关的bean和属性。
58+
59+
#### 创建持久性配置类
60+
61+
在`com.example.config`包中创建一个名为`PersistenceConfig`的新类:
62+
63+
```java
64+
// 这里是java代码,因此不需要翻译
65+
```
5466

5567
---
5668

57-
# [下一步:实体创建](../creation/create-entity.md)
69+
# [下一步:创建实体](../creation/create-entity.md)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# 客户端授权认证用户(客户端)
2+
3+
这项任务概述了如何使用Thymeleaf在客户端视图中根据用户的认证状态授权用户。通过根据用户是否已经认证来显示或隐藏元素,您可以控制特定功能或操作的可见性。
4+
5+
## 目标
6+
7+
在Thymeleaf视图中根据用户的认证状态控制与添加和删除学生相关的元素的可见性。
8+
9+
## 步骤
10+
11+
1. **更新Thymeleaf模板:**
12+
13+
修改Thymeleaf模板以根据用户的认证状态有条件地显示元素。
14+
15+
```html
16+
<!DOCTYPE html>
17+
<html xmlns:th="http://www.thymeleaf.org">
18+
<head>
19+
<title>学生管理</title>
20+
</head>
21+
<body>
22+
<h1>学生管理</h1>
23+
24+
<!-- 添加学生按钮 -->
25+
<div th:if="${#authorization.expression('isAuthenticated()')}">
26+
<a href="/addStudent" class="btn btn-primary">添加学生</a>
27+
</div>
28+
29+
<!-- 删除学生按钮 -->
30+
<div th:if="${#authorization.expression('isAuthenticated()')}">
31+
<a href="/removeStudent" class="btn btn-danger">删除学生</a>
32+
</div>
33+
34+
<!-- 其他内容 -->
35+
<div>
36+
<p>欢迎使用学生管理系统。在这里您可以管理学生记录。</p>
37+
</div>
38+
</body>
39+
</html>
40+
```
41+
42+
在此模板中:
43+
- 使用`th:if="${#authorization.expression('isAuthenticated()')}"`语句仅在用户已认证时条件显示“添加学生”和“删除学生”按钮。
44+
45+
## 解释
46+
47+
- **Thymeleaf安全方言**:`#authorization`对象是Thymeleaf安全方言的一部分,它将Spring Security与Thymeleaf集成在一起。
48+
- **条件显示**:使用`th:if`属性根据用户的认证状态有条件地显示或隐藏元素。
49+
50+
通过按照这些步骤,您可以根据用户是否已认证来控制Thymeleaf视图中元素的可见性,确保只有经过认证的用户才能看到和与特定功能进行交互。
51+
52+
---
53+
54+
# [下一个任务:按角色授权](authorize-client-role.md)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# 根据权限授权用户(客户端)
2+
3+
本任务概述了如何使用Thymeleaf在客户端视图中根据用户的权限授权用户。通过根据用户权限显示或隐藏元素,您可以控制特定功能或操作的可见性。
4+
5+
## 目标
6+
7+
在Thymeleaf视图中根据用户的权限控制与添加和删除学生相关的元素的可见性。
8+
9+
## 步骤
10+
11+
1. **更新Thymeleaf模板:**
12+
13+
修改Thymeleaf模板以根据用户的权限有条件地显示元素。
14+
15+
```html
16+
<!DOCTYPE html>
17+
<html xmlns:th="http://www.thymeleaf.org">
18+
<head>
19+
<title>学生管理</title>
20+
</head>
21+
<body>
22+
<h1>学生管理</h1>
23+
24+
<!-- 添加学生按钮 -->
25+
<div th:if="${#authorization.expression('hasAuthority(''WRITE'')')}">
26+
<a href="/addStudent" class="btn btn-primary">添加学生</a>
27+
</div>
28+
29+
<!-- 删除学生按钮 -->
30+
<div th:if="${#authorization.expression('hasAuthority(''DELETE'')}">
31+
<a href="/removeStudent" class="btn btn-danger">删除学生</a>
32+
</div>
33+
34+
<!-- 其他内容 -->
35+
<div>
36+
<p>欢迎使用学生管理系统。在这里您可以管理学生记录。</p>
37+
</div>
38+
</body>
39+
</html>
40+
```
41+
42+
在此模板中:
43+
- 使用`th:if="${#authorization.expression('hasAuthority(''WRITE'')')}"`语句仅在用户具有`WRITE`权限时条件显示“添加学生”按钮。
44+
- 同样,使用`th:if="${#authorization.expression('hasAuthority(''DELETE'')')}"`语句仅在用户具有`DELETE`权限时条件显示“删除学生”按钮。
45+
46+
## 解释
47+
48+
- **Thymeleaf安全方言**:`#authorization`对象是Thymeleaf安全方言的一部分,它将Spring Security与Thymeleaf集成在一起。
49+
- **条件显示**:使用`th:if`属性根据用户的权限有条件地显示或隐藏元素。
50+
51+
通过按照这些步骤,您可以根据用户的权限控制Thymeleaf视图中元素的可见性,确保只有授权用户才能看到和与特定功能进行交互。
52+
53+
---
54+
55+
# [NEXT TASK: Lab Tasks](../lab-work.md)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# 基于角色授权用户(客户端)
2+
3+
本任务概述了如何在客户端视图中根据用户的角色对用户进行授权,使用Thymeleaf来实现。通过根据用户角色显示或隐藏元素,您可以控制特定功能或操作的可见性。
4+
5+
## 目标
6+
7+
在Thymeleaf视图中根据用户的角色控制与添加和删除学生相关的元素的可见性。
8+
9+
## 步骤
10+
11+
1. **更新Thymeleaf模板:**
12+
13+
修改Thymeleaf模板以根据用户的角色有条件地显示元素。
14+
15+
```html
16+
<!DOCTYPE html>
17+
<html xmlns:th="http://www.thymeleaf.org">
18+
<head>
19+
<title>学生管理</title>
20+
</head>
21+
<body>
22+
<h1>学生管理</h1>
23+
24+
<!-- 添加学生按钮 -->
25+
<div th:if="${#authorization.expression('hasRole(''ROLE_ADMIN'')')}">
26+
<a href="/addStudent" class="btn btn-primary">添加学生</a>
27+
</div>
28+
29+
<!-- 删除学生按钮 -->
30+
<div th:if="${#authorization.expression('hasRole(''ROLE_MANAGER'')')}">
31+
<a href="/removeStudent" class="btn btn-danger">删除学生</a>
32+
</div>
33+
34+
<!-- 其他内容 -->
35+
<div>
36+
<p>欢迎使用学生管理系统。在这里您可以管理学生记录。</p>
37+
</div>
38+
</body>
39+
</html>
40+
```
41+
42+
在此模板中:
43+
- 使用`th:if="${#authorization.expression('hasRole(''ROLE_ADMIN'')')}"`语句仅在用户具有`ROLE_ADMIN`角色时条件显示“添加学生”按钮。
44+
- 同样,使用`th:if="${#authorization.expression('hasRole(''ROLE_MANAGER'')')}"`语句仅在用户具有`ROLE_MANAGER`角色时条件显示“删除学生”按钮。
45+
46+
## 解释
47+
48+
- **Thymeleaf安全方言**:`#authorization`对象是Thymeleaf安全方言的一部分,它将Spring Security与Thymeleaf集成在一起。
49+
- **条件显示**:使用`th:if`属性根据用户的角色有条件地显示或隐藏元素。
50+
51+
通过按照这些步骤,您可以根据用户的角色控制Thymeleaf视图中元素的可见性,确保只有授权用户才能看到和与特定功能进行交互。
52+
53+
---
54+
55+
# [下一个任务:根据权限授权用户](authorize-client-authority.md)

‎docs/cn/springsecurity/practice/configure-thymeleaf-security.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727

2828
---
2929

30-
[下一任务:]()
30+
# [下一个任务:为用户创建基本实现](base-implementation-user.md)

‎docs/cn/thymeleaf/lab-work.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
![login-page.png](../../srcs/thymeleaf/login-page.png)
4848

49-
![signup-page.png](../../srcs/thymeleaf/signup-page.png)
49+
![signup.png](../../srcs/thymeleaf/signup.png)
5050

5151
4. **任务 4:验证登录和注册操作**
5252

‎docs/en/jpa/practice/dependencies/jpa.md‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,130 @@ implementation "org.springframework.boot:spring-boot-starter-data-jpa"
5252

5353
This dependency simplifies the setup of JPA-based repositories and provides default configurations for Hibernate.
5454

55+
### Additional Configuration for Spring WebMVC
56+
57+
If you are using Spring WebMVC, you need to add additional configuration to set up persistence-related beans and properties.
58+
59+
#### Create a Persistence Configuration Class
60+
61+
Create a new class named `PersistenceConfig` in the `com.example.config` package:
62+
63+
```java
64+
package com.example.config;
65+
66+
import org.springframework.beans.factory.annotation.Value;
67+
import org.springframework.context.annotation.Bean;
68+
import org.springframework.context.annotation.Configuration;
69+
import org.springframework.context.annotation.PropertySource;
70+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
71+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
72+
import org.springframework.orm.jpa.JpaTransactionManager;
73+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
74+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
75+
import org.springframework.transaction.PlatformTransactionManager;
76+
import org.springframework.transaction.annotation.EnableTransactionManagement;
77+
78+
import javax.persistence.EntityManagerFactory;
79+
import javax.sql.DataSource;
80+
81+
/**
82+
* Configuration class for setting up persistence-related beans and properties.
83+
*
84+
* <p>
85+
* This class configures the data source, entity manager factory, and transaction manager for JPA.
86+
* It reads database connection properties from an external properties file.
87+
* </p>
88+
*/
89+
@Configuration
90+
@EnableTransactionManagement
91+
@EnableJpaRepositories(basePackages = "com.example.repository")
92+
@PropertySource({"classpath:application.properties"})
93+
public class PersistenceConfig {
94+
95+
@Value("${spring.datasource.driver}")
96+
private String dbDriver;
97+
98+
@Value("${spring.datasource.url}")
99+
private String dbUrl;
100+
101+
@Value("${spring.datasource.username}")
102+
private String dbUsername;
103+
104+
@Value("${spring.datasource.password}")
105+
private String dbPassword;
106+
107+
@Value("${spring.jpa.properties.hibernate.dialect}")
108+
private String hibernateDialect;
109+
110+
@Value("${spring.jpa.properties.hibernate.show_sql}")
111+
private boolean hibernateShowSql;
112+
113+
@Value("${spring.jpa.hibernate.ddl-auto}")
114+
private String hibernateHbm2ddlAuto;
115+
116+
/**
117+
* Bean definition for the DataSource.
118+
*
119+
* <p>
120+
* This method sets up the data source using the properties defined in the application.properties file.
121+
* </p>
122+
*
123+
* @return DataSource bean
124+
*/
125+
@Bean
126+
public DataSource dataSource() {
127+
DriverManagerDataSource dataSource = new DriverManagerDataSource();
128+
dataSource.setDriverClassName(dbDriver);
129+
dataSource.setUrl(dbUrl);
130+
dataSource.setUsername(dbUsername);
131+
dataSource.setPassword(dbPassword);
132+
return dataSource;
133+
}
134+
135+
/**
136+
* Bean definition for the EntityManagerFactory.
137+
*
138+
* <p>
139+
* This method sets up the entity manager factory using the configured data source and Hibernate properties.
140+
* </p>
141+
*
142+
* @param dataSource the data source bean
143+
* @return EntityManagerFactory bean
144+
*/
145+
@Bean
146+
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
147+
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
148+
em.setDataSource(dataSource);
149+
em.setPackagesToScan("com.example.entity");
150+
151+
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
152+
vendorAdapter.setDatabasePlatform(hibernateDialect);
153+
vendorAdapter.setShowSql(hibernateShowSql);
154+
vendorAdapter.setGenerateDdl("update".equalsIgnoreCase(hibernateHbm2ddlAuto));
155+
em.setJpaVendorAdapter(vendorAdapter);
156+
157+
return em;
158+
}
159+
160+
/**
161+
* Bean definition for the Spring Transaction Manager.
162+
*
163+
* <p>
164+
* This method sets up the transaction manager for JPA using the configured entity manager factory.
165+
* </p>
166+
*
167+
* @param entityManagerFactory the entity manager factory bean
168+
* @return TransactionManager bean
169+
*/
170+
@Bean
171+
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
172+
JpaTransactionManager txManager = new JpaTransactionManager();
173+
txManager.setEntityManagerFactory(entityManagerFactory);
174+
return txManager;
175+
}
176+
}
177+
```
178+
55179
---
56180

57181
# [Next: Entity Creation](../creation/create-entity.md)

0 commit comments

Comments
 (0)