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

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

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

增删改查,login

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

表单:

中包含两个参数,action(目标) & method (方法)。使用了标签,type(text,password,submit), name,两个参数。

1. <form action = "user/login" method = "post">
2. <span style="white-space:pre">	</span>username:<input type="text" name="username"/><br/>
3. <span style="white-space:pre">	</span>password:<input type="password" name="password"/><br/>
4. <span style="white-space:pre">		</span> <input type="submit"/>
5. </form>
  1. userController.java

login.jsp点击“submit”以后,将访问userController里的login方法:Public String login(){ }
· 指定进入method的条件
· 指定进入method的参数
· 异常处理1:用户名不存在, 异常处理2:密码不正确
· 把User存到Session中 【?】
· 

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

1. package edu.bit.model;
2. <span style="white-space: pre;">	</span>public class UserException extends RuntimeException {
3. <span style="white-space: pre;">	</span>public UserException() {
4. super();
5. // TODO Auto-generated constructor stub
6. }
7. 
8. public UserException(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
9. super(arg0, arg1, arg2, arg3);
10. // TODO Auto-generated constructor stub
11. }
12. 
13. public UserException(String arg0, Throwable arg1) {
14. super(arg0, arg1);
15. // TODO Auto-generated constructor stub
16. }
17. 
18. public UserException(String arg0) {
19. super(arg0);
20. // TODO Auto-generated constructor stub
21. }
22. 
23. public UserException(Throwable arg0) {
24. super(arg0);
25. // TODO Auto-generated constructor stub
26. }

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

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

文章知识点与官方知识档案匹配,可进一步学习相关知识Java技能树首页概览146525 人正在系统学习中

Published At
comments powered by Disqus