首页>代码>SSM+JQ+Ajax实现学生信息管理系统>/ssm-stu/stu_ssm/src/com/hp/school/controller/StudentController.java
package com.hp.school.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.hp.school.entity.Clazz;
import com.hp.school.entity.Student;
import com.hp.school.page.Page;
import com.hp.school.service.ClazzService;
import com.hp.school.service.StudentService;

import net.sf.json.JSONArray;

@RequestMapping("/student")
@Controller
public class StudentController {

	// ע��ѧ��
	@Autowired
	private StudentService studentService;
	// ע��ѧ��
	@Autowired
	private ClazzService clazzService;

	@RequestMapping("/upload_photo")
	@ResponseBody
	public Map<String, Object> uploadphoto(MultipartFile photo, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();

		// �ж� �Ƿ��Ѿ�ѡ��һ���ļ�
		if (photo == null) {
			map.put("type", "error");
			map.put("msg", "��ѡ���ļ�!");
			return map;
		}
		// 1. ѡ�е��ļ��Ƿ񳬹� �������ֵ
		if (photo.getSize() > 10485760) {
			map.put("type", "error");
			map.put("msg", "��������10M!");
			return map;
		}
		// 2.����ֻ�����ϴ�ͼƬ ,�ж� ��׺�Ƿ���ͼƬ
		String originalFilename = photo.getOriginalFilename(); // ��ȡ�ϴ��ļ���123.jpg

		// ��ȡ ��׺ jpg
		String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());
		if (!"jpg,png,gif,jpeg".contains(suffix.toLowerCase())) {
			map.put("type", "error");
			map.put("msg", "ֻ���ϴ�ͼƬ!");
			return map;
		}
		// 3. ��ȡ �ļ��ϴ� ������·��
		String serverPath = request.getServletContext().getRealPath("/") + "\\" + "upload" + "\\";
		// System.out.println(serverPath);
		File file = new File(serverPath);
		if (!file.exists()) {
			file.mkdir(); // ����������·�� ...tomcat/xx/xxx/upload
		}
		// 4.�ϴ�
		 String filename = new Date().getTime()+ originalFilename;
		try {
			photo.transferTo(new File(serverPath + filename));// �ϴ�
		} catch (IOException e) {
			e.printStackTrace();
		}
		// System.out.println(request.getServletContext().getContextPath()+"/"+originalFilename);
		map.put("type", "success");
		map.put("msg", "�ϴ��ɹ�");
		map.put("src", request.getServletContext().getContextPath() + "/" + "upload/" + filename   );
		return map;
	}

	// list
	 
	/*@RequestMapping(value="/list",method=RequestMethod.GET)
	public ModelAndView toLogin(ModelAndView model){
		model.setViewName("student/student_list");
		return model;
	}
	*/
	/**
	 * ��ת�� ѧ��jspҳ�� ,��תͬʱЯ�� ѧ�� �б���Ϣ
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(ModelAndView model){
		model.setViewName("student/student_list");
		//�� ����ѧ�� ��Ϣ ���б�
		List<Clazz> clazzList = clazzService.findAll();	
		model.addObject("clazzList", clazzList);	//�� ѧ���б� �󶨵� model��
		// ѧ�� �б� �� ��̬��ɵ� , ���������json��� , �� ѧ��ת��json , �Ա��� clazz_list.jspҳ�� �б���ʾjsonѧ����Ϣ
		model.addObject("gradeListJson", JSONArray.fromObject(clazzList));
		return model;
	}
	
	
	/**
	 * ���ѧ��
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,String> add(Student student){
		
		Map<String,String> map = new HashMap<String, String>();
		if(StringUtils.isEmpty(student.getUsername())){
			map.put("type", "error");
			map.put("msg", "ѧ������Ϊ��!");
			return map;
		}
		student.setSn("s2019"+new Date().getTime());
		//����ѧ��
		int result = studentService.add(student);
		
		// TODO  ѧ��  ,���������� 
		
		if(result<=0){
			map.put("type", "error");
			map.put("msg", "ѧ���ʧ��!");
			return map;
		}
		map.put("type", "success");
		map.put("msg", "���ѧ��ɹ�!");
		return map;
	}
	/**
	 * ��ȡѧ���б���� --  �� ������ѯ ��ҳ
	 * @return
	 */
	@RequestMapping(value="/get_list",method=RequestMethod.POST)
	@ResponseBody
	/**
	 * @param username	ģ���ѯ����
	 * @param page		��ҳ��
	 * @param gradeId		��� �꼶 ɸѡ ,���꼶����� ѧ����Ϣ
	 * @return
	 */
	public Map<String,Object> getList(
			@RequestParam(name="name",required=false,defaultValue="") String name,
			@RequestParam(name="clazzid",required=false) String clazzid,
			Page page
			){
		Map<String,Object> map = new HashMap<>();	// �������������
		// ���map ��ͬ��  QueryBean 
		Map<String,Object> queryMap = new HashMap<>();	// ��һ����ѯ������
		//ƴװ limit ?,? 
		queryMap.put("offset", page.getOffset());
		queryMap.put("pageSize", page.getRows());	
		queryMap.put("name", "%"+name+"%");
		if(clazzid!=null){	// �Ѿ�ѡ����һ���Ѵ��ڵ��꼶
			queryMap.put("clazzId", clazzid);
		}
		
		map.put("rows", studentService.getList(queryMap));		//�����ѯ�ĵ�2ҳ��ʾ��һ���������
		map.put("total", studentService.getTotal(queryMap));	//����������
		
		return map;
	}
}
最近下载更多
9632148963  LV1 2024年12月10日
skook7  LV2 2024年10月30日
hongdongdong  LV14 2024年6月18日
潘潘123456  LV2 2023年12月30日
uni-code_0123  LV1 2023年8月4日
douhongwen  LV1 2023年7月21日
ice_candy  LV1 2023年6月19日
493240689  LV3 2023年6月3日
微信网友_6469820124057600  LV6 2023年5月30日
liuchang183  LV5 2023年4月22日
最近浏览更多
暂无贡献等级
甜心冰淇淋  LV4 6月15日
xianyu091012  LV5 2024年12月26日
571818771  LV3 2024年12月16日
84126415  LV2 2024年12月10日
565236523 2024年12月10日
暂无贡献等级
9632148963  LV1 2024年12月10日
moxiao 2024年12月3日
暂无贡献等级
asdfgh112 2024年7月4日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友