今日利用Spring三框架做一个登录界面

思路

我们自顶向下介绍思路,然后自底向上写代码
1.通过jsp中form向controller传送两个参数username,password
2.controller拿到参数后传给mapper层
3.mapper层进行sql语句执行得到的结果会返回controller的user对象中
4.在controller中如果user为空则返回错误信息并且通过return回到登录界面
如果查询到了信息则登陆成功

mapper层

mapper.xml中添加一个select标签取名为login

<select id="login" parameterType="User" resultType="User" >
	select id,name
	from user
	where username = #{username} and password = #{password}
	</select>

mapper.java接口

package cn.edu.wic.mapper;
import cn.edu.wic.entity.User;
public interface UserMapper {
	public User get(Integer id);
	public User login(User user);
	}

service层

service接口跟mapper接口是一样的

package cn.edu.wic.service;
import cn.edu.wic.entity.User;
public interface UserService {
	public User get(Integer id);
	public User login(User user);
	}

然后就是UserServiceImpl

package cn.edu.wic.service.impl;
import cn.edu.wic.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.edu.wic.service.UserService;
import cn.edu.wic.mapper.UserMapper;
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public User get(Integer id) {
		
		return userMapper.get(id);
	}
	@Override
	public User login(User user) {
		
		return userMapper.login(user);
	}
}

编写重要的控制器(controller)

controller中有一个小逻辑
user为空则登陆失败-即查询不到对象则登陆失败

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.wic.entity.User;
import cn.edu.wic.service.UserService;



@Controller
@RequestMapping("/login")
public class LoginController {
	@Autowired
	private UserService userService;
	@RequestMapping(method=RequestMethod.GET)
	public String get(){
		return "login";
	}
	@RequestMapping(method=RequestMethod.POST)
	public String post(@ModelAttribute("user") @Valid User user ,BindingResult errors,Model model){
		user = userService.login(user);
		if(user == null){
			model.addAttribute("error","用户不存在或者密码错误");
			return "login";
		}
		else{
			model.addAttribute("message",String.format("用户[%s]登录成功", user.getName()));
			return "login_success";
		}
		
		
		
	}
}

然后进入视图层

要编写两个视图 一个为登录界面的form表单
一个为登陆成功
login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"    %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"  %>
<!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>
<link type="text/css" rel="stylesheet"  href="<c:url value="/res/bootstrap/css/bootstrap.min.css"/>"></link>
</head>
<body>
	<div class="container">
		<h1 class="page-header">用户登录</h1>
		<form action="" method="post" class="form-horizontal">
			<div class="form-group">
				<label class="col-sm-4 control-label">用户名:</label>
				<div class="col-sm-3">
					<input type="text" name="username" class="form-control">
					<form:errors path="user.username"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-4 control-label">密码:</label>
				<div class="col-sm-3">
					<input type="password" name="password" class="form-control">
					<form:errors path="user.password"/>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-offset-4 col-sm-3 ">
					<button type="submit" class="btn btn-primary">登录</button>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-offset-4 col-sm-3 ">
					${error}
				</div>
			</div>
		</form>
	</div>
</body>
</html>

login_success.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>
  ${message}
  
</body>
</html>

可以进入浏览器运行试试看

在这里插入图片描述
在这里插入图片描述

这样就运行成功了

运行失败的话(用户名密码不正确情况)
就会出现错误信息
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值