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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.servlet.ModelAndView;

import com.hp.school.entity.Clazz;
import com.hp.school.entity.Grade;
import com.hp.school.page.Page;
import com.hp.school.service.ClazzService;
import com.hp.school.service.GradeService;

import net.sf.json.JSONArray;

@RequestMapping("/clazz")
@Controller
public class ClazzController {
	
	//注入班级
	@Autowired
	private GradeService gradeService;
	
	//注入班级
	@Autowired
	private ClazzService clazzService;
	
	/**
	 * 跳转到 班级jsp页面 ,跳转同时携带 班级 列表信息
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(ModelAndView model){
		model.setViewName("clazz/clazz_list");
		//绑定 所有班级 信息 到列表
		List<Grade> gradeList = gradeService.findAll();
		
		model.addObject("gradeList", gradeList);	//将 班级列表 绑定到 model中
		
		// 班级 列表 是 动态生成的 , 而且数据是json数据 , 将 班级转成json , 以便于 clazz_list.jsp页面 列表显示json班级信息
		model.addObject("gradeListJson", JSONArray.fromObject(gradeList));
		return model;
	}
	
	/**
	 * 添加班级
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,String> add(Clazz clazz){
		
		Map<String,String> map = new HashMap<String, String>();
		if(StringUtils.isEmpty(clazz.getName())){
			map.put("type", "error");
			map.put("msg", "班级名不能为空!");
			return map;
		}
		
		//保存班级
		int result = clazzService.add(clazz);
		
		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="gradeId",required=false) String gradeId,
			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(gradeId!=null){	// 已经选中了一个已存在的年级
			queryMap.put("gradeId", gradeId);
		}
		
		map.put("rows", clazzService.getList(queryMap));		//比如查询的第2页显示的一个集合数据
		map.put("total", clazzService.getTotal(queryMap));	//接收总数量
		
		return map;
	}
	
	
	/**
	 * 编辑班级
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,String> editUser(Clazz clazz){
		
		Map<String,String> map = new HashMap<String, String>();
		if(StringUtils.isEmpty(clazz.getName())){
			map.put("type", "error");
			map.put("msg", "班级名不能为空!");
			return map;
		}
		
		// 更新 /删除 /添加操作 最终 返回的是影响的行数 
		int result = clazzService.edit(clazz);
		
		if(result<=0){
			map.put("type", "error");
			map.put("msg", "班级编辑失败!");
			return map;
		}
		map.put("type", "success");
		map.put("msg", "编辑班级成功!");
		return map;
	}
	
	
	
	/**
	 * 删除班级
	 * @param user
	 * @return
	 * 
	 *  delete from user where id in (23,24,17)
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,String> delete(
			@RequestParam(name="ids[]",required=true)Integer[] ids){
		Map<String,String> map = new HashMap<>();
		// ids 非空判断 可以不写
		//需将 数组id转成 23,24,17
		String idsParam="";
		for (Integer id : ids) {
			idsParam += id+",";		// 23,24,17,
		}
		idsParam = idsParam.substring(0, idsParam.length()-1);
		
		try {
			//  通过业务层 调用删除方法 , 根据返回值判断 
			int result = clazzService.delete(idsParam);
			if (result <= 0) {
				map.put("type", "error");
				map.put("msg", "班级删除失败!");
				return map;
			} 
		} catch (Exception e) {
			
			map.put("type", "error");
			map.put("msg", "该班级下包含学生,请先删除学生 ,再删除班级!");
			return map;
		}
		
		
		map.put("type", "success");
		map.put("msg", "删除班级成功!");
		return map;
	}

}
最近下载更多
liuchang183  LV5 4月22日
1379585889  LV6 2022年12月17日
and123456  LV11 2022年7月11日
chen_jw  LV10 2022年6月20日
小顾顾顾顾顾  LV2 2022年5月31日
34385135  LV2 2022年5月31日
lvchengliang  LV4 2022年5月14日
ferrymen  LV6 2022年5月1日
zhoutao1346  LV2 2022年3月31日
最近浏览更多
xiaoding0324 5月28日
暂无贡献等级
ccs773 5月26日
暂无贡献等级
qq2901732871  LV7 5月19日
gaiung  LV1 5月15日
yuanye111  LV2 5月7日
Mrxiaox 5月2日
暂无贡献等级
liuchang183  LV5 4月22日
kk1031  LV8 4月13日
95959595959  LV13 4月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友