LoginController
package com.example.login.controller;
import com.example.login.service.LoginService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
/**
* @ResponseBody
* 这个注解通常使用在控制层(controller)的方法上,
* 其作用是将方法的返回值以特定的格式写入到response的body区域,
* 进而将数据返回给客户端。当方法上面没有写ResponseBody,
* 底层会将方法的返回值封装为ModelAndView对象。
* @RestController
* 注解相当于@ResponseBody + @Controller合在一起的作用。
*
* @RequestMapping
* 是 Spring Web 应用程序中最常被用到的注解之一。
* 这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上。
* 并且一个处理请求地址映射的注解,可用在类或方法上。
* 用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
*
*
*/
@RestController
@RequestMapping("login")
public class LoginController {
@Resource
private LoginService loginService;
@RequestMapping("login")
public boolean login(HttpServletRequest request){
String username = request.getParameter("username");
String password = request.getParameter("password");
return loginService.login(username,password);
}
@RequestMapping("regist")
public boolean regist(HttpServletRequest request){
String username = request.getParameter("username");
String password = request.getParameter("password");
return loginService.regist(username, password);
}
}
LoginService
package com.example.login.service;
import com.example.login.dao.LoginDao;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class LoginService {
@Resource
private LoginDao loginDao;
public boolean login(String username, String password) {
int nums = loginDao.login(username, password);
if (nums == 0) {
return false;
} else {
return true;
}
}
public boolean regist(String username, String password) {
boolean b = loginDao.regist(username, password);
return b;
}
}
LoginDao
package com.example.login.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface LoginDao {
int login(@Param("username")String username,@Param("password")String password);
boolean regist(@Param("username")String username,@Param("password")String password);
}
LoginDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.login.dao.LoginDao">
<select id="login" resultType="int">
select count(*) from user
where username=#{username} and password=#{password}
</select>
<insert id="regist" >
insert into user (username, password)values (#{username},#{password})
</insert>
</mapper>
Login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../jquery-2.1.4.js"></script>
<style>
.a{text-align: center}
</style>
</head>
<body>
<div class="a">
用户名:<input type="text" id="username"><br>
密码:<input type="password" id="password"><br>
<button onclick="submit()">登陆</button>
<button onclick="regist()">注册</button>
<input type="reset" value="重置"/>
</div>
</body>
<script>
function submit()
{
var value = document.getElementById("username").value;
var value1 = document.getElementById("password").value;
$.ajax({
type: "POST",
url: "../login/login",
data: {
username: value,
password: value1
},
success: function (data) {
if (data) {
location.href = "../page/home"
} else {
alert("登陆失败")
}
},
error: function () {
alert("12")
}
});
}
function regist()
{
location.href = "../page/regist"
}
</script>
</html>
regist.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../jquery-2.1.4.js"></script>
</head>
<body>
<div style="text-align: center">
<h3>注册页面</h3>
用户名:<input type="text" id="username"><br>
密码:<input type="password" id="password"><br>
<button type="button" onclick="regist1()">注册</button>
<button type="button" onclick="back()">返回</button>
</div>
<script>
function regist1() {
var elementById = document.getElementById("username").value;
var elementById1 = document.getElementById("password").value;
$.ajax({
type: "POST",
url: "../login/regist",
data: {
username: elementById,
password: elementById1
},
success: function (data) {
if (data) {
alert("注册成功")
} else {
alert("失败")
}
},
error: function () {
alert("erro")
}
});
}
function back() {
location.href = "../page/login"
}
</script>
</body>
</html>
PageController
package com.example.login.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("page")
public class PageController {
@RequestMapping("login")
public String login(){
return "login";
}
@RequestMapping("regist")
public String regist(){
return "regist";
}
@RequestMapping("test")
public String test(){
return "test";
}
@RequestMapping("home")
public String home(){
return "home";
}
@RequestMapping("menu")
public String menu(){
return "menu";
}
@RequestMapping("modify")
public String modify(){
return "modify";
}
}