1.上一篇SSM框架搭建好了之后就要开始写功能了,现在来写一个简单的登录注册功能

这几个包是自己手动创建的,然后往里面写代码

2.代码详情

 package com.maike.controller;

 import javax.servlet.http.HttpServletRequest;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import com.maike.dto.UserDto;
import com.maike.model.User;
import com.maike.service.UserService;
/**
*
* @author tan
*
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService; /**
* toRegister: 跳转到注册页面<br/>
* @return
*/
@RequestMapping("/toRegister")
public String toRegister(){
return "register";
}
/**
* addUser:实现注册 <br/>
* @param userDto
* @return
*/
@RequestMapping("/addUser")
public ModelAndView addUser(UserDto userDto) {
int k = userService.addUser(userDto);
if(k > 0) {
ModelAndView view = new ModelAndView("login");
return view;
}else {
ModelAndView view = new ModelAndView("register");
return view;
}
}
/**
* 登录判断
* @param userDto
* @return
*/
@RequestMapping("/judgeLogin")
public ModelAndView judgeLogin(UserDto userDto) {
int k = userService.judgeLogin(userDto);
if(k > 0) {
ModelAndView view = new ModelAndView("success");
return view;
}else {
ModelAndView view = new ModelAndView("login");
return view;
}
} }

UserController

 package com.maike.dto;

 import java.io.Serializable;

 public class UserDto implements Serializable {

     /**
*
*/
private static final long serialVersionUID = 1L; private String userName; private String userPwd; private String reUserPwd; public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getUserPwd() {
return userPwd;
} public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
} public String getReUserPwd() {
return reUserPwd;
} public void setReUserPwd(String reUserPwd) {
this.reUserPwd = reUserPwd;
} public static long getSerialversionuid() {
return serialVersionUID;
} @Override
public String toString() {
return "UserDto [userName=" + userName + ", userPwd=" + userPwd + ", reUserPwd=" + reUserPwd + "]";
} }

UserDto

UserMapper.xml

  <!-- 通过用户名查询用户 -->
<select id="selectByName" parameterType="java.lang.String" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM user WHERE user_name=#{userName,jdbcType=VARCHAR}
</select>
<!-- 通过用户名密码查询 -->
<select id="selectByNamePwd" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
where user_name=#{userName,jdbcType=VARCHAR} and user_pwd=#{userPwd,jdbcType=VARCHAR}
</select>
<!-- 添加用户信息 -->
<insert id="insert" parameterType="com.maike.model.User">
INSERT INTO user(user_id, user_name, user_pwd) values(#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{userPwd,jdbcType=VARCHAR})
</insert>

UserMapper.java

     int insert(User user);

     User selectByName(String userName);

     User selectByNamePwd(@Param("userName") String userName,@Param("userPwd") String userPwd);
 package com.maike.service;

 import com.maike.dto.UserDto;

 public interface UserService {

     int addUser(UserDto userDto);

     int judgeLogin(UserDto userDto);

 }

UserService

 package com.maike.serviceImpl;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.maike.dao.UserMapper;
import com.maike.dto.UserDto;
import com.maike.model.User;
import com.maike.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper; /**
* addUser: 添加用户<br/>
*/
public int addUser(UserDto userDto) {
// TODO Auto-generated method stub
int k = 0;
String userName = userDto.getUserName();
String userPwd = userDto.getUserPwd();
String reUserPwd = userDto.getReUserPwd();
if("" == userName || "" == userPwd|| "" == reUserPwd) {
return k;
}
if(!userPwd.equals(reUserPwd)) {
return k;
}
User u = userMapper.selectByName(userName);
if(null != u) {
return k;
}
User user = new User();
user.setUserName(userName);
user.setUserPwd(reUserPwd);
k = userMapper.insert(user);
return k;
} /**
* 登录判断
*/
public int judgeLogin(UserDto userDto) {
// TODO Auto-generated method stub
int k = 0;
String userName = userDto.getUserName();
String userPwd = userDto.getUserPwd();
if("" == userName || "" == userPwd) {
return 0;
}
User user = userMapper.selectByNamePwd(userName, userPwd);
if(null != user) {
k = 1;
return k;
}
return 0;
} }

UserServiceImpl

JSP

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>注册页面</title>
</head>
<body>
<br/>
<br/>
<br/>
<form action="addUser">
<table align = "center" >
<tr>
<td>用户名</td><td><input type = "text" name = "userName"></td>
</tr>
<tr>
<td>密码</td><td><input type = "password" name = "userPwd"></td>
</tr>
<tr>
<td>确认密码</td><td><input type = "password" name = "reUserPwd"></td>
</tr>
<tr>
<td colspan="2" align = "center"><input type = "submit" value = "注册" style = "background-color: Cyan;color:blue"></td>
</tr>
</table>
</form>
</body>
</html>

register

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录页面</title>
</head>
<body>
<br/>
<br/>
<br/>
<form action="user/judgeLogin">
<table align = "center">
<tr>
<td>用户名</td><td><input type = "text" name = "userName"></td>
</tr>
<tr>
<td>密码</td><td><input type = "password" name = "userPwd"></td>
</tr>
<tr>
<td align = "left"><input type = "submit" value = "登录" style = "background-color: Cyan;color:blue"></td>
<td align = "right" ><button style = "background-color: Cyan;color:blue"><a href = "/SSM/user/toRegister">注册</a></button></td>
</tr>
</table>
</form>
</body>
</html>

