项目源码github地址一直在更新…https://github.com/rijing29/RjStuManagement
整个功能实现的流程图
1.LoginController.java
package com.whj.stumanagement.rj_stumanagement_back.controller;
import com.whj.stumanagement.rj_stumanagement_back.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/stu")
public class loginController {
@Autowired
private StuService stuService;
@RequestMapping(value = "/login",produces = "application/json;charset=utf-8")
public List login(HttpServletRequest request, @RequestParam("sname") String sname, @RequestParam("spwd") String spwd){
int code=404;
boolean res = stuService.login(sname, spwd);
if(res) {
code = 200;
System.out.println("登录成功");
// 定义session对象并把数据存入session
HttpSession session = request.getSession();
session.setAttribute("sname",sname);
session.setAttribute("spwd",spwd);
}else
System.out.println("登陆失败,用户名或密码错误");
// 返回状态码和登录名
ArrayList<Object> result = new ArrayList<>();
result.add(0,code);
result.add(1,sname);
return result;
}
}
2.login.vue
// 登录
login(){
var url="/login"
var params={
'sname':this.sname,
'spwd':this.spwd
}
this.$http.get(url,{params}).then(res=>{
this.result=res.data
if(res.data[0]==200){
this.$message.success('登陆成功')
// 传用户名到home.vue
this.$router.push({
path:'/home',
query:{
sname:res.data[1]
}
})
}else{
this.$message.error('登陆失败,用户名或密码错误')
}
})
},
3.home.vue
// 获取用户名
getSname(){
var sname=this.$route.query.sname
this.sname=sname
console.log("传过来的sname")
console.log(this.sname)
},