<div th:if="${session.user.role==1}">是什么意思在html标签中
时间: 2024-04-06 09:28:25 浏览: 90
这是 Thymeleaf 模板引擎的语法,它用于在 HTML 中嵌入服务器端的 Java 代码。在这个例子中,if 属性会根据 session 对象中 user 对应的 role 属性的值是否为 1,来判断是否显示包含在标签中的内容。如果 role 等于 1,那么这段代码中的内容会被渲染出来;否则,这段代码就会被忽略。
相关问题
漂亮的管理员界面显示用户信息,并且提供相应的查找和管理功能的html界面,并且和flask框架实现分页功能。前后端代码
前端代码(使用Bootstrap框架):
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>User Management</title>
<!-- Bootstrap core CSS -->
<link href="https://cdn.bootcss.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container-fluid">
<h1 class="text-center my-5">User Management</h1>
<div class="row">
<div class="col-md-6">
<form class="form-inline mb-4">
<input type="text" class="form-control mr-2" placeholder="Search">
<button type="submit" class="btn btn-primary">Search</button>
</form>
</div>
<div class="col-md-6 text-right">
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#add-user-modal">Add User</button>
</div>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>{{ user.phone }}</td>
<td>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#edit-user-modal" data-id="{{ user.id }}" data-name="{{ user.name }}" data-email="{{ user.email }}" data-phone="{{ user.phone }}">Edit</button>
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#delete-user-modal" data-id="{{ user.id }}">Delete</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
{% if page > 1 %}
<li class="page-item"><a class="page-link" href="?page={{ page-1 }}">Previous</a></li>
{% endif %}
{% for i in range(max_page) %}
{% if i+1 == page %}
<li class="page-item active"><a class="page-link" href="?page={{ i+1 }}">{{ i+1 }}</a></li>
{% else %}
<li class="page-item"><a class="page-link" href="?page={{ i+1 }}">{{ i+1 }}</a></li>
{% endif %}
{% endfor %}
{% if page < max_page %}
<li class="page-item"><a class="page-link" href="?page={{ page+1 }}">Next</a></li>
{% endif %}
</ul>
</nav>
</div>
<!-- Add user modal -->
<div class="modal fade" id="add-user-modal" tabindex="-1" role="dialog" aria-labelledby="add-user-modal-label" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form action="{{ url_for('add_user') }}" method="post">
<div class="modal-header">
<h5 class="modal-title" id="add-user-modal-label">Add User</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" required>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" name="email" required>
</div>
<div class="form-group">
<label>Phone</label>
<input type="tel" class="form-control" name="phone" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>
</div>
</div>
</div>
<!-- Edit user modal -->
<div class="modal fade" id="edit-user-modal" tabindex="-1" role="dialog" aria-labelledby="edit-user-modal-label" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form action="{{ url_for('edit_user') }}" method="post">
<div class="modal-header">
<h5 class="modal-title" id="edit-user-modal-label">Edit User</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" required>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" name="email" required>
</div>
<div class="form-group">
<label>Phone</label>
<input type="tel" class="form-control" name="phone" required>
</div>
<input type="hidden" name="id">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
<!-- Delete user modal -->
<div class="modal fade" id="delete-user-modal" tabindex="-1" role="dialog" aria-labelledby="delete-user-modal-label" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form action="{{ url_for('delete_user') }}" method="post">
<div class="modal-header">
<h5 class="modal-title" id="delete-user-modal-label">Delete User</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Are you sure you want to delete this user?
<input type="hidden" name="id">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</form>
</div>
</div>
</div>
<!-- Bootstrap core JavaScript -->
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script>
$('#edit-user-modal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var id = button.data('id')
var name = button.data('name')
var email = button.data('email')
var phone = button.data('phone')
var modal = $(this)
modal.find('input[name="id"]').val(id)
modal.find('input[name="name"]').val(name)
modal.find('input[name="email"]').val(email)
modal.find('input[name="phone"]').val(phone)
})
$('#delete-user-modal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var id = button.data('id')
var modal = $(this)
modal.find('input[name="id"]').val(id)
})
</script>
</body>
</html>
```
后端代码(使用Flask框架):
```python
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
email = db.Column(db.String(120), nullable=False, unique=True)
phone = db.Column(db.String(20), nullable=False)
@app.route('/')
def index():
page = int(request.args.get('page', 1))
per_page = 10
users = User.query.order_by(User.id).paginate(page, per_page=per_page)
max_page = users.pages
return render_template('index.html', users=users.items, page=page, max_page=max_page)
@app.route('/add_user', methods=['POST'])
def add_user():
name = request.form['name']
email = request.form['email']
phone = request.form['phone']
user = User(name=name, email=email, phone=phone)
db.session.add(user)
db.session.commit()
return redirect(url_for('index'))
@app.route('/edit_user', methods=['POST'])
def edit_user():
id = request.form['id']
name = request.form['name']
email = request.form['email']
phone = request.form['phone']
user = User.query.get(id)
user.name = name
user.email = email
user.phone = phone
db.session.commit()
return redirect(url_for('index'))
@app.route('/delete_user', methods=['POST'])
def delete_user():
id = request.form['id']
user = User.query.get(id)
db.session.delete(user)
db.session.commit()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)
```
快点帮我用spring boot生成一个完整的学生宿舍管理系统登录功能,在同一用户表中实现三个不同角色的登录,登录成功后跳转到对应不同页面
好的,下面是一个基本的实现过程:
1. 创建一个Spring Boot项目,添加web和thymeleaf依赖。
2. 在resources目录下创建application.properties文件,配置数据库连接信息。
```
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/student_dormitory?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
```
3. 创建数据库表,包括用户表和角色表,建立用户表和角色表之间的关联。
```
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`role_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_role_id` (`role_id`),
CONSTRAINT `fk_role_id` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
```
4. 添加实体类User和Role,包括属性、构造方法、getter和setter方法。
```
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String username;
private String password;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "role_id")
private Role role;
// 省略构造方法和getter/setter方法
}
@Entity
@Table(name = "role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
// 省略构造方法和getter/setter方法
}
```
5. 创建Repository接口UserRepository,继承JpaRepository,用于对用户表的操作。
```
public interface UserRepository extends JpaRepository<User, Integer> {
User findByUsername(String username);
}
```
6. 在resources/templates目录下创建登录页面login.html,包括用户名、密码和角色选择框。
```
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>学生宿舍管理系统</title>
</head>
<body>
<h1>学生宿舍管理系统</h1>
<form action="/login" method="post">
<div>
<label for="username">用户名:</label>
<input type="text" id="username" name="username">
</div>
<div>
<label for="password">密码:</label>
<input type="password" id="password" name="password">
</div>
<div>
<label for="role">角色:</label>
<select id="role" name="role">
<option value="1">管理员</option>
<option value="2">教师</option>
<option value="3">学生</option>
</select>
</div>
<div>
<input type="submit" value="登录">
</div>
</form>
</body>
</html>
```
7. 创建Controller类LoginController,添加登录方法和跳转到对应页面的方法。
```
@Controller
public class LoginController {
@Autowired
private UserRepository userRepository;
@GetMapping("/")
public String index() {
return "login";
}
@PostMapping("/login")
public String login(String username, String password, Integer role, HttpSession session) {
User user = userRepository.findByUsername(username);
if (user != null && user.getPassword().equals(password) && user.getRole().getId().equals(role)) {
session.setAttribute("user", user);
switch (role) {
case 1:
return "admin";
case 2:
return "teacher";
case 3:
return "student";
}
}
return "login";
}
}
```
8. 在resources/templates目录下创建三个页面admin.html、teacher.html和student.html,用于对应不同角色的页面。
9. 运行项目,打开浏览器访问http://localhost:8080/,输入正确的用户名、密码和角色,即可跳转到对应页面。
以上是一个基本的学生宿舍管理系统登录功能的实现过程,具体的业务功能可以根据实际需求进行扩展。
阅读全文