在eclipse中建立SSM mybatisplus的web app的maven项目,方法如本博主的“亲手实践:eclipse中建立SSM框架的maven项目web app中使用mybatis plus”博文所示。
在该项目基础上,增加一个注册功能
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path=request.getContextPath();
String basePath=request.getScheme()+"://"+
request.getServerName()+":"+
request.getServerPort()+ path +"/";
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body>
<div>
<form action="${pageContext.request.contextPath }/user/login.do" method="post">
<center>
<label>账号:</label>
<input type="text" id="txtUsername" value="admin" name="username" placeholder="请输入账号" required/><br/>
<label>密码:</label>
<input type="password" id="txtPassword" name="password" value="admin" placeholder="请输入密码" required/><br/>
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</center>
</form>
</div>
<center><a href="<%=basePath%>reg.jsp">注册用户</a> <a href="<%=basePath%>lost.jsp">忘记密码</a></center>
reg.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path=request.getContextPath();
String basePath=request.getScheme()+"://"+
request.getServerName()+":"+
request.getServerPort()+ path +"/";
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body>
<div>
<form action="${pageContext.request.contextPath }/user/reg.do" method="post">
<center>
<label>账号:</label>
<input type="text" id="txtUsername" value="" name="username" placeholder="请输入账号" required/><br/>
<label>密码:</label>
<input type="password" id="txtPassword" name="password" value="" placeholder="请输入密码" required/><br/>
<label>确认密码:</label>
<input type="password" id="txtPassword" name="repassword" value="" placeholder="请输入确认密码" required/><br/>
<input type="submit" value="注册" />
<input type="reset" value="重置" />
</center>
</form>
</div>
UserController.java
package ks.sxxy.web;
import java.util.*;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.SessionScope;
import org.springframework.web.servlet.ModelAndView;
import ks.sxxy.dao.UserMapperByMybatisPlus;
import ks.sxxy.domain.User;
//import ks.sxxy.service.UserServiceByMybatisPlus;
import com.baomidou.mybatisplus.core.conditions.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import ks.sxxy.service.UserService;
/**
* 用户控制器
*/
@Controller
@RequestMapping(value = "/user")
public class UserController {
// @Autowired
//private UserServiceByMybatisPlus userServiceByMybatisPlus;
@Autowired
private UserMapperByMybatisPlus userMapperByMybatisPlus;
@RequestMapping("/login.do")
public ModelAndView login(User model,HttpSession session) {
QueryWrapper<User> userWrapper = new QueryWrapper<>();
userWrapper.eq("username", model.getUsername()).eq("password", model.getPassword());
User user = userMapperByMybatisPlus.selectOne(userWrapper);
if(user!=null) {
session.setAttribute("user", user);
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
return mav;
}
else
return new ModelAndView("redirect:/login.jsp");
}
@RequestMapping("/getAllUser.do")
public String getAllUser(Map<String, Object> map) {
List<User> allUser = userMapperByMybatisPlus.selectList(null);
map.put("allUser", allUser);
return "showuser";
}
@RequestMapping("/reg.do")
public void reg(User model, HttpServletResponse response){
QueryWrapper<User> userWrapper = new QueryWrapper<>();
userWrapper.eq("username", model.getUsername());
User user = userMapperByMybatisPlus.selectOne(userWrapper);
if(user==null) {
int ret = userMapperByMybatisPlus.insert(model);
if(ret>0) {
System.out.println("注册成功!");
//response.getWriter().print("注册成功!");
}
else {
System.out.println("注册失败!");
//response.getWriter().print("注册失败!");
}
}
else {
System.out.println("该用户已存在!");
//response.getWriter().print("<script language='javascript'>alert('您输入的账号或密码错误,请重新输入!');</script>");
}
}
}
运行后,
运行页面效果
单击注册用户,出现如下界面
如果注册admin用户,因为该用户已经存在,提示“该用户已存在!”
eclipse运行项目时,控制台中显示中文信息乱码:
......
6�� 22, 2023 6:29:56 ���� org.apache.coyote.AbstractProtocol start
��Ϣ: ��ʼЭ�鴦����["http-nio-8080"]
6�� 22, 2023 6:29:56 ���� org.apache.catalina.startup.Catalina start
��Ϣ: [11751]��������������
���û��Ѵ��ڣ�
解决方法:
菜单:Window -->preference弹出的对话框中将General中的Workspace中的Text file encoding改为Default(GBK).
单击应用之后,再次右键选择项目run as -->run on server,则中文显示正常,如下所示
......
6月 22, 2023 6:31:20 下午 org.apache.coyote.AbstractProtocol start
信息: 开始协议处理句柄["http-nio-8080"]
6月 22, 2023 6:31:21 下午 org.apache.catalina.startup.Catalina start
信息: [12189]毫秒后服务器启动
该用户已存在!
如果注册其他用户,显示如下
......
6月 22, 2023 6:31:20 下午 org.apache.coyote.AbstractProtocol start
信息: 开始协议处理句柄["http-nio-8080"]
6月 22, 2023 6:31:21 下午 org.apache.catalina.startup.Catalina start
信息: [12189]毫秒后服务器启动
该用户已存在!
注册成功!