web前後端分離開發——後端SpringMVC java開發

用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<StudentInfo> 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<StudentInfo> 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<StudentInfo> list
		String data = params;	
		//System.out.println("update" + params);
		List<StudentInfo> 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<StudentInfo> list =  new ArrayList<StudentInfo>();
		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<StudentPerform list
		String data = params;	
		System.out.println(data);
		List<StudentPerform> 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<StudentInfo> list
		String data = params;		
		List<StudentPerform> 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<StudentPerform> list =  new ArrayList<StudentPerform>();
		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<StudentInfo> 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<StudentInfo> list =  new ArrayList<StudentInfo>();
		list.add(student);
		
		return list;
	}
	

	public List<StudentPerform> 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<StudentPerform> list =  new ArrayList<StudentPerform>();
		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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章