`

springmvc的rest风格之二实现用户登录(自定义异常处理)

 
阅读更多

自定义异常类UserException.java

package com.test.exception;

public class UserException extends RuntimeException {

	public UserException(String message) {
		super(message);
	}

}

 

Action控制器UserController.java中类名上添加:

@SessionAttributes("loginUser"),表示loginUsersession属性,为在用户列表上显示当前登录人做准备

然后添加:

/**
 * 登录前
 * 
 * @return
 */
@RequestMapping(value = {"/loginUserPro", "/"}, method = RequestMethod.GET)
public String loginUserPro() {
	return "user/loginUser";
}

/**
 * 登录
 * 
 * @param username
 * @param password
 * @param model
 * @return
 */
@RequestMapping(value = "/loginUser", method = RequestMethod.POST)
public String loginUser(String username, String password, Model model) {
	if (!users.containsKey(username)) {
		throw new UserException("用户名不存在!");// 异常捕获
	}
	if (!password.equals(users.get(username).getPassword())) {
		throw new UserException("密码不正确!");// 异常捕获
	}
	// 把登录用户名的信息保存到session的loginUser中,注意loginUser和上面@SessionAttributes中的参数保持一致
	model.addAttribute("loginUser", users.get(username));
	return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/user/userList";
}

// 局部异常处理
@ExceptionHandler(UserException.class)
public String handlerException(Exception e, HttpServletRequest request) {
	request.setAttribute("e", e);
	return "user/loginUser";
}

 

用户列表页面/jsp/user/userList.jsp添加:登录用户名:${loginUser.username }

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>用户列表</title>
</head>
<body>
	登录用户名:${loginUser.username }
	<table border="1">
		<tr>
			<th>用户名</th>
			<th>别名</th>
			<th>密码</th>
			<th>邮箱</th>
		</tr>
		<c:forEach items="${users}" var="user">
			<tr>
				<td>${user.value.username }</td>
				<td>${user.value.nickname }</td>
				<td>${user.value.password }</td>
				<td>${user.value.email }</td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>

 

登录页面/jsp/user/loginUser.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>添加用户</title>
</head>
<body>
	<!-- 显示自定义局部异常信息 -->
	<font color="red">${e.message }</font><br>
	<form method="post" action="loginUser">
		用户名:<input type="text" name="username"/><br>
		密码:<input type="password" name="password"/><br>
		<input type="submit" value="登录"/>
	</form>
</body>
</html>

 

在浏览器中输入:

http://127.0.0.1:9900/springmvc_005_rest_02/user/,页面随便填写,显示结果

 

 

用户名输入:ldh,密码输入:1,直接跳转到用户列表页面

 

知识扩展:

关于异常处理,spring提供了全局的异常处理:

去掉Action控制器UserController.java中的handlerException局部异常处理方法

springmvc配置文件spring-mvc.xml中添加关于全局异常处理的配置

<!-- 全局异常处理 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
	<property name="exceptionMappings">
		<props>
			<!-- 设置显示异常信息的视图逻辑 -->
			<prop key="com.test.exception.UserException">user/loginUser</prop>
		</props>
	</property>
</bean>

 

这个时候传递到loginUser.jsp页面上的存放异常信息参数名默认就是exception(修改登录页面/jsp/user/loginUser.jspeexception)

  • 大小: 1.8 KB
  • 大小: 1.8 KB
  • 大小: 4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics