思路:用户输入登录信息——信息传到后台——数据库查询——比较查询结果——返回登录信息(成功/失败)
html页面代码:
1 DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>logintitle> 6 <script type="text/javascript" src="http://localhost:8080/ajax.js">script> 7 head> 8 <body> 9 <form> 10 <span>用户ID:span><input style="margin:2px;" id="id" name="id"><br> 11 <span>密码:span><input type="password" style="margin:2px;" id="password" name="password"><br> 12 <button style="margin:2px;" type="button" onclick="sumbit_name_sex()">登录button> 13 <button style="margin:2px;" type="reset">取消button><br> 14 form> 15 body> 16 html>
外部ajax.js代码:
1 function sumbit_name_sex() { 2 var id=document.getElementById("id").value; 3 var password=document.getElementById("password").value; 4 var user={ 5 id:id, 6 password:password, 7 } 8 //步骤一:创建异步对象s 9 var ajax = new XMLHttpRequest(); 10 //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端 11 ajax.open("post", "http://localhost:8080/login",true); 12 ajax.setRequestHeader("Content-type","application/json; charset=utf-8"); 13 //步骤三:发送请求 14 ajax.send(JSON.stringify(user)); 15 //步骤四:注册事件 onreadystatechange 状态改变就会调用 16 ajax.onreadystatechange = function () { 17 if (ajax.readyState == 4 && ajax.status == 200) { 18 //步骤五 如果能够进到这个判断 说明 数据 完美的回来了,并且请求的页面是存在的 19 console.log(ajax.responseText);//输入相应的内容 20 if(ajax.responseText.length>=3){ 21 alert("登录成功"); 22 }else{ 23 alert("登录失败"); 24 } 25 //window.location.href=""; 26 }else{ 27 28 } 29 } 30 }
后台controller层的代码:
1 package com.jhc.taskmanage.controller; 2 3 import com.alibaba.fastjson.JSONObject; 4 import com.jhc.taskmanage.model.User; 5 import com.jhc.taskmanage.service.UserService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RestController; 10 11 import javax.servlet.http.HttpServletRequest; 12 import java.io.InputStreamReader; 13 import java.util.List; 14 15 @Controller 16 @RestController 17 public class UserController { 18 19 @Autowired 20 private UserService userService; 21 22 @RequestMapping("/login") 23 public Listlogin(HttpServletRequest request){ 24 InputStreamReader inputStreamReader= null; 25 String json=""; 26 try{ 27 inputStreamReader=new InputStreamReader(request.getInputStream(),"utf-8"); 28 char[] buff=new char[1024]; 29 int length=0; 30 while((length=inputStreamReader.read(buff))!=-1){ 31 String json_temp=new String(buff,0,length); 32 json+=json_temp; 33 } 34 }catch (Exception ex){ 35 36 } 37 JSONObject jsonObject=JSONObject.parseObject(json); 38 String id=(String)jsonObject.get("id"); 39 String password=(String)jsonObject.get("password"); 40 return userService.login(id,password); 41 } 42 43 }
service层:
1 package com.jhc.taskmanage.service; 2 3 4 import com.jhc.taskmanage.model.User; 5 6 import java.util.List; 7 8 public interface UserService { 9 10 Listlogin(String id,String password); 11 }
1 package com.jhc.taskmanage.service.impl; 2 3 import com.jhc.taskmanage.dao.UserDao; 4 import com.jhc.taskmanage.model.User; 5 import com.jhc.taskmanage.service.UserService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Service; 8 9 import java.util.List; 10 11 @Service 12 public class UserServiceImpl implements UserService { 13 14 @Autowired 15 private UserDao userDao; 16 17 18 @Override 19 public Listlogin(String id,String password){ 20 return userDao.login(id,password); 21 } 22 }
dao层:
1 package com.jhc.taskmanage.dao; 2 3 4 import com.jhc.taskmanage.model.User; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Repository; 7 8 9 import java.util.List; 10 11 12 @Repository 13 public interface UserDao { 14 15 Listlogin(String id,String password); 16 }
1 package com.jhc.taskmanage.dao.impl; 2 3 import com.jhc.taskmanage.dao.UserDao; 4 import com.jhc.taskmanage.model.User; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.jdbc.core.JdbcTemplate; 7 import org.springframework.stereotype.Repository; 8 9 import java.util.ArrayList; 10 import java.util.List; 11 import java.util.Map; 12 13 @Repository 14 public class UserDaoImpl implements UserDao { 15 16 @Autowired 17 private JdbcTemplate jdbcTemplate; 18 19 @Override 20 public Listlogin(String id,String password){ 21 String sql="select id, name, sex, age from user where id='"+id+"' and password='"+password+"';"; 22 List
model层:
1 package com.jhc.taskmanage.model; 2 3 import org.springframework.stereotype.Component; 4 5 @Component 6 public class User { 7 private String id; 8 private String name; 9 private String sex; 10 private int age; 11 private String password; 12 13 public User(){ 14 15 } 16 17 public String getId() { 18 return id; 19 } 20 21 public void setId(String id) { 22 this.id = id; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public String getSex() { 34 return sex; 35 } 36 37 public void setSex(String sex) { 38 this.sex = sex; 39 } 40 41 public int getAge() { 42 return age; 43 } 44 45 public void setAge(int age) { 46 this.age = age; 47 } 48 49 public String getPassword() { 50 return password; 51 } 52 53 public void setPassword(String password) { 54 this.password = password; 55 } 56 }
spring boot启动类:
1 package com.jhc.taskmanage; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class TaskmanageApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(TaskmanageApplication.class, args); 11 } 12 13 }
配置文件application.properties:
1 server.port=8080 2 server.servlet.context-path=/ 3 #spring.mvc.static-path-pattern=/ 4 spring.resources.static-locations=classpath:/resources/,classpath:/static/,classpath:/js,classpath:/css,classpath:/image 5 spring.mvc.view.prefix=/WEB-INF/jsp/ 6 spring.mvc.view.suffix=.jsp 7 spring.datasource.url=jdbc:mysql://localhost:3306/myproject?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC 8 spring.datasource.username=root 9 spring.datasource.password=mysql 10 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 11 12 #spring.datasource.max-idle=10 13 #spring.datasource.max-wait=10000 14 #spring.datasource.min-idle=5 15 #spring.datasource.initial-size=5 16 17 #server.session.timeout=10 18 server.tomcat.uri-encoding=UTF-8
运行截图:
欢迎纠错,不喜勿喷