用SpringMVC开发主要是要要会导入spingmvc文件,然后就是编写接口
package com.imooc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import json.core.ListObject;
import json.responseUtils.ResponseUtils;
import json.status.StatusHouse;
import json.utils.JackJsonUtils;
import model.StudentInfo;
import model.StudentPerform;
import business.BDOperationStudentInfo;
import business.BDOperationStudentPerform;
import business.CheckLogin;
import org.json.JSONException;
import org.json.JSONObject;
@Controller
public class DataPort {
@RequestMapping(value="/insertStudentInfo")
public void insertStudentInfo(@RequestBody String params, HttpServletResponse response){
// 组织前端数据成这种格式
// List list
// 已经处理好了 {"name":"name0","parentWeChatNick":"WeChat0","presentSchool":"sch0","lastFinalGrade":"0","parentTel":"0"}
// 暂时处理不了 [{"name":"name0","parentWeChatNick":"WeChat0","presentSchool":"sch0","lastFinalGrade":"0","parentTel":"0"}]
String data = params;
List list = StudentInfoStringToList(data);
ListObject listObject = new ListObject();
boolean operation = BDOperationStudentInfo.insertStudentInfo(list);
System.out.println(operation);
if(operation == true)
listObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
else {
listObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
}
// 放回是否成功insert
String responseText = JackJsonUtils.toJson(listObject);
//ResponseUtils.renderJson(response, responseText);
}
@RequestMapping(value="/updateStudentInfo" ,method=RequestMethod.POST)
public void updateStudentInfo(@RequestBody String params, HttpServletResponse response) {
// 组织前端数据成这种格式
// List list
String data = params;
//System.out.println("update" + params);
List list = StudentInfoStringToList(data);
ListObject listObject = new ListObject();
boolean operation = BDOperationStudentInfo.updateStudentInfo(list);
if(operation == true)
listObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
else {
listObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
}
// 放回是否成功update
String responseText = JackJsonUtils.toJson(listObject);
ResponseUtils.renderJson(response, responseText);
}
@RequestMapping("/quareStudentInfo")
public void quareStudentInfo(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
// TODO Auto-generated method stub
// 从前端传回两个参数
// String lie, String lie_value
request.setCharacterEncoding("utf-8");
String lie_value = request.getParameter("lie_value");
System.out.println("lie_value:" + lie_value);
// System.out.println(lie_value);
List list = new ArrayList();
ListObject listObject = new ListObject();
list = BDOperationStudentInfo.quareStudentInfoByCol(lie_value);
if(list.size() > 0)
listObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
else {
listObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
}
listObject.setItems(list);
String responseText = JackJsonUtils.toJson(listObject);
ResponseUtils.renderJson(response, responseText);
}
@RequestMapping("/deleteStudentInfo")
public void deleteStudentInfo(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
// 从前端传回两个参数
// String lie, String lie_value
String lie_value = request.getParameter("lie_value");
ListObject listObject = new ListObject();
boolean operation = BDOperationStudentInfo.deleteStudentInfo(lie_value);
if(operation == true)
listObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
else {
listObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
}
String responseText = JackJsonUtils.toJson(listObject);
ResponseUtils.renderJson(response, responseText);
}
@RequestMapping(value="/insertStudentPerform", method=RequestMethod.POST)
public void insertStudentPerform(@RequestBody String params, HttpServletResponse response) {
// 组织前端数据成这种格式
// List list = StudentPerformStringToList(data);
System.out.println(list);
ListObject listObject = new ListObject();
try {
BDOperationStudentPerform.insertStudentPerform(list);
listObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
listObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
}
// 放回是否成功insert
String responseText = JackJsonUtils.toJson(listObject);
ResponseUtils.renderJson(response, responseText);
}
@RequestMapping(value="/updateStudentPerform", method=RequestMethod.POST)
public void updateStudentPerform(@RequestBody String params, HttpServletResponse response) {
// {"name":"name2","score":120,"homework":"no","late":"no","publish":"no","teachercomment":"good 2"}
// 组织前端数据成这种格式
// List list
String data = params;
List list = StudentPerformStringToList(data);
ListObject listObject = new ListObject();
boolean operation = BDOperationStudentPerform.updateStudentPerform(list);
if(operation == true)
listObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
else {
listObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
}
listObject.setItems(list);
// 放回是否成功update
String responseText = JackJsonUtils.toJson(listObject);
ResponseUtils.renderJson(response, responseText);
}
@RequestMapping(value="/quareStudentPerform")
public void quareStudentPerform(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
// 从前端传回两个参数
// String lie, String lie_value
String lie = request.getParameter("lie");
String lie_value = request.getParameter("lie_value");
System.out.println(lie);
System.out.println(lie_value);
List list = new ArrayList();
ListObject listObject = new ListObject();
try {
list = BDOperationStudentPerform.quareStudentPerformByCol(lie, lie_value);
listObject.setItems(list);
listObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
listObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
}
String responseText = JackJsonUtils.toJson(listObject);
ResponseUtils.renderJson(response, responseText);
}
@RequestMapping("/deleteStudentPerform")
public void deleteStudentPerform(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
// 从前端传回两个参数
// String lie, String lie_value
String lie_value = request.getParameter("lie_value");
System.out.println(lie_value);
ListObject listObject = new ListObject();
try {
BDOperationStudentPerform.deleteStudentPerform(lie_value);
listObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
listObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
}
String responseText = JackJsonUtils.toJson(listObject);
ResponseUtils.renderJson(response, responseText);
}
public List StudentInfoStringToList(String data) {
String[] dataStr = data.substring(1, data.length()-1).split(",");
String[] dataArr = new String[dataStr.length];
for(int i = 0; i < dataStr.length; i++) {
String dataKey = dataStr[i].toString().split(":")[1];
dataArr[i] = dataKey.substring(1, dataKey.length()-1);
}
// "姓名","家长微信昵称","就读学校", "上次期末考试成绩", "家长电话号码"
StudentInfo student = new StudentInfo();
student.setName(dataArr[0]);
student.setparentWeChatNick(dataArr[1]);
student.setpresentSchool(dataArr[2]);
student.setlastFinalGrade(dataArr[3]);
student.setparentTel(dataArr[4]);
System.out.println("test" + student.toString());
List list = new ArrayList();
list.add(student);
return list;
}
public List StudentPerformStringToList(String data) {
String[] dataStr = data.substring(1, data.length()-1).split(",");
String[] dataArr = new String[dataStr.length];
for(int i = 0; i < dataStr.length; i++) {
String dataKey = dataStr[i].toString().split(":")[1];
dataArr[i] = dataKey.substring(1, dataKey.length()-1);
}
// 姓名 成绩 作业完成情况 考勤情况 受罚情况 老师评语
StudentPerform student = new StudentPerform();
student.setName(dataArr[0]);
student.setScore(Integer.parseInt(dataArr[1]));
student.setHomeWork(dataArr[2]);
student.setLate(dataArr[3]);
student.setPublish(dataArr[4]);
student.setTeachercomment(dataArr[5]);
List list = new ArrayList();
list.add(student);
return list;
}
@RequestMapping(value="/checkLogin", method=RequestMethod.POST)
public void checkLogin(@RequestBody String params, HttpServletResponse response) {
String login_info = params;
int login_way = 0;
String userName = new String();
String password = new String();
JSONObject login_info_json = new JSONObject();
try {
login_info_json = new JSONObject(login_info);
}catch (JSONException e) {
// TODO: handle exception
System.out.println(e);
System.out.println("json 转换出错");
}
login_way = login_info_json.getInt("login_way");
userName = login_info_json.getString("userName");
password = login_info_json.getString("password");
ListObject listObject = new ListObject();
CheckLogin isLogin = new CheckLogin();
if(isLogin.checklogin(login_way, userName, password)) {
System.out.println("登录/注册成功");
listObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
}else {
System.out.println("登录/注册失败");
listObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
}
String responseText = JackJsonUtils.toJson(listObject);
ResponseUtils.renderJson(response, responseText);
}
}
工程配置可以下载我的工程文件,照着处理一下。
注意如果你们不能直接把我的项目运行起来,建议新建web项目后,把我的项目中的包等拷贝过去。
有些隐式信息没有删,请不要乱搞哈。
web前后端分离开发——后端SpringMVC java开发
这个资源跟下面这个前端是配套的
web前后端分离开发——前端Vue