login

3.最好部署到Tomcat上 运行成功

SSM 实现登录注册功能的更多相关文章

  1. Java Spring+Mysql+Mybatis 实现用户登录注册功能

    前言: 最近在学习Java的编程,前辈让我写一个包含数据库和前端的用户登录功能,通过看博客等我先是写了一个最基础的servlet+jsp,再到后来开始用maven进行编程,最终的完成版是一个 Spri ...

  2. Node.js实现登录注册功能

    使用Node.js + Navicat for mysql实现的登录注册功能 数据库中存在有”user_id,user_name,password,user_img,user_number“字段,其中 ...

  3. flask 开发用户登录注册功能

    flask 开发用户登录注册功能 flask开发过程议案需要四个模块:html页面模板.form表单.db数据库操作.app视图函数 1.主程序 # app.py # Auther: hhh5460 ...

  4. vue koa2 mongodb 从零开始做个人博客(一) 登录注册功能前端部分

    0.效果演示 插入视频插不进来,就很烦.可以出门右拐去优酷看下(点我!). 1.准备工作 1.1前端框架 前端使用了基于vue.js的nuxt.js.为什么使用nuxt.js? 首先我做的是博客的项目 ...

  5. JAVAEE_Servlet_20_登录注册功能

    实现登录注册功能 注册功能 import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import j ...

  6. JAVAEE——宜立方商城11:sso登录注册功能实现、通过token获得用户信息、Ajax跨域请求(jsonp)

    1. 学习计划 第十一天: 1.sso注册功能实现 2.sso登录功能实现 3.通过token获得用户信息 4.Ajax跨域请求(jsonp) 2. Sso系统工程搭建 需要创建一个sso服务工程,可 ...

  7. App登录注册功能,怎样做到用户体验最佳?

    用户登录系统,可以细分为三项功能模块,分别是:登录.注册和密码找回.本文作者将结合自身经历,谈谈他在做这块的时候一些想法,主要是涉及业务流程. 登录和注册功能,不论是PC端还是移动端,大多数产品都会涉 ...

  8. PHP实现用户登录注册功能

    初学php做了一些比较常见且有用的页面,放在上面记录一下咯 我是用了bootstrap框架里面的模态框做注册登陆页面,这样页面比较美观 页面效果: 注册成功条件/功能: 1)用户名不能冲突 2)两次密 ...

  9. 一个关于vue+mysql+express的全栈项目(三)------ 登录注册功能的实现(已经密码安全的设计)

    本系列文章,主要是一个前端的视角来实现一些后端的功能,所以不会讲太多的前端东西,主要是分享做这个项目学到的一些东西,,,,, 好了闲话不多说,我们开始搭建后端服务,这里我们采用node的express ...

随机推荐

  1. [PHP] Workerman中的注册树模式

    注册树模式是把对象挂到一个类的属性数组里,下次直接在这个数组里面取,保持全局唯一,一般在项目入口初始化的时候有用到.在workerman中一开始的就是个注册树模式的运用,下面是对他的模拟 <?p ...

  2. Spring学习的第一天

    Spring是以Ioc和Aop为内核,提供了表现层spring MVC 和持久层Spring JDBC等众多应用技术,还能整合开源世界众多著名的第三方框架和类库,成为使用最多的JavaEE企业应用开源 ...

  3. python serial模块使用,是pyserial而非serial

    import serial from serial.tools.list_ports import comports 运行这两句时分别遇到错误 第一个先提示 no module name of ser ...

  4. 《Web Development with Go》JWT认证

    时间晚了,先来一版调通的JWT普通认证, 明天再弄一个通过中间件,及gorilla,negroni库的认证, 这样正规些, 但认证通过之后,如何对应权限? 由于jwt-go从2升到3,还有rsa 10 ...

  5. C++ std::forward_list 基本用法

    #include <iostream> #include <string> #include <forward_list> using namespace std; ...

  6. go面向对象之多态即接口(interface)

    Go 语言接口 Go 语言提供了另外一种数据类型即接口,它把所有的具有共性的方法定义在一起,任何其他类型只要实现了这些方法就是实现了这个接口. 实例 /* 定义接口 */ type interface ...

  7. 一分钟了解Docker

    一.Docker概述 Docker和虚拟机一样,都拥有环境隔离的能力,但它比虚拟机更加轻量级,可以使资源更大化地得到应用. Docker用途:简单配置.代码流水线管理.开发效率.应用隔离.服务器整合. ...

  8. CSAPP lab1——位运算

    本次为一次计算机系统实验,就是使用一些基本的运算符来实现函数功能. ps做这些题让我想起大一上学期刚学二进制时被鹏哥支配的痛苦. 知识准备: 1.负数等于正数取反加一. 2.左移一位相当于将这个数扩大 ...

  9. C#深入浅出之更多数据类型

    类型的划分        一个类型,要么是值类型,要么是引用类型.区别在于拷贝方式:值类型拷贝值,引用类型拷贝引用 值类型        值类型直接包含值.相当于每一个值类型都有自己单独的值: int ...

  10. goweb-mysql连接

    操作 数据库 Go 语言中的 database/sql 包定义了对数据库的一系列操作.database/sql/driver 包定义了应被数据库驱动实现的接口,这些接口会被 sql 包使用.但是 Go ...