SSM实现简单登录注册

SSM实现简单登录注册

重构了一下以前的SSM登录注册案例,SSM的三大核心IOC,AOP,DI不用多说,网上随便翻一下都有 ,本篇直接上手,使用Maven工程搭建一个简单的SSM框架实现简单的登录注册,验证重名功能,适合刚上手的朋友看看

数据库就不贴了,俩字段,username和userpwd,指定username为主键

项目结构图

SSM实现简单登录注册_第1张图片

web.xml配置文件



	LogAndReg

	
	
		springmvc
		org.springframework.web.servlet.DispatcherServlet
		
		
			contextConfigLocation
			classpath:spring/*.xml
		
		1
	
	
		springmvc
		/
	
	
	
		CharacterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			utf-8
		
	
	
		CharacterEncodingFilter
		/*
	

springmvc.xml

 

	
	
	
	
	
	
	
	
		
		
		
		
	

applicationContext.xml

 

	
	
	
	
	
	
		
		
		
		
		
		
		
		
		
		
	
	
	
		
		
		
	
	
	
		
	
	
	
		
	
	
	

PagesController

package cn.gingost.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;


/*
 * 页面跳转方法
 */
@Controller
public class PagesController {
	@RequestMapping("/{page}")
	public String pageTo(@PathVariable String page) {
		return page;
	}
}

UserController

package cn.gingost.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 cn.gingost.pojo.User;
import cn.gingost.service.UserService;

@Controller
public class UserController {
	@Autowired
	private UserService userService;

	@RequestMapping("/golog")
	public String log(User user, Model model) {
		User u = userService.findOne(user);
		model.addAttribute("user", u);
		if (u != null) {
			return "logok";
		} else
			return "logno";
	}
	
	@RequestMapping("/goreg")
	public String reg(User user) {
		User u = userService.checkReg(user.getUsername());
		if (u == null) {
			userService.addOne(user);
			return "regok";
		}
		return "regno";
	}
}

UserMapper

package cn.gingost.dao;

import cn.gingost.pojo.User;

public interface UserMapper {
	//用户登录
	User findOne(User user);
	//用户注册
	void addOne(User user);
	//注册时的重名检测
	User checkReg(String username);
}

User

package cn.gingost.pojo;

public class User {
	private String username;
	private String userpwd;

	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;
	}

	@Override
	public String toString() {
		return "User [username=" + username + ", userpwd=" + userpwd + "]";
	}

}

UserService

package cn.gingost.service;

import cn.gingost.pojo.User;


public interface UserService {
	User findOne(User user);
	void addOne(User user);
	User checkReg(String username);
}

UserServiceImpl

package cn.gingost.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.gingost.dao.UserMapper;
import cn.gingost.pojo.User;
/*
 * Service层,本例过于简单,其实可以不写
 * 直接在Controller层中自动注入UserMapper usermapper
 * 使用userMapper对象直接调用方法即可
 */
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;

	@Override
	public User findOne(User user) {
		// TODO Auto-generated method stub
		return userMapper.findOne(user);
	}

	@Override
	public void addOne(User user) {
		userMapper.addOne(user);
		// TODO Auto-generated method stub

	}

	@Override
	public User checkReg(String username) {
		// TODO Auto-generated method stub
		return userMapper.checkReg(username);
	}

}

UserMapper.xml




	

	
		insert into user values(#{username},#{userpwd})
	
	

jdbc.properties

一个具有注脚的文本。[^2]

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/logandreg?useUnicode=true&characterEncoding=UTF8
user=root
pwd=root
maxActive=10
minIdle=5
initialSize=5

log.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>




用户登录



	
用户名:
密 码:
还没有账号?点此注册

reg.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>




用户注册



用户名:
密 码:

验证的jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


如果你能看见我,说明你成功了


您的用户名是:${user.username}
您的密码是:${user.userpwd}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Insert title here

用户名或者密码错误

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Insert title here

注册成功如果你能看见我,说明你成功了


点此返回登录界面 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Insert title here

该用户名已被注册

发布项目后,在浏览器地址栏中输入http://localhost:8080/LogAndReg/log进入登录页面,即可开始验证工程
这里不再展示演示结果

有问题欢迎留言探讨
源码下载地址

你可能感兴趣的:(SSM初学)