<form id="upload" action="http://localhost:9091/User/addWork/" method="post" enctype="multipart/form-data"> <div class="form-group"> <label>作评名称</label> <input id="name" name="work.name" type="text" class="form-control" placeholder="作品名称"> </div> <div class="form-group"> <label>摄影时间</label> <input id="shootingDate" name="work.shootingDate" type="text" class="form-control" placeholder="拍摄时间"> </div> <div class="form-group"> <label>拍摄地点</label> <input id="shootingAddress" name="work.shootingAddress" type="text" class="form-control" placeholder="拍摄地址"> </div> <div class="form-group"> <label>上传文件</label><br/> <input type="file" name="file"> </div>
时间: 2024-02-14 22:21:50 浏览: 17
这是一个表单代码,其中包括作品名称、摄影时间、拍摄地点和上传文件的输入框,表单的提交地址为"http://localhost:9091/User/addWork/",提交方式为POST。其中`enctype="multipart/form-data"`表示该表单数据是以二进制流的方式上传的,适用于文件上传等场景。
相关问题
写一个精美的登录注册带头像的ssm
好的,我会为您提供一个简单的登录注册带头像的SSM。
首先,我们需要创建一个数据库,用于存储用户信息和头像。我们可以创建一个名为“user_info”的表,包含以下字段:
- id (int, primary key, auto_increment):用户ID
- username (varchar(50)):用户名
- password (varchar(50)):密码
- email (varchar(50)):邮箱
- avatar (varchar(100)):头像路径
接下来,我们需要创建一个Java Web应用程序,并添加必要的依赖项。为了简单起见,我们将使用Spring Boot和MyBatis框架。
在pom.xml文件中添加以下依赖项:
```xml
<dependencies>
<!-- 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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- MyBatis dependencies -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
```
接下来,我们需要创建一个配置文件application.properties,用于配置数据库连接和MyBatis。
```properties
# Database configuration
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# MyBatis configuration
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.use-generated-keys=true
```
在src/main/resources目录下创建mapper目录,并添加UserMapper.xml文件,用于定义MyBatis映射器。
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<resultMap id="UserMap" type="com.example.demo.entity.User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="email" property="email"/>
<result column="avatar" property="avatar"/>
</resultMap>
<select id="getUserByUsername" resultMap="UserMap">
SELECT id, username, password, email, avatar
FROM user_info
WHERE username = #{username}
</select>
<select id="getUserById" resultMap="UserMap">
SELECT id, username, password, email, avatar
FROM user_info
WHERE id = #{id}
</select>
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user_info (username, password, email, avatar)
VALUES (#{username}, #{password}, #{email}, #{avatar})
</insert>
<update id="updateUser" parameterType="com.example.demo.entity.User">
UPDATE user_info
SET username = #{username}, password = #{password}, email = #{email}, avatar = #{avatar}
WHERE id = #{id}
</update>
</mapper>
```
接下来,我们需要创建一个User实体类,用于封装用户信息。
```java
public class User {
private int id;
private String username;
private String password;
private String email;
private String avatar;
// Getters and setters
}
```
然后,我们需要创建一个UserController类,用于处理用户登录、注册和头像上传等请求。
```java
@Controller
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/")
public String index() {
return "index";
}
@GetMapping("/login")
public String login(Model model) {
model.addAttribute("user", new User());
return "login";
}
@PostMapping("/login")
public String login(@ModelAttribute User user, HttpSession session) {
User dbUser = userMapper.getUserByUsername(user.getUsername());
if (dbUser != null && dbUser.getPassword().equals(user.getPassword())) {
session.setAttribute("user", dbUser);
return "redirect:/profile";
} else {
return "login";
}
}
@GetMapping("/register")
public String register(Model model) {
model.addAttribute("user", new User());
return "register";
}
@PostMapping("/register")
public String register(@ModelAttribute User user, HttpSession session) {
userMapper.insertUser(user);
session.setAttribute("user", user);
return "redirect:/profile";
}
@GetMapping("/profile")
public String profile(HttpSession session, Model model) {
User user = (User) session.getAttribute("user");
if (user == null) {
return "redirect:/login";
}
model.addAttribute("user", user);
return "profile";
}
@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file, HttpSession session) {
User user = (User) session.getAttribute("user");
if (user == null) {
return "redirect:/login";
}
try {
String filename = file.getOriginalFilename();
String filepath = "/avatars/" + user.getId() + "/" + filename;
File dest = new File(filepath);
dest.getParentFile().mkdirs();
file.transferTo(dest);
user.setAvatar(filepath);
userMapper.updateUser(user);
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/profile";
}
}
```
最后,我们需要创建一个Thymeleaf模板,用于显示登录、注册和个人资料页面。
index.html
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>SSM Login/Register Example</title>
</head>
<body>
<h1>Welcome to SSM Login/Register Example!</h1>
<p><a href="/login">Login</a> or <a href="/register">Register</a></p>
</body>
</html>
```
login.html
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form method="post" action="/login" th:object="${user}">
<p>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required="required" th:field="*{username}"/>
</p>
<p>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required="required" th:field="*{password}"/>
</p>
<p>
<button type="submit">Login</button>
</p>
</form>
</body>
</html>
```
register.html
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form method="post" action="/register" th:object="${user}">
<p>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required="required" th:field="*{username}"/>
</p>
<p>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required="required" th:field="*{password}"/>
</p>
<p>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required="required" th:field="*{email}"/>
</p>
<p>
<button type="submit">Register</button>
</p>
</form>
</body>
</html>
```
profile.html
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Profile</title>
</head>
<body>
<h1>Profile</h1>
<p>Hello, <span th:text="${user.username}"></span>!</p>
<img th:src="${user.avatar}" width="100" height="100"/>
<form method="post" action="/upload" enctype="multipart/form-data">
<p>
<input type="file" name="file"/>
<button type="submit">Upload</button>
</p>
</form>
</body>
</html>
```
以上就是一个简单的登录注册带头像的SSM应用程序的示例,您可以根据您的需求进行修改和扩展。
实现一个成绩管理网站,参考要求:前端:HTML的常用标签,包括不限于表单、表格、超链接等;CSS和Javascript使用外部导入方式;后台:至少包括文件上传和下载功能、注册和登录功能等,写出完整代码
好的,这里提供一个基本的成绩管理网站的代码,实现了文件上传和下载功能,以及注册和登录功能。
前端代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>成绩管理</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<h1>成绩管理</h1>
<form action="upload.php" method="post" enctype="multipart/form-data">
<h2>上传成绩单</h2>
<input type="file" name="file"><br>
<input type="submit" value="上传">
</form>
<hr>
<h2>成绩列表</h2>
<table>
<tr>
<th>姓名</th>
<th>语文</th>
<th>数学</th>
<th>英语</th>
<th>总分</th>
</tr>
<?php
// 从数据库中获取成绩列表并输出到表格中
require 'db.php';
$stmt = $pdo->query('SELECT * FROM scores');
while ($row = $stmt->fetch()) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['chinese'] . "</td>";
echo "<td>" . $row['math'] . "</td>";
echo "<td>" . $row['english'] . "</td>";
echo "<td>" . ($row['chinese'] + $row['math'] + $row['english']) . "</td>";
echo "</tr>";
}
?>
</table>
<hr>
<h2>下载成绩单</h2>
<form action="download.php" method="post">
<input type="submit" value="下载">
</form>
<hr>
<h2>注册</h2>
<form action="register.php" method="post">
<label for="username">用户名:</label>
<input type="text" name="username" required><br>
<label for="password">密码:</label>
<input type="password" name="password" required><br>
<input type="submit" value="注册">
</form>
<hr>
<h2>登录</h2>
<form action="login.php" method="post">
<label for="username">用户名:</label>
<input type="text" name="username" required><br>
<label for="password">密码:</label>
<input type="password" name="password" required><br>
<input type="submit" value="登录">
</form>
</body>
</html>
```
CSS代码:
```css
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: center;
padding: 8px;
}
th {
background-color: #4CAF50;
color: white;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
```
JavaScript代码:
```javascript
// 一些 JavaScript 交互代码
```
后台代码:
上传成绩单的 `upload.php`:
```php
<?php
// 处理上传的成绩单并存储到数据库中
require 'db.php';
if ($_FILES["file"]["error"] > 0) {
echo "上传失败:" . $_FILES["file"]["error"];
} else {
$filename = $_FILES["file"]["name"];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if ($ext != "xlsx" && $ext != "xls") {
echo "上传失败:文件格式不正确!";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $filename);
// TODO: 读取上传的成绩单并存储到数据库中
echo "上传成功!";
}
}
?>
```
下载成绩单的 `download.php`:
```php
<?php
// 下载成绩单
$filename = "uploads/成绩单.xlsx";
if (file_exists($filename)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
readfile($filename);
exit;
} else {
echo "文件不存在!";
}
?>
```
注册的 `register.php`:
```php
<?php
// 处理用户注册请求并存储到数据库中
require 'db.php';
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $pdo->prepare('INSERT INTO users (username, password) VALUES (?, ?)');
$stmt->execute([$username, $password]);
echo "注册成功!";
?>
```
登录的 `login.php`:
```php
<?php
// 处理用户登录请求并验证
require 'db.php';
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = ? AND password = ?');
$stmt->execute([$username, $password]);
$user = $stmt->fetch();
if ($user) {
echo "登录成功!";
} else {
echo "登录失败:用户名或密码错误!";
}
?>
```
其中,`db.php` 是数据库连接文件,内容如下:
```php
<?php
// 数据库连接信息
$host = 'localhost';
$dbname = 'database_name';
$username = 'username';
$password = 'password';
// 创建 PDO 连接
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
// 设置 PDO 错误模式为异常
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "数据库连接失败:" . $e->getMessage();
}
?>
```
需要注意的是,实际使用时需要根据自己的实际情况修改数据库连接信息和文件存储路径等相关参数。