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

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.User;
import com.hp.school.service.UserService;
import com.hp.school.utils.CpachaUtil;

/**
 * 控制器 --  等同于 struts2
 * 分模块开发 -- 为了区分不同的 模块 /system  路径
 */
@Controller
@RequestMapping("/system")
public class SystemController {
	
	@Autowired
	private UserService userService;

	/**
	 * 测试 工程
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public ModelAndView index(ModelAndView model){
		model.addObject("key", "dddddddddddd");
		model.setViewName("system/index");
		return model;
	}
	
	
	/**
	 * 跳转到 login.jsp
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public ModelAndView toLogin(ModelAndView model){
		model.setViewName("system/login");
		return model;
	}
	
	@RequestMapping(value="/vcode",method=RequestMethod.GET)
	public void get_cpacha(
			HttpServletResponse response,
			HttpServletRequest request,
			@RequestParam(name="vl",defaultValue="4",required=false) Integer vl,
			@RequestParam(name="w",defaultValue="98",required=false) Integer w,
			@RequestParam(name="h",defaultValue="33",required=false) Integer h
			){
		//1. 创建验证码 对象 
		CpachaUtil cpachaUtil = new CpachaUtil(vl,w,h);
		//2. 生成验证码
		String generatorVCode = cpachaUtil.generatorVCode();
		request.getSession().setAttribute("code", generatorVCode);//存入session
		
		//3. 生成背景图
		BufferedImage generatorVCodeImage = cpachaUtil.generatorVCodeImage(generatorVCode, true);
		
		//4. 将 generatorVCodeImage 返回到jsp页面
		try {
			ImageIO.write(generatorVCodeImage, "gif", response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 登陆
	 * @return   Map<String,String>  用map原因,方便 将后台数据 反馈给jsp
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody	//返回json数据   
	public Map<String,String> login(
			HttpServletRequest request,
			@RequestParam(name="username",required=true) String username,
			@RequestParam(name="password",required=true) String password,
			@RequestParam(name="vcode",required=true) String vcode,
			@RequestParam(name="type",required=true) int type
			){
		Map<String,String> map = new HashMap<String, String>();
		
		//后台验证 
		if(StringUtils.isEmpty(username)){	//判断用户名是否为空
			map.put("type", "error");
			map.put("msg", "用户名不能为空!");
			return map;
		}
		if(StringUtils.isEmpty(password)){	
			map.put("type", "error");
			map.put("msg", "密码不能为空!");
			return map;
		}
		
		if(StringUtils.isEmpty(vcode)){	//判断用户名是否为空
			map.put("type", "error");
			map.put("msg", "验证码不能为空!");
			return map;
		}
		
		//在session中取出验证码
		String code = (String) request.getSession().getAttribute("code");
		if(StringUtils.isEmpty(code)){	
			map.put("type", "error");
			map.put("msg", "长时间未操作,验证码失效!");
			return map;
		}
		
		
		if(!vcode.toUpperCase().equals(code.toUpperCase())){
			map.put("type", "error");
			map.put("msg", "验证码错误!");
			return map;
		}
		
		//TODO 访问数据库 
		// 清空session中 存的 验证码
		request.getSession().setAttribute("code", null);
		
		if(type==1){	//管理员
			User user = userService.findUserByUserName(username);
			if(user==null){
				map.put("type", "error");
				map.put("msg", "该用户不存在!");
				return map;
			}
			//验证密码
			if(!password.equals(user.getPassword())){
				map.put("type", "error");
				map.put("msg", "密码错误!");
				return map;
			}
			
			//用户没问题可以查询到 ,存入session
			request.getSession().setAttribute("user", user);
		}else{
			//TODO  学生登陆
			System.out.println("学生登陆.......");
		}
		
		map.put("type", "success");
		map.put("msg", "用户登陆成功!");
		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日
暂无贡献等级
时光海  LV2 2024年6月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友