代码 现有一个用户信息管理网站,项目名称xmvc01,需要完成登录和用户内容展示两个功能。 1.登录功能:根据输入数据和数据库user表的数据进行比较,登录成功显示用户列表,登录失败返回登录页面2.用户列表:用户登录成功,以表格的形式显示user表所有数据信息。用户没有登录,将返回录页面现有项目结构如下内容:仅限院校复习接课使用经允洋请勿 仅限院 授课信描述 com.neu.pojo.User 对应数据表user的javaBean comneu.controller.LoginController用于用户登录功能 是否完成 是 否 用于用户列表显示访问路径(/users)否comneu.controller.UserController com.neudao.UserMapper 用户持久层接口月 com/neu/mapper/UserMapper.xmlmybatis配置文件 4
时间: 2024-02-24 13:54:22 浏览: 304
xmvc06-update.rar
以下是可能的代码实现,仅供参考:
User.java
```java
package com.neu.pojo;
public class User {
private Integer id;
private String username;
private String password;
// 省略getter和setter方法
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
}
```
LoginController.java
```java
package com.neu.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.neu.pojo.User;
import com.neu.service.UserService;
@Controller
public class LoginController {
@Autowired
private UserService userService;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String showLoginForm() {
return "loginForm";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(String username, String password, Model model) {
User user = userService.findUserByUsernameAndPassword(username, password);
if (user != null) {
model.addAttribute("users", userService.findAllUsers());
return "userList";
} else {
model.addAttribute("error", "用户名或密码错误");
return "loginForm";
}
}
}
```
UserController.java
```java
package com.neu.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.neu.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/users", method = RequestMethod.GET)
public String showUserList(Model model) {
model.addAttribute("users", userService.findAllUsers());
return "userList";
}
}
```
UserService.java
```java
package com.neu.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neu.dao.UserMapper;
import com.neu.pojo.User;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User findUserByUsernameAndPassword(String username, String password) {
return userMapper.findUserByUsernameAndPassword(username, password);
}
public List<User> findAllUsers() {
return userMapper.findAllUsers();
}
}
```
UserMapper.java
```java
package com.neu.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.neu.pojo.User;
public interface UserMapper {
User findUserByUsernameAndPassword(@Param("username") String username, @Param("password") String password);
List<User> findAllUsers();
}
```
UserMapper.xml
```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.neu.dao.UserMapper">
<resultMap type="com.neu.pojo.User" id="userResultMap">
<id column="id" property="id" />
<result column="username" property="username" />
<result column="password" property="password" />
</resultMap>
<select id="findUserByUsernameAndPassword" resultMap="userResultMap">
select * from user where username=#{username} and password=#{password}
</select>
<select id="findAllUsers" resultMap="userResultMap">
select * from user
</select>
</mapper>
```
loginForm.jsp
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h1>用户登录</h1>
<form action="${pageContext.request.contextPath}/login" method="post">
<p>
<label>用户名:</label> <input type="text" name="username" required>
</p>
<p>
<label>密码:</label> <input type="password" name="password" required>
</p>
<p>
<button type="submit">登录</button>
</p>
<p style="color: red">${error}</p>
</form>
</body>
</html>
```
userList.jsp
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户列表</title>
</head>
<body>
<h1>用户列表</h1>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>用户名</th>
<th>密码</th>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${users}">
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
```
以上代码可能有些细节需要根据您的实际情况进行修改,希望能给您提供一些帮助。
阅读全文