Spring MVC 学习笔记8 —— 实现简单的用户管理(4)用户登录

Spring MVC 学习笔记8 —— 实现简单的用户管理(4)用户登录

增删改查,login

1. login.jsp,写在外面,及跟WEB-INF同一级目录,如:ls Webcontent; >> META-INF  WEB-INF  login.jsp

表单:<form>中包含两个参数,action(目标) & method (方法)。使用了<input>标签,type(text,password,submit), name,两个参数。

<form action = "user/login" method = "post">
username:<input type="text" name="username"/><br/>
password:<input type="password" name="password"/><br/>
<input type="submit"/>
</form>

2. userController.java

login.jsp点击“submit”以后,将访问userController里的login方法:Public
String login(){ }

· 指定进入method的条件

· 指定进入method的参数

· 异常处理1:用户名不存在, 异常处理2:密码不正确

· 把User存到Session中 【?】

·

@RequestMapping(value = "/login", method = RequestMethod.POST)//RequestMapping就是指定怎么进入这个方法来的
public String login(String username, String password, HttpSession session){ //传2个参数进来, 1st:username 2nd:password
//第3个参数是指定Session注入的
if (!users.containsKey(username)){ //异常处理1.如果不包含,则用户名不存在
//创建一个异常处理.java,见下“3. 需要一个处理异常的class”
throw new UserException("Username does not exist! ");
}
User u = users.get(username); //得到username
if (!u.getPassword().equals(password) ) {//if 不相等, 说明密码不正确:
throw new UserException("Password does not match!");
}
//把user 存到Session中:
session.setAttribute("loginUser",u);
return "redirect:/user/users"; //login success, jump to user/users
}

3. 需要一个处理异常的class

· 新建一个class:

  Name: UserException.class

  Superclass: java.lang.RuntimExcption

· Add Default Version ID: 代码: private static final long serialVersionUID
= 1L;

·创建一个Constructors构造器 from Superclass: 右键-Resources-Generate Constructors from Superclass

 

   package edu.bit.model;
public class UserException extends RuntimeException {
public UserException() {
super();
// TODO Auto-generated constructor stub
} public UserException(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
super(arg0, arg1, arg2, arg3);
// TODO Auto-generated constructor stub
} public UserException(String arg0, Throwable arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
} public UserException(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
} public UserException(Throwable arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}

==========至此,基本完成一个登陆操作的编写,但如果用户名不存在或密码不正确,将返回500错误,并按UserException("错误提示")提示错误 ========

测试链接: http://localhost:8080/myhello/login.jsp

500错误页截图:

Spring MVC 学习笔记8 —— 实现简单的用户管理(4)用户登录

实现简单的用户管理(4.2)用户登录--显示局部异常信息

第二部分:显示局部异常信息,而不是500错误页

1. 写一个方法,把UserException传进来。

2. 映射方式:@ExceptionHandler (), 括号里value是UserException.class

3. 传参进入UserException方法:(UserException ue, HttpServletRequest req)

-----//1.把 UserException传进来;

-----//2.不能用model来传值,因为不是RequestMapping,用HttpSeverletRequest req

4. 把异常对象存入HttpSeverletRequest req的e参数:      req.setAttribute("e",ue);

5. 返回视图error:        return error;



----实现代码-----

UserController.java中加入:

	/*
* 显示局部的异常信息:仅仅只能处理这个控制器中的异常
* 写一个方法,HandlerException, 把 UserException传进来
*/
@ExceptionHandler(value={UserException.class}) //用ExceptionHandler来映射,要处理的value是一个数组
//要处理一个对象就这样写,可处理多个对象。
public String handlerException(UserException ue, HttpServletRequest req){
//1.把 UserException传进来;
//2.不能用model来传值,因为不是RequestMapping,用HttpSeverletRequest req
//把异常对象ue存进去:
req.setAttribute("e", ue); //req 的 “e”参数,被set为 uexception
return "error";
}

/WEB-INF/error.jsp中代码:${e.message}
调取message信息:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Error page</title>
</head>
<body>
<h1>
${e.message}
</h1>
</body>
</html>
上一篇:Spring MVC 学习笔记1 - First Helloworld by Eclipse【& - java web 开发Tips集锦】


下一篇:使用 IntelliJ IDEA 2016和Maven创建Java Web项目的详细步骤及相关问题解决办法