ログインインタフェースと機能の実現(ssm)

4537 ワード






 












	$(function(){
		$(document).keyup(function(event){
			  if(event.keyCode==13){
			    login1();
			  }
		});
		
		$('#userName').textbox('setValue',"${cookie.userName.value}");
		
		
	});

	function login1(){
		if($('#userName').val()==""){
			$.messager.alert(' ',' !');
			return;
		}
		
		if($('#password').val()==""){
			$.messager.alert(' ',' !');
			return;
		}
		$.ajax({
			url:'${pageContext.request.contextPath}/user/login.action',
			data:$('#fm').serialize(),
			type:'post',
			success:function(map){
				if(map.msg=="1"){
					window.self.location='jsp/index.jsp';
					
				}else{
					$.messager.alert(' ',' ,'+map.msg);
				}
			},
			error:function(xhr,status,error){
				$.messager.alert(' ',xhr.responseText);
			}
			
		});
	}




	

せいぎょそう
package com.admin.controller;


@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	Map map = new HashMap();

	
	@RequestMapping("/login")
	@ResponseBody
	public Map login(String userName, String password, HttpSession session,HttpServletRequest request,HttpServletResponse response) {
		map.clear();
		try {
			User user = this.userService.userFind(userName);

			if (user != null && user.getPassword().equals(password)) {
				map.put("msg", "1");
				session.setAttribute("user", user);		
				
				Cookie ck= new Cookie("userName", userName);				
				String path=request.getContextPath();
				ck.setPath(path);				
		        ck.setMaxAge(30*24*60*60);
		        response.addCookie(ck);	  				
			}else{				
				map.put("msg", " , !");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", e.getMessage());
		}

		return map;
	}
}

ビジネス層
public User userFind(String userName) {
		return userMapper.selectUser(userName);
	}

dao層