您的位置:首页 > 其它

四、利用ssm进行登录和注册

2017-08-30 11:26 417 查看
依旧是转载~不过我会加上一些细节方面的修改,有些细节需要自己去摸索(http://blog.csdn.net/lxfHaHaHa/article/details/70142871

1)准备工作

如果你是跟着上一篇博客来的,那么就已经创建好了那三个自己添加的查询语句(http://blog.csdn.net/zyf2333/article/details/77709466),接下来做下准备工作:

首先添加如下目录(框起来的部分)。说明一下:zcController是控制器,UserService具体实现了UserMapper.java中的方法。然后在views文件夹下创建一个文件夹,里面三个文件分别对应着 登录,登录成功,注册 页面



接下来修改下web.xml,修改成如图所示。说明:此处修改后,我们再运行Tomcat7,就会令这个dl.jsp是默认页面,就可以直接进入登录页面。这里先看看最终效果的url



注意到此处的url就是localhost:8080,而不包含其他路径



2)代码

1、jsp文件的代码

dl.jsp(此处我们用了js来判断登录情况,
<script>
中的#{result}标签在zcController中设置,通过传入的参数来进行判断)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form id="zc" action="/zyf/dl" method="post">
<label for="name">用户名:</label> <input type="text" required id="name" name="name"><br>
<label for="pw">密码:</label> <input type="password" required id="pw" name="pw"><br>
<input type="submit" value="登录">
<input type="button" value="注册" onclick="location.href='/zyf/zc'">
</form>
<p>反馈信息:${result}</p>
</body>
<script>

//对应后台返回的提示
if ('${result}' != '') {
if ('${result}' == 0)
alert('该账户不存在!')
if ('${result}' == 1)
alert('密码错误!')
if ('${result}' == 2) {
alert('登录成功!')
location.href = '/zyf/userInfo'
}
}
</script>
</html>


zc.jsp(也是通过Controller来进行判断注册状况)

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form id="zc" action="/zyf/zc" method="post">
<label for="name">
用户名:
</label>
<input type="text" required id="name" name="name"><br>
<label for="pw">
密码:
</label>
<input type="password" required id="pw" name="pw"><br>
性别: <input type="radio" required value="0" name="sex">男 <input type="radio" value="1" required name="sex">女<br>
<input type="submit" value="注册">
<input type="reset" value="重置"/>
</form>
</body>
<script>
//对应后台返回的提示
if ('${result}' != '') {
if ('${result}' == 1) {
alert('注册成功,将跳转到登录页面!')
location.href = '/zyf/dl'
}
if ('${result}' == 0) {
alert('未知错误,注册失败!')
}
if ('${result}' == 2) {
alert('该账号已有人注册!')
}

}

</script>
</html>


userInfo.jsp(成功页面)

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
欢迎
</body>
</html>


2、UserService.java

这个类一般写法是implements UserMapper,然后实现它的抽象方法。但是此处我们用了自动注入,效果一样(记得类开头标上@Service,不然Spring无法识别这个bean,运行会失败!)

package com.springmvc.service;
import com.springmvc.dao.UserMapper;
import com.springmvc.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
/**
* Created by 邹一凡 on 2017/8/29.
*/
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public int insert(User user) {
return userMapper.insertSelective(user);
}

int deleteByPrimaryKey(Integer id) {
return userMapper.deleteByPrimaryKey(id);
}

User selectByPrimaryKey(Integer id) {
return userMapper.selectByPrimaryKey(id);
}

int updateByPrimaryKeySelective(User record) {
return userMapper.updateByPrimaryKeySelective(record);
}

int updateByPrimaryKey(User record) {
return userMapper.updateByPrimaryKey(record);
}

int getCount() {
return userMapper.getCount();
}

public ArrayList<User> selectSelective(User record) {
return userMapper.selectSelective(record);
}

public  ArrayList<User> selectLike(User record) {
return userMapper.selectLike(record);
}

}


3、zcController.java(在这里进行逻辑判断,并决定要跳到哪个页面去)

package com.springmvc.controller;

import com.springmvc.entity.User;
import com.springmvc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import sun.security.provider.MD5;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* Created by 邹一凡 on 2017/8/30.
*/
@Controller
public class zcController {
@Autowired
private UserService userService;

//注册
@RequestMapping(value = "/zyf/zc",method = RequestMethod.GET)
public String toZhuce(){
return "zyf/zc";
}
@RequestMapping(value = "/zyf/zc",method = RequestMethod.POST)
public String insertUser(@ModelAttribute User user, Model model){
User record=new User();
record.setName(user.getName());
List<User> list=userService.selectSelective(record);
if(list.size()==0){
user.setCreatetime(new Date());
user.setPw(user.getPw());
if(userService.insert(user)==1)
model.addAttribute("result",1);//注册成功
else
model.addAttribute("result",0);//注册失败
}
else
model.addAttribute("result",2);//已有人注册
return "zyf/zc";
}

//登录
@RequestMapping(value = "/zyf/dl",method = RequestMethod.GET)
public String toDenglu(){
return "zyf/dl";
}

//将信息保存在session中
@RequestMapping(value = "/zyf/dl",method = RequestMethod.POST)
public String toDenglu(HttpSession session,Model model,@ModelAttribute User user){
List<User> list=new ArrayList<User>();
User record=new User();
record.setName(user.getName());
list=userService.selectSelective(record);
if(list.size()==0)
model.addAttribute("result","0");//该用户不存在
else{
record.setPw(user.getPw());
list=userService.selectSelective(record);
if(list.size()==0)
model.addAttribute("result","1");//密码错误
else {
record=list.get(0);
session.setAttribute("user",record);
//将用户保存在session里面
model.addAttribute("result","2");//登录成功
}
}
return "zyf/dl";
}
//跳转到成功页面
@RequestMapping(value = "/zyf/userInfo", method = RequestMethod.GET)
public String toInfo(Model model, HttpSession session) {
User user = (User) session.getAttribute("user");
if (user != null)
model.addAttribute("user", user);
return "zyf/userInfo";
}
}


3)效果图

登录:





注册:





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: